下午好,
我有以下脚本来生成一个表作为我的html报告的一部分:
(import-csv "C:\AutoTasks\server.txt" |
% {new-object psobject -property @{
"Asset Number"=$_.Computer;
"Region"=$_.Description;
"Online Status"=(test-connection -computername $_.Computer -quiet -count 1);
"Online Since"= Try {(([Management.ManagementDateTimeConverter]::ToDateTime((gwmi Win32_OperatingSystem -ComputerName $_.Computer -ErrorAction Stop).LastBootUpTime)))} Catch {"Offline"} ;
"Service"=((Get-Service -ComputerName $_.Computer | Where-Object {$_.DisplayName -eq "ServiceName"}).Status);
"Disk Size (GB)" = ([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).size)/1Gb,2));
"Free Disk Space (GB)" = ([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).freespace)/1Gb,2));
"Free Disk Space %" = ([Math]::Round(100*(([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).freespace)/1Gb,2)) / ([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).size)/1Gb,2))),2))
}} | ConvertTo-HTML -as Table -Fragment -PreContent "<h2>Backup Machines</h2>" | Out-String)
它完成了我需要的一切。
但是,我的问题是列排序。它没有按照我列出的列顺序显示,并且它提供的订单背后似乎没有逻辑。
我如何指定订单的任何想法,我错过了某处的格式表语句?我的搜索空白。
答案 0 :(得分:1)
在PowerShell 3.0及更高版本中,您可以使用散列表文字中的[ordered]
属性来表示您想要一个有序字典:
(import-csv "C:\AutoTasks\server.txt" |
% {new-object psobject -property $([ordered]@{
"Asset Number"=$_.Computer;
"Region"=$_.Description;
"Online Status"=(test-connection -computername $_.Computer -quiet -count 1);
"Online Since"= Try {(([Management.ManagementDateTimeConverter]::ToDateTime((gwmi Win32_OperatingSystem -ComputerName $_.Computer -ErrorAction Stop).LastBootUpTime)))} Catch {"Offline"} ;
"Service"=((Get-Service -ComputerName $_.Computer | Where-Object {$_.DisplayName -eq "ServiceName"}).Status);
"Disk Size (GB)" = ([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).size)/1Gb,2));
"Free Disk Space (GB)" = ([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).freespace)/1Gb,2));
"Free Disk Space %" = ([Math]::Round(100*(([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).freespace)/1Gb,2)) / ([Math]::Round(((get-WmiObject win32_logicaldisk -Computername $_.Computer -Filter $_.Drive).size)/1Gb,2))),2))
})} | ConvertTo-HTML -as Table -Fragment -PreContent "<h2>Backup Machines</h2>" | Out-String)