Winrm脚本不通过Jenkins执行

时间:2017-06-12 09:45:41

标签: windows powershell jenkins

我有一个winrm脚本,我使用Jenkins从windows slave到远程机器执行此脚本,我已经在windows slave c:\ windows \ system32文件夹中复制了psexec.exe,pservice.exe。但是当我从jenkins执行以下脚本时,它会出现错误

  

PsExec.exe:术语“PsExec.exe”无法识别为a的名称   cmdlet,函数,脚本文件或可操作程序。检查   拼写名称,或者如果包含路径,请验证路径   是正确的,然后再试一次。在   C:\ Users \用户CICD \应用程序数据\本地\ TEMP \ hudson5218849623344511653.ps1:66   焦炭:7   + PsExec.exe \ $ computer -accepteula -s C:\ Windows \ System32 \ winrm.cmd qc -qu ...   + ~~~~~~~~~~       + CategoryInfo:ObjectNotFound:(PsExec.exe:String)[],CommandNotFoundException       + FullyQualifiedErrorId:CommandNotFoundException **

代码:

foreach ($computer in $hosts) {
$result = winrm id -r:$computer 2> $null
    if ($lastExitCode -eq 0) {
    Write-Host "WinRM already enabled on" $computer "..." -ForegroundColor green
    } else {
    Write-Host "Enabling WinRM on" $computer "..." -ForegroundColor red 
     PsExec.exe \\$computer -accepteula -s C:\Windows\System32\winrm.cmd qc -quiet 

    if ($LastExitCode -eq 0) {
        PsService.exe \\$computer -accepteula restart WinRM 
        $result  = winrm id -r:$computer 2>$null

    if ($LastExitCode -eq 0) {Write-Host "WinRM successfully enabled!" -ForegroundColor green}
        else {Write-Host "WinRM not enabled!" -ForegroundColor red}

       } 

    }  
} 

当我从windows slaves powershell窗口执行脚本时,它工作正常。但是从詹金斯那里得出错误。

有谁知道我为什么会收到这个错误?

1 个答案:

答案 0 :(得分:1)

该错误表明它无法找到PsExec.exe

在错误中你可以看到它提到AppData\Local\Temp\-.ps1,这意味着该脚本是从临时文件夹启动的,而不是你实际存储PsExec的地方,解决这个问题的最简单方法要么:

  • 将PsExec的完整路径添加到$Path变量中。 link

这将确保自动解决,或

  • 以可能的全名引用可执行文件

即。使用完整版PsExec.exe

替换代码中的C:\Path\To\File\PsExec.exe
相关问题