![](/img/trans.png)
[英]What is the right way to capture the output of Format-Table (to show it on the host, rather than the stdout)?
[英]How to capture output in a variable rather than a logfile?
这会将输出写入日志文件:
& $Env:WinDir\system32\inetsrv\appcmd.exe >test.log
但是,如果我想将输出保留在字符串变量中以在电子邮件正文中使用该怎么办?
我没有运气就尝试了..
$test = ""
& $Env:WinDir\system32\inetsrv\appcmd.exe >$test
Write-Host $test
你所要做的:
$test = & $Env:WinDir\system32\inetsrv\appcmd.exe
如果您还想重定向错误,请在末尾添加2>&1
。
捕获可执行文件的输出非常简单,
$cmdOutput = &"Application.exe" 2>&1
2>&1-在输出中包括错误流
PowerShell中可执行文件的返回类型是一个字符串数组 。 如果记录此类输出,
Write-Host $cmdOutput
将数组中的字符串输出到以空格分隔的输出流
要以每行的字符串形式打印它们,请选择
Write-Output $cmdOutput
要么
$cmdOutput = &"Application.exe" | Out-String
Write-Host $cmdOutput
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.