PoshCode Archive  Artifact [b48212a2b1]

Artifact b48212a2b1b3e93bd48adbf8d936593f3a328ceb1e07ec8979659b87b31f586b:

  • File Get-MountPointData.ps1 — part of check-in [08fe85df98] at 2018-06-10 13:29:55 on branch trunk — Get information about free/used space on Windows server mount points (regular drives are also supported) using Get-MountPointData — click that link for more information. (user: Joakim Svendsen size: 3346)

# encoding: ascii
# api: powershell
# title: Get-MountPointData
# description: Get information about free/used space on Windows server mount points (regular drives are also supported) using Get-MountPointData — click that link for more information.
# version: 0.1
# type: function
# author: Joakim Svendsen
# license: CC0
# function: Get-DeviceIDFromMP
# x-poshcode-id: 3717
# x-archived: 2013-07-21T00:49:28
# x-published: 2013-10-27T11:29:00
#
#
# Convert from one device ID format to another.
function Get-DeviceIDFromMP {
    
    param([Parameter(Mandatory=$true)][string] $VolumeString,
          [Parameter(Mandatory=$true)][string] $Directory)
    
    if ($VolumeString -imatch '^\s*Win32_Volume\.DeviceID="([^"]+)"\s*$') {
        # Return it in the wanted format.
        $Matches[1] -replace '\\{2}', '\'
    }
    else {
        # Return a presumably unique hashtable key if there's no match.
        "Unknown device ID for " + $Directory
    }
    
}

function Get-MountPointData {
    
    param([Parameter(Mandatory=$true)][string[]] $ComputerName,
          #[switch] $DoNotExcludeDefaults,
          [switch] $IncludeRootDrives
          )
    
    foreach ($Computer in $ComputerName) {
        
        try {
            
            # Collect mount point device IDs and populate a hashtable with IDs as keys
            $MountPointData = @{}
            Get-WmiObject Win32_MountPoint -ComputerName $Computer | 
                Where { if ($IncludeRootDrives) { $true } else { $_.Directory -NotMatch '^\s*Win32_Directory\.Name="[a-z]:\\{2}"\s*$' } } | ForEach-Object {
                    $MountPointData.(Get-DeviceIDFromMP $_.Volume $_.Directory) = $_.Directory
            }
            
            $Volumes = Get-WmiObject Win32_Volume -ComputerName $Computer | Where {
                    if ($IncludeRootDrives) { $true } else { -not $_.DriveLetter }
                } | 
                Select-Object Label, Caption, Capacity, FreeSpace, FileSystem, DeviceID, @{n='Computer';e={$Computer}}
            
        }
        
        catch {
            
            Write-Host -Fore Red "Terminating WMI error for ${Computer} (skipping): $($Error[0])"
            continue
            
        }
        
        if (-not $Volumes) {
            Write-Host -Fore Red "No mount points found on $Computer. Skipping..."
            continue
        }
        
        $Volumes | ForEach-Object {
            
            if ($MountPointData.ContainsKey($_.DeviceID)) {
                
                if ($_.Capacity) { $PercentFree = $_.FreeSpace*100/$_.Capacity }
                else { $PercentFree = 0 }
                
                $_ | Select-Object Computer, Label, Caption, FileSystem, @{n='Size (GB)';e={$_.Capacity/1GB}},
                    @{n='Free space';e={($_.FreeSpace/1GB).ToString('N')}}, @{n='Percent free';e={$PercentFree}}
                
            }
            
        } | Sort-Object -Property 'Percent free', @{Descending=$true;e={$_.'Size (GB)'}}, Label, Caption |
            Select-Object Computer, Label, Caption, FileSystem, @{n='Size (GB)';e={$_.'Size (GB)'.ToString('N')}},
                          'Free space', @{n='Percent free';e={$_.'Percent free'.ToString('N')}}
        
    }
    
}