Networking/Get-DiskSpace.ps1

<#
    .NOTES
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.165
     Created on: 8/8/2019 22:04
     Created by: jisodl0
     Organization: J.B. Hunt
     Filename: Get-DiskSpace.ps1
    ===========================================================================
    .DESCRIPTION
        A utility script for retrieving disk space info of remote servers within
  J. B. Hunt.
#>


<#
.SYNOPSIS
  Gets disk space data for a remote PC.
 
.DESCRIPTION
  Gets the disk space info for a remote PC. If specified, will return data on a specific drive, or the C: drive by default.
 
.PARAMETER ComputerName
  The name of the computer to query.
 
.PARAMETER DriveLetter
  The drive letter to retrieve capacity info for.
 
.EXAMPLE
  PS C:\> Get-DiskSpace -ComputerName 'JVPWEB18107'
#>

function Get-DiskSpace {
  [CmdletBinding()]
  param (
    [Parameter(Position = 0,
               HelpMessage = 'Which server would you like to query?')]
    [Alias('Computer', 'Server')]
    [System.String]
    $ComputerName,
    
    [Parameter(Position = 1,
               HelpMessage = 'What is the name/letter of the drive to analyze?')]
    [Alias('Drive')]
    [System.String]
    $DriveName = 'C'
  )
  
  Begin {
    if (!(Test-Connection -ComputerName $ComputerName -Count 1)) {
      Write-Error "$ComputerName does not appear to be online. Please try again later."
    } else {
      Write-Verbose "Getting disk space data for drive $DriveName on $ComputerName..."
    }
    
    $FreeGB = @{
      Name       = 'Free Space';
      Expression = { "$([Math]::Round($_.Free / 1GB, 2))GB ($([Math]::Round($_.Free / ($_.Used + $_.Free), 2) * 100)%)" };
    }
    
    $UsedGB = @{
      Name       = 'Used Space';
      Expression = { "$([Math]::Round($_.Used / 1GB, 2))GB ($([Math]::Round($_.Used / ($_.Used + $_.Free), 2) * 100)%)" };
    }
    
    $TotalGB = @{
      Name       = 'Total Space';
      Expression = { "$([Math]::Round(($_.Used + $_.Free) / 1GB, 2))GB" };
    }
  }
  
  Process {
    Invoke-Command -ComputerName $ComputerName `
                   -ArgumentList $DriveName `
                   -ScriptBlock { param ($DriveName) Get-PSDrive -Name $DriveName } `
    | Select-Object $FreeGB, $UsedGB, $TotalGB
  }
}