【发布时间】:2018-10-26 19:50:03
【问题描述】:
下面的脚本成功获取了我在hostnames.txt中提供的每台电脑的制造商、型号、序列号和操作系统。但是,它很慢,因为它必须连接到每台计算机上的 WMI 三次。
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
$BIOS = Get-WmiObject Win32_Bios -ComputerName $Computer
如何使用 PowerShell 连接到远程计算机的 WMI 一次并使用同一连接执行三个查询?
$Array = @() ## Create Array to hold the Data
$Computers = Get-Content -Path .\hostnames.txt
foreach ($Computer in $Computers)
{
$Result = "" | Select HostPS,Mfg,Model,Serial,OS
$Result.HostPS = $Computer
$ErrorActionPreference = "SilentlyContinue" ## Don't output errors for offline computers
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
$BIOS = Get-WmiObject Win32_Bios -ComputerName $Computer
$ErrorActionPreference = "Continue"
$Result.Mfg = $CS.Manufacturer
$Result.Model = $CS.Model
$Result.Serial = $BIOS.SerialNumber
$Result.OS = $OS.Caption
$Array += $Result ## Add the data to the array
}
$Array | Export-Csv file.csv -NoTypeInformation
【问题讨论】:
-
我不会回答您的具体问题,但如果您只在使用
Get-WMIObject时选择您需要的内容,它应该会有所帮助。例如,$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer | Select-Object Caption
标签: powershell wmi get-wmiobject