4
votes

I am using environment credential to get the username and password. When I echo them they are printed perfectly as ****.

The next comes the powershell commands, when I run them separately, all the commands works perfectly. But through Jenkins pipeline it throws me the following error:

groovy.lang.MissingPropertyException: No such property: psw for class: groovy.lang.Binding

Can anyone explain is this correct way to incorporate powershell in Jenkins pipeline?

environment {
   CREDENTIAL = credentials('Test')
}

stage('Deployment') {
        steps {
                echo "$CREDENTIAL_USR"
                echo "$CREDENTIAL_PSW"
                powershell """($psw = ConvertTo-SecureString -String $CREDENTIAL_PSW -AsPlainText -Force)"""
                powershell """($mySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $CREDENTIAL_USR, $psw -Verbose)"""
                powershell """(Set-Item WSMan:/localhost/Client/TrustedHosts -Value "*" -Force)"""
                powershell """($session = New-PSSession -ComputerName "192.111.111.111" -Credential $mySecureCreds)"""
3
a bit late in the party, but did you end up finding an answer for this? - fhuseynli

3 Answers

2
votes

In case someone is here, and still trying to figure out what is the issue. I will share the solution that worked for me.

Use escaping before variable "$" sign in multi-line string.

powershell ("""
    \$psw = ConvertTo-SecureString -String \$CREDENTIAL_PSW -AsPlainText -Force
    \$mySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList \$CREDENTIAL_USR, \$psw -Verbose
    Set-Item WSMan:/localhost/Client/TrustedHosts -Value "*" -Force
    \$session = New-PSSession -ComputerName "192.111.111.111" -Credential \$mySecureCreds
""")
1
votes

At the moment you're running each line in its own powershell process, so the results of the line before are not available to the next command.

I think you just need to move the script into a multi-line string:

powershell ("""
    $psw = ConvertTo-SecureString -String $CREDENTIAL_PSW -AsPlainText -Force
    $mySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $CREDENTIAL_USR, $psw -Verbose
    Set-Item WSMan:/localhost/Client/TrustedHosts -Value "*" -Force
    $session = New-PSSession -ComputerName "192.111.111.111" -Credential $mySecureCreds
""")
1
votes

You can easily run multiline powershell commands in jenkins pipeline like this. Example, if you want to login to azure using service principal, you'll do something like below:

powershell '''
            $pass = ConvertTo-SecureString your_client_secret -AsPlainText –Force
            $cred = New-Object -TypeName pscredential –ArgumentList your_client_id, $pass
            Login-AzureRmAccount -Credential $cred -ServicePrincipal –TenantId your_tenant_id
            -vaultName "eusdevmbe2keyvault" -name "normalizedcontainername").SecretValueText
           '''

Check here for reference https://jenkins.io/blog/2017/07/26/powershell-pipeline/