Author Posts

January 1, 2012 at 12:00 am

by seanthehulk at 2012-10-04 11:04:12

I am in the process of converting some of my server side VBScripts to PowerShell. One of these scripts copies a file down to the remote machine and then uses psexec to remotely execute that file with the administrator context.

So I have a couple questions.

Why is it when I execute something like nslookup I get back an array of strings. But when I execute psexec, I can't seem to capture what its writing to the console?

Secondly, is there a way to replicate VBScripts WshShell.Run Method where bWaitOnReturn = False? If I can't capture and parse what PSExec is doing, then I would rather just fire it off and hope all goes well.

Is there any way to not have the console capture what psexec is doing?

Is there a better way to execute psexec then Invoke-Expression?

What I am currently doing:
$strCMD = "psexec.exe -accepteula -s -d \\$strFQDN C:\Windows\$strClientFolder\$strServerFile"
Invoke-Expression -Command $strCMD

An Environment Disclaimer:
We still have many Windows XP Machines and PowerShell has unfortunately not been deployed to them.

Thank you

by JeffH at 2012-10-04 13:07:34

Powershell remoting would definitely be easier, if you could deploy PS v2 to the XP boxes, but that doesn't sound likely. I haven't tried PSExec in PowerShell but it may be that it doesn't write to StdOut. The only thing I can think for you to try is using Start-Process. In the mean time, I'll have to play with PSExec in PowerShell.

by JeffH at 2012-10-04 13:13:39

Using Invoke-Expression I was able to at least save the console output.

invoke-expression "c:\scripts\psexec \\jdhit-dc01 ipconfig" | out-file c:\work\out.txt

You could parse the text file I guess then.

by JeffH at 2012-10-04 13:18:42

Actually, this also works:

c:\scripts\psexec \\jdhit-dc01 ipconfig | out-file c:\work\b.txt

by JeffH at 2012-10-04 13:28:51

Finally, depending on what you need to do with the results, you might be able to do it directly from the output.

PS C:\> $results = c:\scripts\psexec \\jdhit-dc01 nbtstat -n | where {$_ -match "Unique"} | foreach {$data = $_.Trim().Split() | where {$_} ; New-Object psobject -property @{Name=$data[0];Type=$data[1]}}

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

Starting nbtstat on jdhit-dc01... jdhit-dc01...
nbtstat exited on jdhit-dc01 with error code 0.
PS C:\>
PS C:\> $results

Name Type
---- ----
JDHIT-DC01 <00>
JDHIT-DC01 <20>