我输入了一个集合,然后我必须遍历才能创建customobject。对象中的名称都是唯一的。我希望将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.PSObjectBaseObject中的"$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发布于 2020-02-26 08:43:22
我不知道为什么每次循环发生时都会重写properties,为什么还要将$obj赋值给它。如果我没记错的话,您打算在循环完成后返回完整的$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
}发布于 2020-02-26 08:51:14
所以你是在没有赋值的情况下转换一个变量。所以null in null out。稍后,当您使用+=操作符时,您会将哈希表添加到null中。该操作将产生一个数组,其中索引0为null,索引1以上为哈希表。
我有点惊讶的是,转换为null会初始化该变量。
测试:
[hashtable]$properties
Get-Variable properties这将返回Cannot find a variable with the name 'properties'.,但是,如果您对该行进行注释,该函数实际上将返回一个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循环,它使您不必自己递增变量...
如果这有帮助,请告诉我。谢谢。
https://stackoverflow.com/questions/60405036
复制相似问题