【问题标题】:Powershell - Merge values from two variables using hashtablePowershell - 使用哈希表合并来自两个变量的值
【发布时间】:2021-06-03 09:31:11
【问题描述】:

示例代码::-

$hash = $null
$hash = @{}

1..8 | ForEach-Object {
    if (Test-Connection -ComputerName "192.168.1.$_" -Count 1 -Quiet -ErrorAction "SilentlyContinue") {
        try{
                
            $proc = Get-WmiObject -Class Win32_operatingsystem -ComputerName "192.168.1.$_" -ErrorAction Stop |
            select CSName,caption

                    foreach ($p in $proc)
                        {
                            $hash.add($p.csname,$p.caption)
                        }
            }
        catch{
            write-warning "Unable to get WMI info from '192.168.1.$_'" |
            out-file "results.txt" -append
        }
    }
    else {
        write-warning "Unable to connect to '192.168.1.$_'" |
        out-file "results.txt" -append
    }
}

$hash

结果::

Name                           Value                                                                                                                                           
----                           -----                                                                                                                                           
DC                             Microsoft Windows Server 2016 Standard                                                                                                          
SCCMCB                         Microsoft Windows Server 2016 Standard                                                                                                          
WIN1020H2                      Microsoft Windows 10 Enterprise Evaluation    

DESIRED Result 也应包含“IP”::

Name            IP                            Value                                                                                                                                           
----                                         -----                                                                                                                                           
DC              192.168.1.3               Microsoft Windows Server 2016 Standard                                                                                                          
SCCMCB          192.168.1.5               Microsoft Windows Server 2016 Standard                                                                                                          
WIN1020H2       192.168.1.7               Microsoft Windows 10 Enterprise Evaluation 
                                                                                                  

【问题讨论】:

    标签: powershell


    【解决方案1】:

    为什么是散列而不是 PsObject?
    可以通过删除foreach($p in $proc) 来简化代码,并直接返回包含计算属性“IP”的对象

    $result = 1..8 | ForEach-Object {
        $ip = "192.168.1.$_"
        if (Test-Connection -ComputerName $ip -Count 1 -Quiet -ErrorAction "SilentlyContinue") {
            try{
                # nowadays you should be using Get-CimInstance instead of Get-WmiObject
                # see https://blog.ipswitch.com/get-ciminstance-vs-get-wmiobject-whats-the-difference
                Get-WmiObject -Class  Win32_operatingsystem -ComputerName $ip -ErrorAction Stop |
                Select-Object CSName, @{Name = 'IP'; Expression = {$ip}}, Caption
            }
            catch{
                Write-Warning "Unable to get WMI info from '$ip'"
                "Unable to get WMI info from '$ip'" | Add-Content -Path "results.txt"
            }
        }
        else {
            Write-Warning "Unable to connect to '$ip'"
            "Unable to connect to '$ip'" | Add-Content -Path "results.txt"
        }
    }
    
    # output on screen
    $result | Format-Table -AutoSize
    
    # save as structured CSV file
    $result | Export-Csv -Path 'D:\Test\ComputerInfo.csv' -NoTypeInformation
    

    附:如果您还想更改属性名称CSName,您也可以使用计算属性,例如:

    Select-Object @{Name = 'ComputerName'; Expression = {$_.CSName}}, @{Name = 'IP'; Expression = {$ip}}, Caption
    

    【讨论】:

    • 与 WMIObject 一起工作得很好。 & 如果我使用 CIMInstance,则会收到错误消息。 Get-CimInstance : WinRM 客户端无法处理请求。
    • @Varun 在这种情况下使用 Get-WmiObject -Class Win32_operatingsystem.. 将其改回为您使用旧的 WMI。
    猜你喜欢
    • 2013-06-06
    • 2017-08-15
    • 1970-01-01
    • 2023-03-21
    • 2014-05-30
    • 1970-01-01
    • 2016-09-02
    • 2012-04-10
    • 1970-01-01
    相关资源
    最近更新 更多