List physical drive space

后端 未结 3 1056

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.

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-24 13:41

    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.

提交回复
热议问题