Reputation: 5860
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?
Upvotes: 3
Views: 3128
Reputation: 5860
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
Upvotes: 2