2017-03-22 47 views
0

我在使用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

回答

1

每個調用,爲你注意,進行遠程調用:

$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 
+0

我知道必須有一種方法來做到這一點。像魅力一樣工作!謝謝 –

+0

最後兩段代碼中的小錯誤,'Version'是'Win32_OperatingSystem'的屬性,而不是'Win32_ComputerSystem'。還有,展示如何最大限度地減少'Get-WmiObject'調用次數的+1,甚至更好的是隻提取您需要的屬性。另一種方法是使用'-Property'參數並讓cmdlet爲您生成查詢:'$ c = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ ComputerName - 屬性名稱,模型。 – BACON

相關問題