在 PowerShell 中捕获 Google 命令输出时出现问题

问题描述 投票:0回答:1

我正在尝试使用 GAM(Google 帐户管理?)在 PowerShell 中获取信息。有一个名为“whatis”的命令,在 PS 终端中运行时会给出这种输出...

PS C:\GamFolder> .\gam whatis gamdemo
[email protected] is a user

User: [email protected]
First Name: Gam
Last Name: Demo
.....
PS C:\GamFolder> .\gam whatis anotherdemo
[email protected] is a user alias

Alias Email: [email protected]
User Email: [email protected]
...
PS C:\GamFolder> .\gam whatis demogroup
[email protected] is not a user...
[email protected] is not a user alias...
[email protected] is a group

Group Settings:
...

我尝试通过将输出分配给变量并使用 Tee-Object 来捕获输出,每次顶部的重要的是/不是信息都会输出到控制台,而不是由变量捕获。 例如:

PS C:\GamFolder> $WhatIsInfo = .\gam whatis demogroup
[email protected] is not a user...
[email protected] is not a user alias...
[email protected] is a group

PS C:\GamFolder>
PS C:\GamFolder> $WhatIsInfo

Group Settings:
....

我也尝试将 stderr 重定向到 stdout,但随后 is/is not 信息就一起消失了。

PS C:\GamFolder> $WhatIsInfo = .\gam whatis demogroup 2>1
PS C:\GamFolder> $WhatIsInfo

Group Settings:
...

还有其他人遇到过类似的事情吗?我只是缺少一些简单的东西吗?

谢谢你。

powershell stdout
1个回答
0
投票

谢谢@js2010,

您的评论起作用了并且它让我意识到当我第一次尝试该命令时我错过了&。我刚刚尝试了 '$WhatIsInfo = .\gam Whatis demogroup 2>&1' 并且效果非常好。

以防万一其他人偶然发现这篇文章并提出有关“gam Whatis”的问题,我想我会提供一些正确命令的示例。

PS C:\GamFolder> $WhatIsInfo = .\gam whatis demogroup 2>&1
PS C:\GamFolder> 
PS C:\GamFolder> $WhatIsInfo
.\gam : [email protected] is not a user...
At line:1 char:15
+ $WhatIsInfo = .\gam whatis demogroup 2>&1
+            ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ([email protected] is not a user...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

[email protected] is not a user alias...
[email protected] is a group

Group Settings:
...

PS C:\GamFolder> $WhatIsInfo[0]
.\gam : [email protected] is not a user...
At line:1 char:15
+ $WhatIsInfo = .\gam whatis demogroup 2>&1
+            ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ([email protected] is not a user...:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PS C:\GamFolder> $WhatIsInfo[1]
[email protected] is not a user alias...
PS C:\GamFolder> $WhatIsInfo[2]
[email protected] is a group

谢谢大家的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.