Powershell代码,用于在从文本文件中读取每个值后打印/中断行

时间:2014-02-08 09:55:48

标签: powershell newline patch line-breaks

我正在尝试创建一个脚本,在我们的环境中获取所有服务器的补丁安装。我正在运行的脚本在每个服务器从文本文件中读取后,无需打印任何成功消息即可连续输出。我写了一个相当基本的脚本!卫生署。

我想在我的脚本中插入一些代码,在CLI中连续打印另一台服务器之前,使用Break / Success消息打印每个服务器详细信息,或者在单独的文本文件中打印每个服务器。请找到以下代码:

$Computers = gc ServerListFile.txt

Get-hotfix -computer $Computers

请至少给我一些意见,尝试完成它。

1 个答案:

答案 0 :(得分:2)

看起来它接受$Computers变量为string而不是string[](数组)。

您需要通过循环来指定每组修补程序后面的换行符:

$computers = gc ServerListFile.txt
ForEach ($computer in $computers) {

    # You could even put the computer name at the beginning of the hotfixes
    "Hotfixes for $($computer)" | Out-File hotfixes.log -a -en ASCII

    # Get the hotfixes and output to text file
    Get-Hotfix -computer $computer | Out-File hotfixes.log -a -en ASCII

    # Add a new line after each computer's hotfixes
    "`n" | Out-File hotfixes.log -a -en ASCII
}