【发布时间】:2019-06-29 00:22:00
【问题描述】:
我想要实现的是从运行psexec -nobanner \\1.2.3.4 net localgroup Administrators 重定向标准输出和标准错误。当我重定向标准输出时,命令的结果会发生变化。以我尝试过的任何方式捕获标准输出似乎都会改变结果。我想知道为什么,我想让它工作。
在 PowerShell 中,如果我运行这个:
psexec -nobanner \\1.2.3.4 net localgroup Administrators
我看到了:
Couldn't access 1.2.3.4:
The trust relationship between this workstation and the primary domain failed.
(Couldn't access 1.2.3.4: 结束的地方,我短暂地看到了 Connecting to 1.2.3.4... 并且其他东西闪过太快而看不到。)
如果我尝试捕获输出,请使用:
$output = psexec.exe -nobanner \\1.2.3.4 net localgroup Administrators
我明白了:
Couldn't access 1.2.3.4:
The handle is invalid.
(如上,Couldn't access 1.2.3.4: 结束的地方,我简要地看到了Connecting to 1.2.3.4...。)
我意识到我需要重定向错误流 - 这就是我开始的地方。但是我什至不能得到标准输出而不改变它。这个问题是关于当我尝试捕获它时输出会发生变化的原因。
更新
我刚刚注意到,如果我运行相同的命令(在上面的 PowerShell 主机中运行)
psexec -nobanner \\1.2.3.4 net localgroup Administrators
在 PowerShell ISE 中,我收到与以下相同的错误:
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...
那么,为什么在 ISE 中运行它会得到与普通主机不同的输出?
我尝试过的其他事情:
1.启动进程
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
给出:
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。仅重定向标准输出
psexec -nobanner \\1.2.3.4 net localgroup Administrators > psexec.log
给出:
Couldn't access 1.2.3.4:
The handle is invalid.
[psexec.log 为空,因为我只重定向标准输出,而 PsExec 将自己的消息写入标准错误。]
3.仅重定向标准错误
我注意到其他一些奇怪的地方:如果我只重定向标准错误,它可以工作(PsExec 工作,命令失败,输出被重定向):
psexec -nobanner \\1.2.3.4 net localgroup Administrators 2> psexec.log
文件psexec.log 包含:
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.全部重定向
psexec.exe -nobanner \\1.2.3.4 net localgroup Administrators *>&1 | Set-Variable -Name Output
这给出了这个:
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...
我用cmd重复了上面的一些:
5.仅重定向标准输出,使用 cmd
cmd /c C:\Windows\PsTools\psexec -nobanner \\1.2.3.4 net localgroup Administrators --% 1> psexec.log
给予:
Couldn't access 1.2.3.4:
The handle is invalid.
(直接到控制台)。 6。仅重定向标准错误,使用 cmd
cmd /c C:\Windows\PsTools\psexec -nobanner \\1.2.3.4 net localgroup Administrators --% 2> psexec.log
给(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...
【问题讨论】:
-
如果你尝试
psexec.exe -nobanner \\1.2.3.4 net localgroup Administrators *>&1| Set-Variable -Name Output会发生什么? -
psexec :句柄无效。在 line:1 char:1 + psexec -nobanner \\1.2.3.4 net localgroup Administrators *>&1 ... + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (句柄无效。:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Connecting to 1.2.3.4...Couldn't access 1.2.3.4: Connecting to 1.2.3.4...
-
那是不可读的。请edit您的问题。
标签: powershell psexec