多个Get-WmiObject调用的单个连接

Ste*_*ven 3 powershell wmi get-wmiobject

以下脚本从我提供的每台计算机成功获取制造商,型号,序列号和操作系统hostnames.txt.但是,它很慢,因为它必须连接到每台计算机上的WMI三次.

$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
$BIOS = Get-WmiObject Win32_Bios -ComputerName $Computer
Run Code Online (Sandbox Code Playgroud)

使用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
Run Code Online (Sandbox Code Playgroud)

Nas*_*Nas 6

您可以使用CIM(具有会话选项),更多关于CIM与WMI("WMI是Windows平台的CIM的Microsoft实现")

$CIMSession = New-CimSession -ComputerName $RemoteComputer

Get-CimInstance win32_OperatingSystem -CimSession $CIMSession -Property Caption
Get-CimInstance Win32_ComputerSystem -CimSession $CIMSession -Property Manufacturer,Model
Get-CimInstance Win32_Bios -CimSession $CIMSession -Property SerialNumber
Run Code Online (Sandbox Code Playgroud)