0
votes

I have a winrm script and I am using Jenkins to execute this script from windows slave to remote machine and I have copied psexec.exe, pservice.exe in windows slaves c:\windows\system32 folder. But when I execute the below script from jenkins it gives an error that

PsExec.exe : The term 'PsExec.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At 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:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException**

Code:

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}

       } 

    }  
} 

When I execute the script from windows slaves powershell window it works fine. but from jenkins it gives the error.

Does anyone have any idea why am I getting that error ?

1

1 Answers

1
votes

The error is stating that it can't find PsExec.exe

in the error you can see that it's mentioning AppData\Local\Temp\-.ps1, this means that the script is being launched from the temporary folder, rather than wherever you actually have PsExec stored, the easiest ways to resolve this are to either:

  • add the full path for PsExec to your $Path variable. link

this will ensure it is automatically resolved, or

  • Reference the executable by its full name

i.e. replace PsExec.exe in your code with the full version C:\Path\To\File\PsExec.exe