5

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)"""
1
  • 2
    a bit late in the party, but did you end up finding an answer for this? Commented Feb 11, 2020 at 16:11

3 Answers 3

6

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
""")
Sign up to request clarification or add additional context in comments.

Comments

2

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/

Comments

1

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 Comment

I tried using the multi-line string as you suggested James. But even after that I am getting the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.