避免对同一台计算机进行多次WMI调用?
问题描述:
我在使用Powershell的许多远程计算机上查询WMI类。避免对同一台计算机进行多次WMI调用?
我有一个非常有效以下,但由于做任何远程计算机需要一定的时间,我希望尽量减少:
$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个远程调用(每个类一个),它返回我需要的所有信息并将它们存储在我的变量中(希望这会加快速度)?
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
我知道必须有一种方法来做到这一点。像魅力一样工作!谢谢 –
最后两段代码中的小错误,'Version'是'Win32_OperatingSystem'的属性,而不是'Win32_ComputerSystem'。还有,展示如何最大限度地减少'Get-WmiObject'调用次数的+1,甚至更好的是只提取您需要的属性。另一种方法是使用'-Property'参数并让cmdlet为您生成查询:'$ c = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ ComputerName - 属性名称,模型。 – BACON