为什么此代码的输出中的LicenseStatusText列为空?

$statusLookup=@{0='Unlicensed'; 1='Licensed'; 2='OOBGrace'; 3='OOTGrace'; 4='NonGenuineGrace'; 5='Notification'; 6='ExtendedGrace'}
Get-CimInstance SoftwareLicensingProduct -Filter "Name like 'Windows%'" | where { $_.PartialProductKey } |
     select Description, LicenseStatus, @{N='LicenseStatusText';E={$statusLookup[$_.LicenseStatus]}}

我也曾尝试将hashtable-keys包装在'

当我使用E={$_.LicenseStatus} LicenseStatusText输出时为 1

最佳答案

未强制转换或未引用的整数(在-2 ^ 32到2 ^ 32范围内)被强制为Int32值类型。创建哈希表条目@{0='something'}(其中0Int32)时,会发生这种情况。由于LicenseStatus属性提供的值为Uint32,因此您的密钥类型不匹配。您需要通过强制在哈希表或计算的属性中强制发布问题。

# Casting in calculated property
$statusLookup=@{0='Unlicensed'; 1='Licensed'; 2='OOBGrace'; 3='OOTGrace'; 4='NonGenuineGrace'; 5='Notification'; 6='ExtendedGrace'}
Get-CimInstance SoftwareLicensingProduct -Filter "Name like 'Windows%'" | where { $_.PartialProductKey } |
     select Description, LicenseStatus, @{N='LicenseStatusText';E={$statusLookup[[int]$_.LicenseStatus]}}

# Casting in hash table
$statusLookup=@{[uint32]0='Unlicensed'; [uint32]1='Licensed'; [uint32]2='OOBGrace'; [uint32]3='OOTGrace'; [uint32]4='NonGenuineGrace'; [uint32]5='Notification'; [uint32]6='ExtendedGrace'}
Get-CimInstance SoftwareLicensingProduct -Filter "Name like 'Windows%'" | where { $_.PartialProductKey } |
     select Description, LicenseStatus, @{N='LicenseStatusText';E={$statusLookup[$_.LicenseStatus]}}

关于powershell - 为什么我的计算列为空(它使用哈希表查找),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61117765/

10-13 09:18