[英]How can I capture the Powershell informational console output to a variable?
I can't seem to figure out how to capture the quoted text below for parsing.我似乎无法弄清楚如何捕获下面引用的文本进行解析。 I've tried setting it to a variable as well as redirecting all streams with *>&1 .
我试过将它设置为一个变量以及使用 *>&1 重定向所有流。 How can I grab the output text?
如何获取输出文本? The *>&1 works for errors, but not for the text below for some reason?
*>&1 适用于错误,但出于某种原因不适用于下面的文本? Writing to a file is not an option.
写入文件不是一种选择。
Connect-Mailbox -Identity $guidT -user $targetDNT -Alias $aliasT -Database $databaseT -DomainController $domainSpecificDCsSourceAccountT[0]
I've also tried我也试过
$outValue = Connect-Mailbox -Identity $guidT -user $targetDNT -Alias $aliasT -Database $databaseT -DomainController $domainSpecificDCsSourceAccountT[0] *>&1
WARNING: The operation completed successfully but the change will not become effective until Active Directory replication occurs.
警告:操作已成功完成,但在 Active Directory 复制发生之前,更改不会生效。
[edit] For repro, this command appears to fall into the same situation, and is easier to setup/configure [编辑] 对于 repro,此命令似乎属于相同的情况,并且更易于设置/配置
$var = Set-Mailbox $sourceUserT -LitigationHoldEnabled $false
[edit2] [编辑2]
Some commands like set-mailbox will work if you change the type from a string to an array such as but connect-mailbox is not able to use that?如果将类型从字符串更改为数组,诸如 set-mailbox 之类的命令将起作用,但 connect-mailbox 无法使用它?
So this was kind of dumb luck, it works for the set-mailbox command but not the connect-mailbox command?所以这是一种愚蠢的运气,它适用于 set-mailbox 命令但不适用于 connect-mailbox 命令?
PS> Set-Mailbox "first.last" -LitigationHoldEnabled $false -WarningVariable wv
WARNING: The command completed successfully but no settings of 'xxx/last, first' have been modified.
PS> $wv
PS>
However when it did it this way然而,当它这样做时
[System.Collections.ArrayList]$wvar = @();
Set-Mailbox "onprem.detailee1" -LitigationHoldEnabled $false -WarningVariable +wvar
WARNING: The command completed successfully but no settings of 'xxxx/last, first' have been modified.
PS> write-host $wvar
The command completed successfully but no settings of 'xxxx/last, first' have been modified.
So Powershell cannot cast some outputs (warning, error, etc) to a string, however they can add the object to an array.因此 Powershell 无法将某些输出(警告、错误等)转换为字符串,但是它们可以将对象添加到数组中。 Strangely enough the non-typing portion of the Powershell language is not applicable here.
奇怪的是,Powershell 语言的非打字部分在这里并不适用。
You should be able to use the -WarningVariable
Common Parameter to capture messages destined for the warning stream.您应该能够使用
-WarningVariable
通用参数来捕获发往警告流的消息。
Connect-Mailbox -Identity $guidT -user $targetDNT -Alias $aliasT -Database $databaseT -DomainController $domainSpecificDCsSourceAccountT[0] -WarningVariable WarningVar
$WarningVar
The common parameters include variable options for messages sent to the error, information, and warning streams.通用参数包括发送到错误、信息和警告流的消息的变量选项。
One way I found to do this was to set erroraction explicitly to stop ( -ea stop
) for the cmdlet in a try catch statement, then I could get the exception value with $_.Exception.Message
in the catch statement as it forces the cmdlet to have a terminating error.我发现这样做的一种方法是在 try catch 语句中为 cmdlet 显式设置 erroraction 以停止(
-ea stop
),然后我可以在 catch 语句中使用$_.Exception.Message
获取异常值,因为它强制执行cmdlet 出现终止错误。
This is something I've found to be frustrating coming from c# where a try/catch statement works without having to explicitly define an error as terminating or not.这是我发现来自 c# 令人沮丧的事情,其中 try/catch 语句可以工作,而无需明确定义错误是否终止。 If you do not do that for some cmdlets, the try/catch will not work and it will never catch the terminating error.
如果您不对某些 cmdlet 执行此操作,则 try/catch 将不起作用,并且永远不会捕获终止错误。
Hopefully this helps others, more info希望这对其他人有帮助,更多信息
https://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell https://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell
The problem most likely stems from buggy behavior with respect to streams from implicitly remoting commands , such as created via Import-PSSession
;问题很可能源于隐式远程命令的流方面的错误行为,例如通过
Import-PSSession
创建; this GitHub issue is presumably related.这个GitHub 问题大概是相关的。
You have already found one workaround, as demonstrated in your question: if you explicitly create the target variable to pass to -WarningVariable
as a System.Collections.ArrayList
instance and pass the variable name prefixed with +
( -WarningVariable +var
) - normally intended for appending to a preexisting variable value - capturing the warning(s) is possible.您已经找到了一种解决方法,如您的问题所示:如果您明确创建目标变量以作为
System.Collections.ArrayList
实例传递给-WarningVariable
并传递以+
( -WarningVariable +var
) 为前缀的变量名称 - 通常预期附加到预先存在的变量值 - 捕获警告是可能的。
A slightly simpler workaround is to call the command via Invoke-Command
and apply -WarningVariable
to the latter :一个稍微简单的解决方法是通过
Invoke-Command
并将-WarningVariable
应用于后者:
Invoke-Command -WarningVariable warnings {
Connect-Mailbox -Identity $guidT -user $targetDNT -Alias $aliasT -Database $databaseT -DomainController $domainSpecificDCsSourceAccountT[0]
}
# $warnings now contains any warnings emitted by the command.
您是否尝试过以下操作?
$var = myCommand | out-string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.