2017-03-21 8 views
0

こんにちは、発注表の列 - PowerShellの

私は私が持っている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) 

それは私が必要なすべてを行います。

ただし、私の問題は列の順序です。私が列挙した順序では表示されず、提供する順序の背後には論理がないようです。

どのように私は順序を指定することができますどのようなアイデア、どこかにフォーマットテーブルのステートメントが欠けている?私の検索は空白です。 PowerShellの3.0以降で

+0

あなたはPowerShellのどのバージョンを使用していますか? ( '$ PSVersionTable.PSVersion') –

+0

バージョン3.0 – PJC83

答えて

1

、あなたがあなたの代わりに注文した辞書をしたいことを示すために、ハッシュテーブルリテラルに[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) 
+0

これは完璧に動作します、情報をありがとうございます。 – PJC83