You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PowerShell命令输出转换为Hashtable并与哈希表变量进行对比?

Convert PowerShell Command Output to Hashtable for Comparison

The core issue here is that ConvertFrom-StringData isn't designed to handle PowerShell objects (like CIM instances or Get-WebConfigurationProperty results)—it works with key=value formatted string content, which is why you got that unexpected single-key output. Let's fix this with a straightforward approach to convert any PowerShell object to a hashtable, then handle the comparison and update logic.

Step 1: Convert PowerShell Objects to Hashtable

To map an object's property names as hashtable keys and property values as hashtable values, iterate over the object's properties using PSObject.Properties:

Example with Get-CimInstance

# Get the BIOS object
$biosObject = Get-CimInstance -Class Win32_BIOS

# Initialize empty hashtable
$biosHash = @{}

# Populate the hashtable with object properties
foreach ($property in $biosObject.PSObject.Properties) {
    # Optional: Skip properties with null values to avoid clutter
    if ($null -ne $property.Value) {
        $biosHash[$property.Name] = $property.Value
    }
}

This will give you a clean hashtable matching the output you expect:

Name                           Value
----                           -----
SMBIOSBIOSVersion              1.6.0
Manufacturer                   Dell Inc.
Name                           1.6.0
SerialNumber                   BDM8P93
Version                        DELL - 20170001

For Your IIS Get-WebConfigurationProperty Use Case

The exact same logic applies—since Get-WebConfigurationProperty returns a PowerShell object, just swap out the source command:

# Get your IIS configuration property
$webConfigObject = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter 'system.webServer/security/authentication/windowsAuthentication' -Name 'enabled'

# Convert to hashtable
$webConfigHash = @{}
foreach ($prop in $webConfigObject.PSObject.Properties) {
    if ($null -ne $prop.Value) {
        $webConfigHash[$prop.Name] = $prop.Value
    }
}

Step 2: Compare Hashtables and Update Mismatches

To compare your target hashtable ($dict_object) with the converted one, you'll need to convert both to a comparable structure (like a collection of key-value pairs) before using Compare-Object:

# Define your target hashtable
$dict_object = @{Firstname="A";Lastname="B"} # Replace with your actual target values

# Convert both hashtables to key-value pair collections
$targetPairs = $dict_object.GetEnumerator() | Select-Object Key, Value
$convertedPairs = $webConfigHash.GetEnumerator() | Select-Object Key, Value

# Find differences
$mismatches = Compare-Object -ReferenceObject $targetPairs -DifferenceObject $convertedPairs -Property Key, Value

# Handle each mismatch (adjust the Add-WebConfigurationProperty logic to your needs)
foreach ($mismatch in $mismatches) {
    # Check if the difference is in the converted (actual) output
    if ($mismatch.SideIndicator -eq '=>') {
        Write-Warning "Mismatch detected: Key '$($mismatch.Key)' has value '$($mismatch.Value)' (expected '$($dict_object[$mismatch.Key])')"
        
        # Execute Add-WebConfigurationProperty with the correct values
        Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
            -Filter 'system.webServer/security/authentication/windowsAuthentication' `
            -Name $mismatch.Key `
            -Value $mismatch.Value
    }
}

Why ConvertFrom-StringData Failed

When you pipe a PowerShell object to ConvertFrom-StringData, PowerShell first converts the object to its string representation (which is that single Win32_BIOS: ... line you saw). Since this string doesn't follow the key=value format the cmdlet expects, it can't parse it into individual key-value pairs.

内容的提问来源于stack exchange,提问作者user12007109

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:12:30