Public/Get-DiskSpace.ps1
|
<#
.SYNOPSIS Get DiskSpace by target % free less than 20% on a Drive, you can set your own threshold .DESCRIPTION This script check on a server or list of servers to provide Disk Space by target % free less than 20% on a Drive, you can set your own threshold .NOTE File Name : Get-DiskSpace.ps1 Author : Srini Vemulapalli Requires : PowerShell 5 .EXAMPLE Get-DiskSpace .EXAMPLE Get-DiskSpace -ComputerName (Get-Content ("Servers.txt")) | ft -AutoSize .EXAMPLE Get-DiskSpace -ComputerName mylocalhost| ? {$_.PercentFree -lt 20}| select ComputerName, status, Drive_Letter, Freespace_GB, PercentFree #> function Get-DiskSpace { [CmdletBinding()] param ( [Parameter(Mandatory = $false, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] [Alias('HostName', 'cn', 'IPAddress')] [string[]] $ComputerName = $Env:ComputerName, [Parameter(Mandatory = $false, HelpMessage="Disk space Threshold")] [decimal]$threshold = 101 ) Write-Verbose "Script execution in Progress... Please wait" Write-Log "Script execution in Progress... Please wait" $Max = $ComputerName.Count $count = 1 $results=@() foreach ($Computer in $ComputerName) { $Computer = $Computer.trim() Write-Verbose ("Currently Processing Server: $Count " + "of " + $max + " " + $Computer) Write-Log ("Currently Processing Server: $Count " + "of " + $max + " " + $Computer) -Severity INFO Try { $Items = Get-CimInstance -ComputerName $Computer cim_logicaldisk -ErrorAction Stop |select SystemName, DriveType, VolumeName, Name, @{n='Size_Gb' ; e={"{0:n2}" -f ($_.size/1gb)}}, @{n='FreeSpace_Gb' ; e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree' ; e={"{0:n2}" -f ($_.freespace/$_.size*100)}} | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$threshold} @(foreach ($Item in $Items) { $Properties = [Ordered] @{ ComputerName = $Computer Status = "Connected" Drive_Letter = $Item.Name Volume_Name = $Item.VolumeName Size_Gb = $Item.Size_Gb FreeSpace_Gb = $Item.FreeSpace_Gb PercentFree = $Item.PercentFree } $Objoutput = New-Object -TypeName PSObject -Property $Properties $results += $Objoutput }) # end 2nd foreach } catch { $Message = $($_.Exception.Message) Write-Log "Unable to Connect to $Computer , $Message Please check" -Severity ERROR Write-Verbose "Entered Disconnected Hosts Section" $Properties = [Ordered] @{ ComputerName = $Computer Status = "Unable_to_Connect" Drive_Letter = $null Volume_Name = $null Size_Gb = $null FreeSpace_Gb = $null PercentFree = $null } $Objoutput = New-Object -TypeName PSObject -Property $Properties $resluts += $Objoutput } # Remove-CimSession -Name $session # Incrimenting count for interactive console text $count = $count + 1 } # foreach Write-Output $results Write-Verbose "Completed Processing this command" } #Function Closing |