Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 200
Rob Wiley Avatar asked Jul 01 '10 00:07

Rob Wiley


People also ask

What is required to work with PsExec?

PsExec has simple requirements; File and Printer Sharing enabled and the admin$ administrative share available. You could go to all of the remote computers, open up the Windows Firewall applet, go to Allowed Apps and enable File and Printer Sharing on all computers as you see below.

What does PsExec EXE do?

PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software.

Does PsExec use PowerShell?

Jump to page sections. Invoke-PsExec is a function ("cmdlet") that lets you execute PowerShell and batch/cmd.exe code asynchronously on target Windows computers, using PsExec.exe. PsExec can be downloaded from the SysInternals suite on Microsoft's site here. It works with PowerShell version 2 and up.


2 Answers

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
like image 56
Lizz Avatar answered Sep 27 '22 23:09

Lizz


This hanging issue occurs on Win2003 and Win2008 servers.

Most people solve this issue with a workaround like echoing and piping so that powershell gets some input from STDIN.

But there exists a solution within powershell. Just start powershell with the option -inputformat none like:

powershell -inputformat none -command ...
like image 45
Walter Ebner Avatar answered Sep 28 '22 00:09

Walter Ebner