简体   繁体   English

PowerShell哈希表顺序

[英]PowerShell hash table order

The below script is running in a for-each loop and the output heading is not coming in the right order. 下面的脚本在for-each循环中运行,并且输出标题的顺序不正确。 I want the output order as I mentioned in the hash table. 我想要我在哈希表中提到的输出顺序。 I want the CSV table column heading in right order , not the data inside the table. 我希望CSV表格的列标题顺序正确 ,而不是表格内的数据。

Like 1.Machine_Name, 2.Ping_Status, 3.OS_Name 像1.Machine_Name,2.Ping_Status,3.OS_Name

How can I solve this? 我该如何解决?

$Result = @{
            MACHINE_NAME     = "$_"
            PING_STATUS      = "MACHINE ONLINE"
            OS_NAME = "$OSNAME"
        }

$Details += New-Object PSObject -Property $Result
$Details | export-csv -Path $pathofcsv -NoTypeInformation

Use an ordered hash table: 使用有序哈希表:

$Result = [ordered]@{             
            MACHINE_NAME     = "$_" 
            PING_STATUS      = "MACHINE ONLINE"
            OS_NAME = "$OSNAME"
        }

$Details += New-Object PSObject -Property $Result
$Details | export-csv -Path $pathofcsv -NoTypeInformation

要强制执行顺序,请使用Select-Object

$Details | Select-Object MACHINE_NAME, PING_STATUS, OS_NAME | export-csv -Path $pathofcsv -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM