如何在变量而不是日志文件中捕获输出? [英] How to capture output in a variable rather than a logfile?

查看:44
本文介绍了如何在变量而不是日志文件中捕获输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于如何在变量而不是日志文件中捕获输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆