List physical drive space

只谈情不闲聊 提交于 2019-12-20 04:56:43

问题


I have around 200 servers and I need to get the disk space & logical drive space details (free space, used space & total space).

Here is my PowerShell query.

$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 
}

It is giving me desired output but if some of serverd have more than one disk or have more logical drive than the output is stuck with that number of drives of first server. It is not giving me output in the CSV file of rest of the drives of other servers.

Here is the example about what I am saying.

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

If it seems like I am not able to explain the problem. Let me try again.

  • First server has 2 physical drives that are coming in my output file (CSV).
  • Second server also has 2 physical drives that are also in CSV file.
  • But third server has more than 2 drives and only 2 drives are showing in output.

回答1:


I would like to point that there is several places where error could occur. The purpose of this answer is to address the unknown number of headers. I would recommend that you run this in place to see what it is trying to show you before you attempt to integrate this.

# 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 ....

Since this uses the pipeline you can easily add export-CSV on the end of this.




回答2:


Export-Csv assumes that all objects in your list are uniform, i.e. that they all have the same properties, just with different values. It takes the properties of the first element to determine what to export. Either make sure that all objects have the same properties, or use a different output method, for instance putting all disk information in an array per host and write that to the output file, e.g. like this:

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'
}

BTW, you don't need multiple WMI queries, since the first one already returns all disks including their sizes.




回答3:


I Have below script:
And I am looking for help to convert the output to Excel format

$Pather = Get-Content C:\Servers.txt
foreach($Path in $Pather)
{
  (Get-Acl $Path).access | ft $path,IdentityReference,FileSystemRights,AccessControlType,IsInherited -auto
}


来源:https://stackoverflow.com/questions/34633699/list-physical-drive-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!