我输入了一个集合,然后我必须循环创建自定义对象。对象中的名称都是唯一的。我希望将 PSCustomObject 放入以后可以使用的变量中。
Function Get-PSCustomObjectAsOutputType {
$services = get-service | select -First 5
[hashtable]$properties
$X = 0
foreach ($s in $services)
{
$properties += @{"Name$x" = $S.name
"Status$x" = $S.status
"Displayname$x" = $S.displayName
}
$obj = New-Object -TypeName PSCustomObject -Property $properties
$x++
}
$Obj
}
当我将它分配给一个变量时,它似乎发生了 $null 。
$TheTypeNeedsToBePSCustomOutputType = Get-PSCustomObjectAsOutputType
$TheTypeNeedsToBePSCustomOutputType.PSobject.TypeNames
我要求 System.Object[] 是 PSCustomObject
PS> $TheTypeNeedsToBePSCustomOutputType.PSobject.TypeNames
System.Object[]
System.Array
System.Object
$TheTypeNeedsToBePSCustomOutputType.PSObject
BaseObject 中的“$null”妨碍了我。
BaseObject : {$null, @{Displayname3=Application Layer Gateway Service; Name4=AppIDSvc; Displayname2=AllJoyn Router
Service; Displayname0=Agent Activation Runtime_8af1b; Status0=Stopped; Status2=Stopped; Name3=ALG;
Displayname1=Intel® SGX AESM; Name2=AJRouter; Name1=AESMService; Status1=Running; Status3=Stopped;
Name0=AarSvc_8af1b; Status4=Stopped; Displayname4=Application Identity