从Jenkins执行PowerShell脚本时出错

时间:2019-06-13 21:21:53

标签: powershell jenkins

我有这个简单的PS脚本test.ps1,它将文件从一台服务器复制到另一台服务器。直接从Jenkins服务器执行时,脚本按预期工作。

$username = "domain\tester"
$pass = Get-Content "C:\test\Convert Password To Encrypted String\servername.txt" | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential - 
ArgumentList $username, $pass
Start-Sleep -Seconds 1.5
$Session = New-PSSession -ComputerName "servername" -Authentication 
Negotiate -Credential $cred
Copy-Item "C:\test.txt" -Destination "C:\test" -ToSession $Session

但是当从Jenkins UI调用相同的脚本时,它会失败并显示以下错误。

C:\Program Files (x86)\Jenkins\workspace\jenkinsJob>powershell -File C:\test.ps1 
New-PSSession : Cannot process argument transformation on parameter 
'Credential'. userName
At C:\test\test.ps1:8 char:91
+ ... ComputerName "servername" -Authentication Negotiate -Credential $cred
+                                                                     ~~~~~
    + CategoryInfo          : InvalidData: (:) [New-PSSession], ParameterBindi 
   ngArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Microsoft.P 
   owerShell.Commands.NewPSSessionCommand

Copy-Item : Cannot validate argument on parameter 'ToSession'. The argument is 
null or empty. Provide an argument that is not null or empty, and then try the 
command again.
At C:\test\test.ps1:9 char:120
+ ... build.test\dist\test.txt" -Destination "C:\test" -ToSession $Session
+                                                                  ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingVa 
   lidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power 
   Shell.Commands.CopyItemCommand
C:\Program Files (x86)\Jenkins\workspace\jenkinsJob>exit 0 
Finished: SUCCESS

我已验证所有PowerShell配置均已按预期实施。 已验证的Set-ExecutionPolicy无限制。 Jenkins将脚本称为powershell-文件路径。

0 个答案:

没有答案