PoshCode Archive  Artifact Content

Artifact 9746d0179f207c500fa5c54ab7a6f49d80fd42b2637b0fd47b175e457cd6a321:

  • File get-DiskVolumeInfo.ps1 — part of check-in [2614a3e0a7] at 2018-06-10 13:18:02 on branch trunk — This function returns information about disk volumes, works on Win2K, WinXP, Win2003, Win2008, WinVista, Win7. Can be used in a pipeline with -Raw switch or stand-alone. (user: Richard Vantreas size: 3665)

# encoding: ascii
# api: powershell
# title: get-DiskVolumeInfo
# description: This function returns information about disk volumes, works on Win2K, WinXP, Win2003, Win2008, WinVista, Win7.  Can be used in a pipeline with -Raw switch or stand-alone.
# version: 5.2
# type: function
# author: Richard Vantreas
# license: CC0
# function: get-DiskVolumeInfo
# x-poshcode-id: 2977
# x-archived: 2012-01-14T07:03:25
# x-published: 2012-09-28T11:11:00
#
# v1.0 Initial Build  Richard Vantrease
# v1.1 Bug Fix, replaced write-log (a function I use internally) with write-verbose a function natively available – Richard Vantrease
#
function get-DiskVolumeInfo
{
    <#
        .SYNOPSIS
        Returns information about disk volumes including freespace
        
        .DESCRIPTION
        Returns information about disk volumes including freespace
        
        .EXAMPLE
        show-InnerException ExceptionObject
        Shows the inner exception object of the error object that is passed to the
        function.
        
        .Notes
    	ChangeLog    :
    	Date	    Initials	Short Description
        02/18/2009  RLV         New
 
        .Link
        http://msdn.microsoft.com/en-us/library/aa394239(v=VS.85).aspx
        
        .Link
        http://msdn.microsoft.com/en-us/library/aa394515(VS.85).aspx
        
        .Link
        http://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx
    #>

    [CmdletBinding()]
    param
        (
            [Parameter(Mandatory=$true)][string]$ComputerName,
            [Parameter(Mandatory=$false)][switch]$Raw
        )
        
    trap 
    {
        show-InnerException -ex $_
        continue
    }

    write-verbose "$($MyInvocation.InvocationName) - Begin function"
    foreach($Key in $PSBoundParameters.Keys){ write-verbose "$($MyInvocation.InvocationName) PARAM: -$Key - $($PSBoundParameters[$Key])" }

    # Create an empty array to hold the property bags that will be created later
    $VolArray = @()
    
    # Windows 2003 supports mount points
    if($(gwmi win32_operatingSystem -comp $ComputerName).version -ge '5.2')
    {
        # #region Persistent fold region
        $VolArray = gwmi win32_volume -Computer $ComputerName | Select-Object `
            @{Name='Computer';Expression={$ComputerName}}, `
            @{Name='VolumeName';Expression={if($_.Name -like "\\?\Volume*"){'\\?\Volume'}else{$_.Name}}}, `
            @{Name='Capacity_GB';Expression={[math]::Round($_.Capacity/1GB)}}, `
            @{Name='FreeSpace_GB';Expression={[math]::Round($_.FreeSpace/1GB)}}, `
            @{Name='Pct_Free';Expression={if($_.Capacity -gt 0){[math]::Round(($_.FreeSpace/$_.Capacity)*100)}else{0}}}, `
            @{Name='BlockSize_KB';Expression={[math]::Round($_.Blocksize/1KB)}}
        # #endregion
    }
    else
    # Windows 2000 and Windows XP no mount point support
    {
        # #region Persistent fold region
        $VolArray = gwmi win32_LogicalDisk -Computer $ComputerName | Select-Object `
            @{Name='Computer';Expression={$ComputerName}}, `
            @{Name='VolumeName';Expression={$_.Name}}, `
            @{Name='Capacity_GB';Expression={[math]::Round($_.Size/1GB)}}, `
            @{Name='FreeSpace_GB';Expression={[math]::Round($_.FreeSpace/1GB)}}, `
            @{Name='Pct_Free';Expression={if($_.Size -gt 0){[math]::Round(($_.FreeSpace/$_.Size)*100)}else{0}}}
        # #endregion
   }
    
    if($Raw){  $VolArray   }
    else{   $VolArray | ft -auto    }
    
    write-verbose "$($MyInvocation.InvocationName) - End function"
}