Hash Table - 按照输入的顺序排序
Hash Table - Sort in order of how they are input
我这里有一个散列 table,我最终将它输出到 Excel 电子表格,但问题似乎是系统默认对散列 table 进行排序的方式.我希望它 return 机器按照输入的顺序排列,它们目前的工作方式是弹出一个框,然后粘贴所有机器名称,这样它们就都在 [=12 之前的内存中了=] 循环。我之前按最长的正常运行时间对此进行排序,但现在需要与输入它们的方式相同。我最初的想法是创建另一个散列 table 并以与 $machineList
变量相同的顺序捕获它们,但这甚至可能让我处于相同的位置。我尝试搜索,但找不到有关散列 tables 排序的默认方式的信息。
有什么想法吗?
$machineUptime = @{}
foreach($machine in $machineList){
if(Test-Connection $machine -Count 1 -Quiet){
try{
$logonUser = #gets the logged on user
$systemUptime = #gets the wmi property for uptime
if($logonUser -eq $null){
$logonUser = "No Users Logged on"
}
$machineUptime[$machine] = "$systemUptime - $logonUser"
}
catch{
Write-Error $_
$machineUptime[$machine] = "Error retrieving uptime"
}
}
else{
$machineUptime[$machine] = "Offline"
}
}
创建 $machineUptime
作为有序哈希表(前提是您有 PowerShell v3 或更新版本):
$machineUptime = [ordered]@{}
我这里有一个散列 table,我最终将它输出到 Excel 电子表格,但问题似乎是系统默认对散列 table 进行排序的方式.我希望它 return 机器按照输入的顺序排列,它们目前的工作方式是弹出一个框,然后粘贴所有机器名称,这样它们就都在 [=12 之前的内存中了=] 循环。我之前按最长的正常运行时间对此进行排序,但现在需要与输入它们的方式相同。我最初的想法是创建另一个散列 table 并以与 $machineList
变量相同的顺序捕获它们,但这甚至可能让我处于相同的位置。我尝试搜索,但找不到有关散列 tables 排序的默认方式的信息。
有什么想法吗?
$machineUptime = @{}
foreach($machine in $machineList){
if(Test-Connection $machine -Count 1 -Quiet){
try{
$logonUser = #gets the logged on user
$systemUptime = #gets the wmi property for uptime
if($logonUser -eq $null){
$logonUser = "No Users Logged on"
}
$machineUptime[$machine] = "$systemUptime - $logonUser"
}
catch{
Write-Error $_
$machineUptime[$machine] = "Error retrieving uptime"
}
}
else{
$machineUptime[$machine] = "Offline"
}
}
创建 $machineUptime
作为有序哈希表(前提是您有 PowerShell v3 或更新版本):
$machineUptime = [ordered]@{}