gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:25:17 26 4
gpt4 key购买 nike

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

$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
$BIOS = Get-WmiObject Win32_Bios -ComputerName $Computer

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

最佳答案

您可以使用 CIM(它有一个 session 选项),more on CIM vs WMI (“WMI 是 Microsoft 为 Windows 平台实现的 CIM”)

$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

关于powershell - 多个 Get-WmiObject 调用的单一连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53015448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com