【问题标题】:Winrm script not executing via JenkinsWinrm 脚本未通过 Jenkins 执行
【发布时间】:2017-11-13 17:52:03
【问题描述】:

我有一个 winrm 脚本,我正在使用 Jenkins 将这个脚本从 windows slave 执行到远程机器,并且我已将 psexec.exe、pservice.exe 复制到 windows slaves c:\windows\system32 文件夹中。但是当我从詹金斯执行下面的脚本时,它给出了一个错误,

PsExec.exe : 术语“PsExec.exe”未被识别为 cmdlet、函数、脚本文件或可运行的程序。检查 名称的拼写,或者如果包含路径,请验证路径 是正确的,然后再试一次。在 C:\Users\cicd\AppData\Local\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 窗口执行脚本时,它工作正常。但从詹金斯它给出了错误。

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

【问题讨论】:

    标签: windows powershell jenkins


    【解决方案1】:

    错误提示找不到PsExec.exe

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

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

    这将确保它被自动解决,或者

    • 按全名引用可执行文件

    即将代码中的PsExec.exe 替换为完整版C:\Path\To\File\PsExec.exe

    【讨论】:

    • 我试过上面的方法。由于 psexec 文件已经存在于 system32 文件夹中,即使从不同的目录运行它也需要。
    • 我试过上面的方法。由于 psexec 文件已经存在于 system32 文件夹中,即使从不同的目录运行它也需要它。但是脚本仍然给出错误。但是当我从从机运行脚本时,它运行没有任何问题
    • @Simith 你试过在脚本中使用可执行文件的全名吗?
    • 是的,我尝试过以下格式。但仍然是同样的问题 C:\Windows\System32\PsExec.exe \\$computer -accepteula -s winrm.cmd qc -quiet
    • 在您的代码顶部尝试放入 `Get-Item "C:\Windows\System32\PsExec.exe" 并查看是否也失败,如果也失败,则意味着脚本是被阻止查看它,或在由于其他原因无法看到它的上下文中执行。
    猜你喜欢
    • 2020-05-31
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多