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

为何PowerShell代码输出中的LicenseStatusText列为空?

哈希表查找计算列空的原因及PowerShell代码修复方案

一、为什么哈希表查找的计算列会为空?

通常有这几个常见原因:

  • 类型不匹配:哈希表的键和你要查找的值类型不一致,PowerShell默认会严格匹配键的类型。比如哈希表键是int类型,但你传入的是uint32或者string类型的值,就会找不到对应的值。
  • 键不存在:你要查找的值不在哈希表的键集合里,比如哈希表只有0-6的键,但你传入的是7,自然返回空。
  • 语法/拼写错误:比如哈希表变量名写错了,或者属性名拼错(比如把LicenseStatus写成LicenceStatus),导致无法正确获取要查找的值。

二、针对你的PowerShell代码的具体修复

你的代码里,Get-CimInstance返回的LicenseStatus属性是UInt32类型,而你创建的哈希表$statusLookup的键是Int32类型——这就是导致LicenseStatusText列为空的核心原因!虽然表面上看数值都是1,但类型不匹配,哈希表就找不到对应的值。

修复方法(两种任选其一):

方法1:将LicenseStatus转换为Int32类型

修改计算列的表达式,把$_.LicenseStatus强制转换成和哈希表键一致的int类型:

$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]}}

方法2:创建哈希表时使用UInt32类型的键

直接把哈希表的键改成uint32类型,和LicenseStatus的类型匹配:

$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]}}

验证类型的小技巧

你可以用以下命令查看LicenseStatus的类型,确认问题:

Get-CimInstance SoftwareLicensingProduct -Filter "Name like 'Windows%'" | 
    where { $_.PartialProductKey } | 
    select -First 1 | 
    ForEach-Object { $_.LicenseStatus.GetType().FullName }

执行后会输出System.UInt32,这就验证了类型不匹配的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 10:13:13