Почему мой вычисляемый столбец пуст (используется поиск по хеш-таблице) - PullRequest
1 голос
/ 09 апреля 2020

Почему столбец 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]}}

Я также пытался обернуть ключи хеш-таблицы в '

Когда я использую E={$_.LicenseStatus} LicenseStatusText вывод 1

1 Ответ

2 голосов
/ 09 апреля 2020

Целые числа (в диапазоне от -2 ^ 32 до 2 ^ 32), которые не приведены или не заключены в кавычки, приводятся к типу значения Int32. Это происходит при создании записи таблицы ha sh @{0='something'}, где 0 равно Int32. Поскольку значение, предоставленное свойством LicenseStatus, равно Uint32, типы ключей не совпадают. Вы должны форсировать проблему либо приведением в таблицу ha sh, либо в вычисляемое свойство.

# 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]}}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...