哈希表 - 按输入方式排序

nka*_*sco 1 powershell hashtable

我在这里有一个哈希表,我最终输出到Excel电子表格,但问题似乎是系统默认情况下对哈希表进行排序的方式.我希望它以与它们输入相同的顺序返回机器,它们当前工作的方式是弹出一个框并粘贴所有机器名称,以便它们在foreach循环之前都在内存中.我之前通过最长的正常运行时间对它进行排序,但它现在需要与它们输入的方式相同.我最初的想法是创建另一个哈希表并以与$machineList变量相同的顺序捕获它们,但这甚至可能使我处于相同的位置.我试图搜索,但我找不到哈希表排序的默认方式的信息.

有任何想法吗?

$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"
    }
}
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 5

创建$machineUptime为有序哈希表(假设您有PowerShell v3或更新版本):

$machineUptime = [ordered]@{}
Run Code Online (Sandbox Code Playgroud)