【发布时间】:2019-01-08 06:42:23
【问题描述】:
警告:我希望在 PowerShell v2 中执行此操作(抱歉!)。
我想要一个具有数组属性的自定义对象(可能创建为自定义类型)。我知道如何使用“noteproperty”属性制作自定义对象:
$person = new-object PSObject
$person | add-member -type NoteProperty -Name First -Value "Joe"
$person | add-member -type NoteProperty -Name Last -Value "Schmoe"
$person | add-member -type NoteProperty -Name Phone -Value "555-5555"
而且我知道如何根据自定义类型制作自定义对象:
Add-Type @"
public struct PersonType {
public string First;
public string Last;
public string Phone;
}
"@
$person += New-Object PersonType -Property @{
First = "Joe";
Last = "Schmoe";
Phone = "555-5555";
}
如何创建包含数组属性的类型的自定义对象?类似于这个哈希表,但作为一个对象:
$hash = @{
First = "Joe"
Last = "Schmoe"
Pets = @("Fluffy","Spot","Stinky")
}
我很确定我可以在 PowerShell v3 中使用 [PSCustomObject]$hash 来做到这一点,但我需要包含 v2。
谢谢。
【问题讨论】:
-
New-Object PSObject -Property $hash -
有什么理由限制自己使用 v2?
标签: arrays powershell object hashtable powershell-2.0