PSSession和Get-Hotfix

时间:2014-03-06 18:56:57

标签: powershell

我正在尝试远程获取服务器上的所有补丁,以便我们知道什么有什么。下面是我为实现这一目的而编写的脚本。但是,当我执行脚本时,我只获得运行脚本的本地机器的补丁。我做错了什么?

$computers = Get-ADComputer -Filter * -Properties operatingsystem;
$filter = "Windows Server*"

foreach($computer in $computers)
{
     if($computer.OperatingSystem -like $filter)
     {
         $name = $computer.Name.ToString();
         write-host "Working on computer: "$name
         New-PSSession -ComputerName $name;
         Enter-PSSession -ComputerName $name | Get-HotFix | Export-Csv "c:\new\$name patches.csv";
        Exit-PSSession;
     }
}

1 个答案:

答案 0 :(得分:1)

Enter-PSSession仅可以交互使用。对于脚本,您需要使用Invoke-Command

$computers = Get-ADComputer -Filter * -Properties operatingsystem;
$filter = "Windows Server*"

foreach($computer in $computers)
{
  if($computer.OperatingSystem -like $filter)
   {
     $name = $computer.Name.ToString();
     write-host "Working on computer: "$name
     Invoke-Command -ScriptBlock {Get-HotFix} -ComputerName $name | 
       Export-Csv "c:\new\$name patches.csv"
  }
}