Search code examples
powershellpsexecstart-job

PSExec never completes when run inside start-job


I'm trying to execute a cmd file on a list of 48 computers. I don't want to execute and wait for completion sequentially because each cmd takes about 10 minutes to complete. WinRM isn't an option. Neither is WMI. PSExec is an option....but I can't seem to make it work inside of Start-Job.

I'm doing something like:

$sb = {
    param
    (
        $computer = "serverw01",
        $userid = "domain2\serviceid",
        $password = 'servicepw',
        $command = "cd /d d:\ && updateAll.cmd"
    )

    d:\eps\pstools\PsExec.exe -u $userid  -p $password "\\$($computer)" cmd /c $command
}

foreach ($computer in Get-Content "D:\Data\serverlist.txt") {
    Start-Job $sb -ArgumentList $computer
}

This creates a bunch of jobs....but the never complete and if I Receive-Job on any of them i get back

    PS> get-job | receive-job -Keep

    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

it executes just fine if I run the function like:

& $sb -computer "serverw01"

Initiating script is run in Powershell v2.0 on Server 2008r2 box I've tried it on a box in domain2 while logged in with a domain admin userid (same result).


Solution

  • Try this for the psexec command, ensuring you include "-d" to not wait for response, and put the computer variable right after psexec:

    d:\eps\pstools\psexec "\\$($computer)" /accepteula -u $userid -p $password -d cmd /c $command