Spi*_*idy 5 powershell jenkins jenkins-plugins
我正在使用环境凭据来获取用户名和密码。当我回显它们时,它们被完美地打印为****.
接下来是 powershell 命令,当我单独运行它们时,所有命令都运行良好。但通过 Jenkins 管道,它向我抛出以下错误:
groovy.lang.MissingPropertyException:没有这样的属性:类的 psw:groovy.lang.Binding
谁能解释一下将 powershell 合并到 Jenkins 管道中的正确方法吗?
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)"""
Run Code Online (Sandbox Code Playgroud)
万一有人在这里,并且仍在试图找出问题所在。我将分享对我有用的解决方案。
在多行字符串中的变量“$”符号之前使用转义。
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
""")
Run Code Online (Sandbox Code Playgroud)
目前,您正在其自己的 powershell 进程中运行每一行,因此前一行的结果不可用于下一个命令。
我认为您只需将脚本移动到多行字符串中:
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
""")
Run Code Online (Sandbox Code Playgroud)