如何在变量而不是日志文件中捕获输出?

问题描述:

这会将输出写入日志文件:

This would write the output to a logfile:

& $Env:WinDir\system32\inetsrv\appcmd.exe >test.log

但是如果我想将输出保存在一个字符串变量中以便在电子邮件正文中使用它呢?

But what if I wanted to keep the output in a string variable to use it in the email body?

我尝试了这个没有任何运气..

I tried this without any luck..

$test = ""
& $Env:WinDir\system32\inetsrv\appcmd.exe >$test

Write-Host $test

你要做的:

$test = & $Env:WinDir\system32\inetsrv\appcmd.exe

如果您还想重定向错误,请在最后添加 2>&1.

If you wanted to redirect error as well, add 2>&1 in the end.