首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于多个Get-WmiObject调用的单个连接

用于多个Get-WmiObject调用的单个连接
EN

Stack Overflow用户
提问于 2018-10-26 19:50:03
回答 2查看 1.7K关注 0票数 3

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

代码语言:javascript
复制
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
$BIOS = Get-WmiObject Win32_Bios -ComputerName $Computer

使用PowerShell,如何连接到远程计算机的WMI一次,并使用相同的连接执行这三个查询?

代码语言:javascript
复制
$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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-26 20:13:55

您可以使用CIM (它有一个会话选项)、关于CIM与WMI的更多信息 ("WMI是用于Windows平台的CIM的微软实现“)

代码语言:javascript
复制
$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
票数 6
EN

Stack Overflow用户

发布于 2018-10-27 05:25:04

对我来说,通常的方法是使用Invoke-Command在每个目标系统上运行一个脚本块。如果您让它忽略错误,它将在目标系统上并行运行,并从响应程序返回您的数据。要获得非响应者,请将输入列表与结果进行比较。笑一笑

这是这个想法的演示..。

代码语言:javascript
复制
#requires -RunAsAdministrator

# fake reading in a list of computer names
#    in real life, use Get-Content or (Get-ADComputer).Name
$ComputerList = @'
Localhost
BetterNotBeThere
127.0.0.1
10.0.0.1
::1
'@.Split("`n").Trim("`r")

$IC_ScriptBlock = {
    $CIM_ComputerSystem = Get-CimInstance -ClassName CIM_ComputerSystem
    $CIM_BIOSElement = Get-CimInstance -ClassName CIM_BIOSElement
    $CIM_OperatingSystem = Get-CimInstance -ClassName CIM_OperatingSystem
    $CIM_Processor = Get-CimInstance -ClassName CIM_Processor
    $CIM_LogicalDisk = Get-CimInstance -ClassName CIM_LogicalDisk |
        Where-Object {$_.Name -eq $CIM_OperatingSystem.SystemDrive}

    [PSCustomObject]@{
        LocalComputerName = $env:COMPUTERNAME
        Manufacturer = $CIM_ComputerSystem.Manufacturer
        Model = $CIM_ComputerSystem.Model
        SerialNumber = $CIM_BIOSElement.SerialNumber
        CPU = $CIM_Processor.Name
        RAM_GB = '{0:N2}' -f ($CIM_ComputerSystem.TotalPhysicalMemory / 1GB)
        SysDrive_Capacity_GB = '{0:N2}' -f ($CIM_LogicalDisk.Size / 1GB)
        SysDrive_FreeSpace_GB ='{0:N2}' -f ($CIM_LogicalDisk.FreeSpace / 1GB)
        SysDrive_FreeSpace_Pct = '{0:N0}' -f ($CIM_LogicalDisk.FreeSpace / $CIM_LogicalDisk.Size * 100)
        OperatingSystem_Name = $CIM_OperatingSystem.Caption
        OperatingSystem_Version = $CIM_OperatingSystem.Version
        OperatingSystem_BuildNumber = $CIM_OperatingSystem.BuildNumber
        OperatingSystem_ServicePack = $CIM_OperatingSystem.ServicePackMajorVersion
        CurrentUser = $CIM_ComputerSystem.UserName
        LastBootUpTime = $CIM_OperatingSystem.LastBootUpTime
        UpTime_Days = '{0:N2}' -f ([datetime]::Now - $CIM_OperatingSystem.LastBootUpTime).Days
        }

    }

$IC_Params = @{
    ComputerName = $ComputerList
    ScriptBlock = $IC_ScriptBlock
    ErrorAction = 'SilentlyContinue'
    }
$RespondingSystems = Invoke-Command @IC_Params
$NOT_RespondingSystems = $ComputerList.Where({
    # these two variants are needed to deal with an ipv6 localhost address
    "[$_]" -notin $RespondingSystems.PSComputerName -and
    $_ -notin $RespondingSystems.PSComputerName
    })

$RespondingSystems
$NOT_RespondingSystems

$RespondingSystems列表中的一项..。

代码语言:javascript
复制
LocalComputerName           : [MySystemName]
Manufacturer                : System manufacturer
Model                       : System Product Name
SerialNumber                : System Serial Number
CPU                         : AMD Phenom(tm) II X4 945 Processor
RAM_GB                      : 8.00
SysDrive_Capacity_GB        : 931.41
SysDrive_FreeSpace_GB       : 735.15
SysDrive_FreeSpace_Pct      : 79
OperatingSystem_Name        : Microsoft Windows 7 Professional 
OperatingSystem_Version     : 6.1.7601
OperatingSystem_BuildNumber : 7601
OperatingSystem_ServicePack : 1
CurrentUser                 : [MySystemName]\[MyUserName]
LastBootUpTime              : 2018-10-19 7:01:51 PM
UpTime_Days                 : 7.00
PSComputerName              : [::1]
RunspaceId                  : e17c2741-ba8b-4fbb-b3db-9c7fd0d84f0d

$NOT_RespondingSystems名单..。

代码语言:javascript
复制
BetterNotBeThere
10.0.0.1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53015448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档