2017-06-12 132 views
0

我有一個winrm腳本,我正在使用Jenkins從windows slave執行這個腳本到遠程機器,並且我已經在windows從站c:\ windows \ system32文件夾中複製了psexec.exe,pservice.exe 。但是,當我從詹金斯執行下面的腳本,它給出了Winrm腳本不能通過Jenkins執行

PsExec.exe一個錯誤:術語「PsExec.exe」不被識別爲一個 cmdlet,函數的名稱,腳本文件或可操作的程序。檢查名稱的拼寫,或者如果包含路徑,請確認路徑 是正確的,然後重試。在 C:\ Users \ cicd \ AppData \ Local \ Temp \ hudson5218849623344511653.ps1:66 char:7 + PsExec.exe \ $ computer -accepteula -s C:\ Windows \ System32 \ winrm.cmd qc -qu。 .. + ~~~~~~~~~~ + CategoryInfo:ObjectNotFound:(PsExec.exe:字符串)[],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從屬powershell窗口執行腳本時,它工作正常。但從jenkins它給出了錯誤。

有沒有人有任何想法我爲什麼會得到那個錯誤?

回答

1

的錯誤,指出它不能在錯誤發現PsExec.exe

,你可以看到它提AppData\Local\Temp\-.ps1,這意味着腳本正在從臨時文件夾中啓動,而不是隻要你確實有PSEXEC存儲,解決此問題的最簡單方法是:

  • 將PsExec的完整路徑添加到您的$Path變量中。 link

這將確保它會自動解決,或者

  • 參考可執行文件由它的全名

即在你的代碼的完整版C:\Path\To\File\PsExec.exe

+0

我更換PsExec.exe嘗試以上方法。由於psexec文件已存在於system32文件夾中,因此即使正在從不同的目錄運行該文件。 – Shimith

+0

我試過以上方法。由於psexec文件已經存在於system32文件夾中,所以即使從不同的目錄運行它,也需要它。但是腳本仍然會給出錯誤。但是當我從奴隸機器運行腳本時,它運行時沒有任何問題 – Shimith

+0

@Shimith你是否嘗試過使用full腳本中可執行文件的名稱? – ConnorLSW