无法在Powershell中将单个元素数组添加到哈希表

问题描述

在PS 5.1中,我编写了这个小函数,以确保Processor的结果始终是数组,但由于某些奇怪的原因,它不起作用。你知道为什么吗?

function ConvertTo-Array($result) {
   [bool]$isArray = ($result.getType() | Select-Object basetype).toString() -eq "System.Array"

   if ($isArray -eq $false) {
      return  @($result)
   }
   return $result
}

$table = @{
   "Processor" = ConvertTo-Array(Get-wmiobject Win32_Processor | Select-Object -Property Name,Number*);
}

$machinedata = @{
   "Machine Info" = $table;
}

write-output (ConvertTo-Json $machinedata -Depth 3)

如果我这样做,它将起作用

...
"Processor" = @(Get-wmiobject Win32_Processor | Select-Object -Property Name,Number*);
...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)