针对多个服务器的PowerShell命令

时间:2017-02-17 16:41:13

标签: powershell

我想要针对多个服务器运行以下脚本

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
    Select-Object DisplayName, DisplayVersion, Publisher | #InstallDate  
    Format-Table –AutoSize |
    Out-File C:\Temp\InventoryTestScripts.txt

目前,此输出保存在运行它的服务器上。我想对30个不同的服务器运行此命令,并将每个服务器的输出保存到一个文件。我还需要一种方法来指示哪个结果集来自文件中的哪个服务器。

1 个答案:

答案 0 :(得分:0)

[string[]]$servers = 'server1,server2,server3' -split ',' #put a list of servers into an array; you can get these however (typically read from a file; in this case splittling a comma separated string)
$credential = Get-Credential #this will prompt you to enter your account credentials
Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock {
    Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate 
} | Format-Table PSComputerName, DisplayName, DisplayVersion, Publisher, InstallDate