【问题标题】:Avoid multiple WMI calls to same computer?避免对同一台计算机进行多次 WMI 调用?
【发布时间】:2017-03-22 13:27:47
【问题描述】:

我正在使用 Powershell 查询许多远程计算机上的 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

【问题讨论】:

    标签: powershell wmi


    【解决方案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
    

    【讨论】:

    • 我知道必须有办法做到这一点。像魅力一样工作!谢谢
    • 最后两个 sn-ps 中的小错误,VersionWin32_OperatingSystem 的属性,而不是 Win32_ComputerSystem。尽管如此,+1 用于展示如何最小化 Get-WmiObject 调用的数量,甚至更好地提及仅检索您需要的属性。另一种方法是使用 -Property 参数并让 cmdlet 为您构建查询:$c = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName -Property Name, Model
    猜你喜欢
    • 2019-05-24
    • 2017-12-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多