Author Posts

January 1, 2012 at 12:00 am

by bobsdesk at 2012-08-29 10:09:17

I want to see the results of the commands from the remote computer that I issue using:
Invoke-Command -Session $Session -ScriptBlock {Remove-Item C:\SC\*.opn}, I get no results
Invoke-Command -Session $Session -ScriptBlock {Write-Output Remove-Item C:\SC\*.opn}, I see only the text of the command itself, not the result.

If I create a .bat file on the remote computer that has the same command (in this case DEL c:\SC\*.opn)
Invoke-Command -Session $Session -ScriptBlock {C:\Batch.bat}, I get the command as well as the results (in this case No Files Found).

How can I get the similar behavior without using a local batch file on the remote computer?

by RichardSiddaway at 2012-08-29 10:30:06

Try using -verbose on your remove-item call

By default remove-item won't show any results for deleting the file

Follow these steps to see what I mean

PS> $s = New-PSSession -ComputerName $env:COMPUTERNAME
PS> Get-Process | out-file c:\temp\mytest.txt
PS> Remove-Item c:\temp\mytest.txt
PS> Get-Process | out-file c:\temp\mytest.txt
PS> Invoke-Command -Session $s -ScriptBlock {Remove-Item c:\temp\mytest.txt}
PS> Get-Process | out-file c:\temp\mytest.txt
PS> Invoke-Command -Session $s -ScriptBlock {Remove-Item c:\temp\mytest.txt -Verbose}
VERBOSE: Performing operation "Remove file" on Target "C:\temp\mytest.txt".

by bobsdesk at 2012-08-30 09:46:21

That seems to work fine for powershell commands but I have executable programs I'm running as well the display output to the remote console. But if I run a batch file that just calls that same executable I can see the the output from the remote console.

by poshoholic at 2012-08-30 11:13:42

It would probably be worth it for you to read this article:
http://rkeithhill.wordpress.com/2007/09/16/effective-powershell-item-7-understanding-output/

Output management gets better in PowerShell 3, but for PowerShell 2 and earlier there are some limitations.

Also, when invoking executables, if you're not getting the output you want, try using the 2&>1 redirection operator to redirect stderr output to stdout. This may help with some programs you use.