What I'm trying to achieve is to redirect standard output and standard error from running psexec -nobanner \\1.2.3.4 net localgroup Administrators
. When I redirect standard output, the result of the command changes. Something about capturing standard output, in any of the ways I've tried, seems to change the result. I'd like to know why and I'd like to get it working.
In PowerShell, if I run this:
psexec -nobanner \\1.2.3.4 net localgroup Administrators
I see this:
Couldn't access 1.2.3.4:
The trust relationship between this workstation and the primary domain failed.
(Where Couldn't access 1.2.3.4:
ends up, I briefly see Connecting to 1.2.3.4...
and something else flashes past too quickly to see.)
If I try to capture the output, using this:
$output = psexec.exe -nobanner \\1.2.3.4 net localgroup Administrators
I see:
Couldn't access 1.2.3.4:
The handle is invalid.
(As above, where Couldn't access 1.2.3.4:
ends up, I briefly see Connecting to 1.2.3.4...
.)
I realise that I need to redirect the error stream - that's where I started. But I can't even get the standard output without it changing. This question is about whey the output changes as soon as I try to capture it.
UPDATE
I've just noticed that if I run the same command (that works above in the PowerShell host)
psexec -nobanner \\1.2.3.4 net localgroup Administrators
in PowerShell ISE, I get the same error as below:
psexec : The handle is invalid.
At line:1 char:1
+ psexec -nobanner \\1.2.3.4 net localgroup Administrators
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The handle is invalid.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Connecting to 1.2.3.4...Couldn't access 1.2.3.4:
Connecting to 1.2.3.4...
So, why does running it in ISE give different output from the normal host?
Other things I've tried:
1. Start-Process
Start-Process -Wait -PSPath 'C:\Windows\PSTools\psexec.exe' -NoNewWindow `
-ArgumentList "-nobanner \\$ip net localgroup Administrators" `
-RedirectStandardError '.\tempError.log' -RedirectStandardOutput '.\tempOutput.log'
'O:'
Get-Content .\tempOutput.log
'E:'
Get-Content .\tempError.log
which gives:
O:
E:
The handle is invalid.
Connecting to 1.2.3.4...
Couldn't access 1.2.3.4:
Connecting to 1.2.3.4...
2. Redirect Standard Output only
psexec -nobanner \\1.2.3.4 net localgroup Administrators > psexec.log
which gives:
Couldn't access 1.2.3.4:
The handle is invalid.
[psexec.log is empty because I'm only redirecting Standard Output and PsExec writes its own messages to Standard Error.]
3. Redirect Standard Error only
I've noticed something else odd about this: if I only redirect Standard Error, it works (PsExec works, the command fails, the output is redirected):
psexec -nobanner \\1.2.3.4 net localgroup Administrators 2> psexec.log
The file psexec.log
contains:
psexec : The trust relationship between this workstation and the primary domain failed.
At line:1 char:1
+ psexec -nobanner \\1.2.3.4 net localgroup Administrators 2> ps ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The trust relat... domain failed.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Connecting to 1.2.3.4...
Couldn't access 1.2.3.4:
Connecting to 1.2.3.4...
4. Redirect all
psexec.exe -nobanner \\1.2.3.4 net localgroup Administrators *>&1 | Set-Variable -Name Output
which gives this:
psexec : The handle is invalid.
At line:1 char:1
+ psexec -nobanner \\1.2.3.4 net localgroup Administrators *>&1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The handle is invalid.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Connecting to 1.2.3.4...Couldn't access 1.2.3.4:
Connecting to 1.2.3.4...
I repeated some of the above using cmd
:
5. Redirect Standard Output only, with cmd
cmd /c C:\Windows\PsTools\psexec -nobanner \\1.2.3.4 net localgroup Administrators --% 1> psexec.log
gives:
Couldn't access 1.2.3.4:
The handle is invalid.
(directly to the console). 6. Redirecting Standard Error only, with cmd
cmd /c C:\Windows\PsTools\psexec -nobanner \\1.2.3.4 net localgroup Administrators --% 2> psexec.log
gives (in psexec.log
):
The trust relationship between this workstation and the primary domain failed.
Connecting to 1.2.3.4...
Couldn't access 1.2.3.4:
Connecting to 1.2.3.4...