【问题标题】:PSSession and Get-HotfixPSSession 和 Get-Hotfix
【发布时间】:2014-03-06 18:56:56
【问题描述】:

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

$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;
     }
}

【问题讨论】:

  • 您是否在这些计算机上检查过 c:\new?我怀疑您的 PSSession 中的 CSV 位于远程计算机上,而不是您自己的计算机上。
  • 是的,它不会将它发布到它正在寻找的计算机上,而是将它放在运行脚本的本地计算机上。但是,我确实在获得所有人许可的​​情况下创建了一条 UNC 路径,结果如下:Access to the path '\\server name\d$\Patches\sample.csv' is denied. + CategoryInfo : OpenError: (:) [Export-Csv], UnauthorizedAccessException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
  • 您是否尝试过仅使用 WMI? Get-WMIObject Win32_QuickFixEngineering -ComputerName $name.
  • @EBGreen - 不,我没有。虽然,我尽量远离我们的 WMI 查询,因为当我们查询 WMI 时某些系统会崩溃。我正在尝试了解网络及其所有 Windows 计算机。想一想,我喜欢它!
  • 明确一点,Get-Hotfix 无论如何都使用 WMI。

标签: powershell


【解决方案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"
  }
}

【讨论】:

  • 效果很好!我在想我可以使用 pssession 来完成这项工作。你真棒,摇滚!
  • 或者完全跳过 Invoke-Command,Get-HotFix 有一个 ComputerName 参数。 Get-HotFix -ComputerName $name 应该可以正常工作。
猜你喜欢
  • 1970-01-01
  • 2018-03-27
  • 2015-11-03
  • 1970-01-01
  • 2015-11-11
  • 2016-09-09
  • 2019-07-31
  • 2023-04-06
  • 2019-10-26
相关资源
最近更新 更多