避免对同一台计算机进行多次 WMI 调用?

Avoid multiple WMI calls to same computer?

我正在使用 Powershell 在许多远程计算机上查询 WMI classes。

我有以下工作得很好但因为对远程计算机做任何事情都需要一些时间,我希望尽量减少它:

$file = Get-Content c:\temp\bitlocker\non-compliant-wksn1.txt
foreach ($ComputerName in $file) {
    if (Test-Connection -ComputerName $ComputerName -Quiet) {
        $Hostname = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName).Name
        $model = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName).Model
        $OS = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName).Version
        $TpmActivate = (Get-WMIObject -Namespace "root/CIMV2/Security/MicrosoftTpm" -query "SELECT * FROM Win32_TPM" -ComputerName $ComputerName).IsActivated_InitialValue
        $TpmEnabled = (Get-WMIObject -Namespace "root/CIMV2/Security/MicrosoftTpm" -query "SELECT * FROM Win32_TPM" -ComputerName $ComputerName).IsEnabled_InitialValue
        $TpmOwned = (Get-WMIObject -Namespace "root/CIMV2/Security/MicrosoftTpm" -query "SELECT * FROM Win32_TPM" -ComputerName $ComputerName).IsOwned_InitialValue
        $Encrypted = (Get-WMIObject -Namespace "root/CIMV2/Security/MicrosoftVolumeEncryption" -query "SELECT * FROM Win32_EncryptableVolume WHERE DriveLetter='C:'" -ComputerName $ComputerName).ProtectionStatus
        write-host $ComputerName "`t" $Hostname "`t" $model "`t" $OS "`t" $TpmActivate "`t" $TpmEnabled "`t" $TpmOwned "`t" "C:" "`t" $Encrypted
        }
    else {
        write-host $Computername "`t" "Offline"
    }
}

正如您从代码中看到的那样,我进行了 2 次远程调用以从 Win32_ComputerSystem 获取 2 个值,并进行了 3 次远程调用以从 Win32_TPM 获取 3 个值。是否可以接受这 5 个远程调用并以某种方式将它们减少到 2 个远程调用(每个 class 一个)returns 我需要的所有信息并将它们存储在我的变量中(希望这会加快事情了)?

TIA

正如您所注意到的,这些调用中的每一个都进行远程调用:

$Hostname = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName).Name
$model = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName).Model
$OS = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName).Version**strong text**

改为在一次调用中获取完整的 WMI 对象,然后提取所需的属性:

$c = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName
$Hostname = $c.Name
$model = $c.Model
$OS = $c.Version

更好的是,只从该对象获取所需的属性:

$c = Get-WmiObject -Query 'select Name, Model, Version from Win32_ComputerSystem' -ComputerName $ComputerName
$Hostname = $c.Name
$model = $c.Model
$OS = $c.Version