这是一个简短的脚本,提供了相关行为的示例:
$foo = New-Object -TypeName PSCustomObject -Property @{
Username = "BSmith"
OU = "Finance"
Department = "Finance"
Description = "Accounts"
Office = "345 2nd St"
ServerName = "WFINANCE"
ShareName = "BSmith$"
LocalPath = "E:\Users\BSmith"
}
Write-Host $foo
输出为:@{ServerName=WFINANCE; Office=345 2nd St; Username=BSmith; LocalPath=E:\Users\BSmith; OU=Finance; Description=Accounts; ShareName=BSmith$; Department=Finance}
如您所见,输出变量的顺序与指定变量时的顺序不同。为什么这样,我怎样才能使订单保持一致?
我只有机会在一台计算机上测试它,但输出的顺序在脚本的执行之间是一致的。在声明属性时我如何订购属性也没有什么不同。我还没有能够测试并查看不同的机器是否会返回不同的订单。
答案 0 :(得分:1)
预先将属性列表定义为[ordered]
哈希表,然后从该哈希表中创建对象:
$properties = [ordered]@{
Username = "BSmith"
OU = "Finance"
Department = "Finance"
Description = "Accounts"
Office = "345 2nd St"
ServerName = "WFINANCE"
ShareName = "BSmith$"
LocalPath = "E:\Users\BSmith"
}
$foo = New-Object -TypeName PSCustomObject -Property $properties
请注意,这需要PowerShell v3或更高版本。