2014-09-24 86 views
0
我得到CSV格式的磁盤的詳細信息在Windows Server

大家好我想在下面的格式細節如何使用PowerShell

Hostname Drive_0    Drive_1    Drive_2 

Name  C: 99.899227142334 d: 99.899227142334 e: 99.899227142334 

我可以用下面的腳本得到這個細節,但這個工程上的PowerShell 3.0 我如何更改爲在PowerShell 2.0上執行

$result = @() 
$obj = new-object PSobject 
$server = hostname 
$obj |Add-Member -MemberType NoteProperty -Name "Hostname" -Value $server -ErrorAction SilentlyContinue 
$z = Get-WmiObject -Class win32_Logicaldisk -Filter 'DriveType=3' | Select-Object -Property DeviceID, @{LABEL='TotalSize';EXPRESSION={$_.Size/1GB}} 

$z3 = $z.DeviceID 
$z4 = $z.TotalSize 
$i = 0 
foreach($z3 in $z3){ 
$z1 = $z.DeviceID[$i] 
$z2 = $z.TotalSize[$i] 
    $zx = "$z1" + ": $z2" 

     $obj | Add-Member -MemberType NoteProperty -Name "Drive_$i" -Value $zx -ErrorAction SilentlyContinue 

$i++ 
    } 
    $result+=$obj 
    $result | Export-Csv "$env:userprofile\Desktop\Result.csv" -NoTypeInformation 
+0

在PowerShell 2.0上運行時會出現什麼錯誤? – Richard 2014-09-24 11:18:57

+0

您是否偶然在運行2.0的計算機上只有一個磁盤出現此錯誤? '無法索引到System.Double類型的對象' – Matt 2014-09-24 12:19:31

+0

完美馬特..我得到同樣的錯誤 – Chand 2014-09-24 12:29:07

回答

0

您可以將您的代碼更改爲下面的代碼。這是一個有點整潔,整理了您的循環和限制,所以你並不需要對它們進行管理

$result = @() 
$obj = new-object PSobject 
$server = hostname 
$obj |Add-Member -MemberType NoteProperty -Name "Hostname" -Value $server -ErrorAction SilentlyContinue 
$z = Get-WmiObject -Class win32_Logicaldisk -Filter 'DriveType=3' | Select-Object -Property DeviceID, @{LABEL='TotalSize';EXPRESSION={$_.Size/1GB}} 

$i = 0 
$z | % { 
    $z1 = $_.DeviceID 
    $z2 = $_.TotalSize 
    $zx = "$z1" + ": $z2" 
    $obj | Add-Member -MemberType NoteProperty -Name "Drive_$i" -Value $zx 
    $i++ 
} 
$result+=$obj 
$result | Export-Csv "$env:userprofile\Desktop\Result.csv" -NoTypeInformation 

我也去掉了-ErrorAction關閉Add-Member,你應該嘗試和處理任何作物了自己,但如果您覺得有必要,請將它添加回來。

+0

謝謝arco ...尋找此... – Chand 2014-09-24 12:45:05