无法输出 PSCustomObject

Unable to outputting PSCustomObject

我输入了一个集合,然后我必须循环创建自定义对象。对象中的名称都是唯一的。我希望在以后可以使用的变量中包含 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

我不确定你为什么要将 properties 分配给 $obj,因为每次循环发生时它都会被覆盖。如果我没记错的话,您打算在循环完成后 return 完成 $properties 哈希表。

另外,Hashtable的声明也不对。您可以简单地使用@{} 来定义哈希表。如果您有兴趣获得 PSCustomObject,则可以将 HashTable 转换为 PSCustomObject。

Function Get-PSCustomObjectAsOutputType {

     $services = get-service | select -First 5
     $properties = @{}
     $X = 0
     foreach ($s in $services)
        {
            $properties += @{"Name$x" = $S.name
                            "Status$x" = $S.status
                            "Displayname$x" = $S.displayName
                           }
            $x++     
         }
        [PsCustomObject]$properties
}

$TheTypeNeedsToBePSCustomOutputType = Get-PSCustomObjectAsOutputType
$TheTypeNeedsToBePSCustomOutputType.GetType()

# Output :
IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                                                                  
True     False    PSCustomObject                           System.Object    

我不确定您打算如何检索这些,因为您只是将所有属性加在一起。我建议使用带有键的散列表并将其中的每一个作为值分配给它。

Function Get-PSCustomObjectAsOutputType {

    $services = get-service | select -First 5
    $properties = @{}
    $X = 0
    foreach ($s in $services)
    {
        # Use $x as number or use $s.Name as the key for this service.
        $properties[$x] = @{Name = $S.name
                        Status = $S.status
                        Displayname = $S.displayName
                        }
        $x++     
        }
    [PsCustomObject]$properties
}

所以你在没有赋值的情况下转换一个变量。所以空进空出。稍后当您使用 += 运算符时,您将散列 table 添加到 null。该操作生成一个数组,其中索引 0 为 null,索引 1 及之后是散列 tables.

令我有些吃惊的是,强制转换为 null 会启动变量。

测试:

[hashtable]$properties
Get-Variable properties

That returns Cannot find a variable with the name 'properties'. 不过,如果您要评论该行,该函数确实 return 一个 PSCustomObject。

也就是说,我认为真正的问题是您在循环内创建对象。因此在每次迭代时重新创建它。这似乎不是您的代码的意图。

如果我正确地解释了您的意图,您希望创建一个大对象,其中的属性为您遇到的每个服务递增命名。

试试这些小调整:

Function Get-PSCustomObjectAsOutputType
{
    $Services = Get-Service | Select -First 5
    $Properties = [Ordered]@{}

    # Ordered can only be on a literal

    For( $i = 0; $i -le $Services.Count; ++$i)
    {
        $Service = $Services[$i] # Assign a var; easier to reference
        $Properties +=
        @{
            "Name$i" = $Service.Name
            "Status$i" = $Service.Status
            "Displayname$i" = $Service.DisplayName
        }
    }

[PSCustomObject]$Properties # Another way to create the object...

} #End Function Get-PSCustomObjectAsOutputType

注意我使用了有序哈希。这应该会使您的输出更具可读性。

我还使用了 for 循环,它使您不必自己递增变量...

如果有帮助请告诉我。谢谢