0

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 ?

4c74356b41
  • 59,484
  • 5
  • 63
  • 109
Shimith
  • 77
  • 1
  • 8

1 Answers1

1

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

colsw
  • 2,858
  • 1
  • 11
  • 24
  • I tried above methord. As the psexec file already there in system32 folder it takes even though am running it from different directory. – Shimith Jun 12 '17 at 14:21
  • I tried above methord. As the psexec file already there in system32 folder it takes even though am running it from different directory.But script still gives error.But when I run the script from slave machine it runs without any issue – Shimith Jun 12 '17 at 14:28
  • @Shimith have you tried it using the full name of the executable in the script? – colsw Jun 12 '17 at 14:41
  • Yes, I have tried in below format.But still same issue C:\Windows\System32\PsExec.exe \\$computer -accepteula -s winrm.cmd qc -quiet – Shimith Jun 12 '17 at 14:47
  • at the top of your code try put in `Get-Item "C:\Windows\System32\PsExec.exe" and see if that also fails, if that also fails it means the script is either being blocked from viewing it, or executing in a context where it can't see it for another reason. – colsw Jun 12 '17 at 15:06
  • yes, even now it fails.But I can execute the script locally from that windows slave machine without any issue – Shimith Jun 12 '17 at 15:36