Running PSEXEC under parallel processing Fail

TECH_GUY_MC
TECH_GUY_MC used Ask the Experts™
on
I am running the following PSEXEC code and this works fine if I run it serially in a normal powershell script loop.
c:\scripts\message\psexec.exe -s -i -d \\$_ wscript.exe "\\server\share\myscript.vbs" /message:"****A Message****"

When I try running it as a parallel processed task, I get the following message

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to ...Couldn't access :
Connecting to ...
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to ...Couldn't access :
Connecting to ...


Below is my Code for the Parallel processed task.
# Loop through the server list
 
Get-Content "computers.txt" | %{
 
  # Define what each job does
 
  $ScriptBlock = {
c:\scripts\message\psexec.exe -s -i -d \\$_ wscript.exe "\\server\share\myscript.vbs" /message:"****A Message****"
    Start-Sleep 15
  }
 
  # Execute the jobs in parallel
 
  Start-Job $ScriptBlock
}
 
Get-Job
 
# Wait for it all to complete
 
While (Get-Job -State "Running")
{
  Start-Sleep 10
}
 
# Getting the information back from the jobs
 
Get-Job | Receive-Job
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Any reason you are not useing PowerShell remoting to get to the remote PC? And for executing VBS instead of PS on the other machines?

Whatseover, obviously $_ in the script block is not resolved at all, according to the psexec error message. That is because the scope of $_ is the outer block, and it is not available in the inner script block.
You need to take a slightly different approach, e.g.
Get-Content "computers.txt"  | %{
 
  # Define what each job does
 
  $ScriptBlock = {
    c:\scripts\message\psexec.exe -s -i -d \\$args wscript.exe "\\server\share\myscript.vbs" /message:"****A Message****"
    Start-Sleep 15
  }
 
  # Execute the jobs in parallel
 
  Write-Host $ScriptBlock
  Start-Job $ScriptBlock -ArgumentList $_
}
 
Get-Job
 
# Wait for it all to complete
 
While (Get-Job -State "Running")
{
  Start-Sleep 10
}
 
# Getting the information back from the jobs
 
Get-Job | Receive-Job

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Just to add to above: The Wait loop is not necessary, you can do the same with
   get-job  | wait-job

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial