2016-01-06 86 views
1

我有大約200臺服務器,我需要得到磁盤空間&邏輯驅動器空間信息(可用空間,使用空間&總空間)。列出物理驅動器空間

這裏是我的PowerShell的查詢。

$infoObjects = New-Object PSObject 
foreach ($machine in $servers) { 
    $counts = Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive" -ComputerName $machine 
    $total_disk = @($counts).Count 
    $i = 0 
    $total_disk = $total_disk -1 

    for (; $i -le $total_disk; $i++) { 
    $a = $i 
    $a = Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive WHERE DeviceID='\\\\.\\PHYSICALDRIVE$i'" -ComputerName $machine 

    $b = $i 
    $b = [math]::round($a.size /1GB) 

    Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Physical_Disk $i" -Value $b 
    } 
    $infoObject | Export-Csv -Path Inventory.csv -Append -Force -NoTypeInformation 
} 

這是給我想要的輸出,但如果服務器D的一些有多個磁盤或有更多的邏輯驅動器比輸出被套牢,這個數字第一臺服務器的驅動器。它不會在其他服務器的其餘驅動器的CSV文件中輸出。

這裏是關於我在說什麼的例子。

ServerName Physical_Disk 0 Physical_Disk 1 Physical_Disk 2 Physical_Disk 3 
Server1 100 20 40 
Server2 85 
Server3 60 450 200 420 
Server4 60 
Server5 60     
Server10 55 20 40

如果看起來我無法解釋問題。讓我再嘗試一次。

  • 第一臺服務器有2個物理驅動器進入我的輸出文件(CSV)。
  • 第二臺服務器還有2個物理驅動器也在CSV文件中。
  • 但第三臺服務器有2個以上的驅動器,只有2個驅動器顯示在輸出中。
+0

你是有限的,因爲第一個服務器只有兩個磁盤。如果您希望以這種方式對它們進行分組,則需要爲最大數量的磁盤創建空屬性。這就是PowerShell將數據分組在一起以便顯示的方式。第一臺服務器沒有這些額外的磁盤。您可以人爲地爲X驅動器創建磁盤值,其中X大約爲10.大多數服務器沒有10個磁盤。 – Matt

+0

示例數據不包含所有你在你的第一句話 – Matt

+2

要求對於你的未來的自己的神智着想的細節,請使用有意義的變量名。他們不需要額外的費用。 – alroc

回答

1

我想指出,有幾個地方可以發生錯誤。這個答案的目的是解決未知數量的頭文件。我建議您在嘗試將其集成之前運行此操作以查看它試圖向您顯示的內容。

# Gather all wmi drives query at once 
$alldisksInfo = Get-WmiObject –query "SELECT * FROM Win32_DiskDrive" -ComputerName $servers -ErrorAction SilentlyContinue | Group-Object __Server 

# Figure out the maximum number of disks 
$MaximumDrives = $alldisksInfo | Measure-Object -Property Count -Maximum | Select-Object -ExpandProperty Maximum 

# Build the objects, making empty properties for the drives that dont exist for each server where need be. 
$servers | ForEach-Object{ 
    # Clean the hashtable 
    $infoObject = [ordered]@{} 

    # Populate Server 
    $infoObject.Server = $_  

    # Add other simple properties here 
    $infoObject.PhysicalMemory = (Get-WmiObject Win32_PhysicalMemory -ComputerName $infoObject.Server | Measure-Object Capacity -Sum).Sum/1gb 

    # Add the disks information from the $diskInfo Array 
    $serverDisksWMI = $alldisksInfo | Where-Object{$_.Name -eq $infoObject.Server} | Select-Object -ExpandProperty Group 

    for($diskIndex =0; $diskIndex -lt $MaximumDrives;$diskIndex++){ 
     $infoObject."PhysicalDisk$diskIndex" = [Math]::Round(($serverDisksWMI | Where-Object{($_.DeviceID -replace "^\D*") -eq $diskIndex} | Select -Expand Size)/1GB) 
    } 

    # Create the custom object now. 
    New-Object -TypeName psobject -Property $infoObject 
} # | Export-Csv .... 

由於使用管道,您可以在此結尾處輕鬆添加export-CSV

+0

@ 404我只能認爲你沒有正確複製代碼或進行了更改。我使用'$ server'的計算機名稱數組來運行此代碼,並且我沒有遇到問題。這顯然是一個哈希表的輸出,但我不會生成散列表輸出,因爲它被轉換爲一個ps對象。 – Matt

+0

我提出了另一個與此相關的問題。 http://stackoverflow.com/questions/34674122/powershell-issue-in-export-array-to-csv-file – Ironic

2

Export-Csv假定列表中的所有對象都是統一的,即它們都具有相同的屬性,只是具有不同的值。它採用第一個元素的屬性來確定要導出的內容。要麼確保所有對象具有相同的屬性,要麼使用不同的輸出方法,例如將每個主機的所有磁盤信息放在一個數組中,並將其寫入輸出文件,例如像這樣:

foreach ($machine in $servers) { 
    $disks = @($machine) 
    $disks += Get-WmiObject -Computer $machine -Class Win32_DiskDrive | 
      ForEach-Object { [Math]::Round($_.Size/1GB) } 
    $disks -join ',' | Add-Content 'C:\path\to\output.csv' 
} 

順便說一句,你並不需要多個WMI查詢,因爲第一個已返回所有磁盤,包括它們的大小。

+1

自從您在前面的選擇中展開後,$ _。Size/1GB'是不是'$ _/1GB'?或者只是刪除'選擇' – Matt

+0

的確應該。固定。 –

+0

感謝您的回答。如果有其他多個查詢,那麼我怎樣才能導出它們在CSV文件。因爲我必須顯示操作系統類型,內存大小,域名以及。 – Ironic

相關問題