【问题标题】:Output multiple command results to a single TXT file将多个命令结果输出到单个 TXT 文件
【发布时间】:2018-11-27 11:54:53
【问题描述】:

我对 Powershell 很陌生,并且已经制定了一些我需要运行的命令,这些命令将为我提供一个非常简单的系统状态报告形式。我将在此基础上构建更多命令,以便稍后创建更详细的报告。我有下面的代码,似乎写的是实际的文本,但是是命令的结果。我想知道是否有人知道上述方法。

$health = {"Sys Techout" 
"==========================" 
"" 
"Run at:" 
date 
"" 
"Command: systeminfo" 
"===================" 
systeminfo 
"" 
"Command: query session" 
"======================" 
query session /counter 
"" 
"Command: query user" 
"===================" 
query user 
"" 

"Command: Listening WinRM port check" 
"=================================" 
netstat -an | findstr LISTENING | findstr :598[56] 
"" }

$health | Out-File -append c:\Systech\Health2.txt -NoClobber

上面的代码理想应该做到以下几点

  1. 将结果写入位置 C:\Systech\Helath2.txt 中的文本文件。
  2. 将内容中的标题设置为Sys Techout
  3. 写下每个子标题(命令:systeminfo)和下一个 行应该是命令的结果。依此类推 添加的命令。

写完以上内容后,我只得到原始文本,文本中没有结果。任何帮助都会很棒。

哈里

【问题讨论】:

  • 单独做每件事,将结果输出到文件并追加。
  • $health | Out-File ... -> & $health | Out-File ...

标签: powershell text output multiline


【解决方案1】:

您已经非常接近解决方案了。只需将& 放在最后一行的开头即可调用$health 中的脚本块,如下所示:

& $health | Out-File -append c:\Systech\Health2.txt -NoClobber

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2016-05-06
    • 1970-01-01
    相关资源
    最近更新 更多