简体   繁体   中英

How do I use a jenkins declarative pipeline credential and convert to a powershell credential?

How do I use a jenkins declarative pipeline credential and convert to powershell credential?

Using

 environment {
       APPLICATION_SERVER_CRED = credentials('app-cred')
 }

simply using the converted secure strings does not appear to work well since PSCredential constructor is expecting different types : String and SecureString

PSCredential(String, SecureString)

How do I take the environment variables that were created by jenkins pipeline

$ENV:APPLICATION_SERVER_CRED_USR
$ENV:APPLICATION_SERVER_CRED_PSW

and convert them into variables that I can then pass into the PSCredential constructor of System.Management.Automation.PSCredential?

Within the powershell script, the types of the two environment variables when running

($ENV:APPLICATION_SERVER_CRED_USR).GetType()

is

System.String

which wont work directly, since the constructor expects different types.

If jenkins did not convert the username to secure text, I would not have this problem since I would convert password to secure string and satisfy the contructor argument for PSCredential. But since they are secure text ( and type System.String), I cannot.

How can I convert the variables to create a PSCredential?

oh kay, Yeah that was easier than I thought. I guess masked text literally just masks it from being visible in UI. This works.

$backendServerPass = ConvertTo-SecureString $ENV:APPLICATION_SERVER_CRED_PSW -AsPlainText -Force
$backendServerCredential = New-Object System.Management.Automation.PSCredential ($ENV:APPLICATION_SERVER_CRED_USR, $backendServerPass) -Verbose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM