Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Get-DiskInformation
- {
- <#
- .SYNOPSIS
- Tool to get disk information from a computer or series of computers
- .DESCRIPTION
- Gives a quick way to query multiple computers at once for disk statistics.
- This requires the user to input a domain admin credential to work correctly across all servers.
- Attributes brought back include (when used with -ShowAll switch)
- Computer
- Drive
- CapacityGB
- CapacityMB
- CapacityRaw
- UsedGB
- UsedMB
- UsedRaw
- FreeGB
- FreeMB
- FreeRaw
- VolumeName
- .PARAMETER Computername
- List of computers you want disk information for
- .EXAMPLE
- Get-DiskInformation -Computername Server01
- Get disk information for server01, returning default attributes
- .EXAMPLE
- Get-DiskInformation -computername SERVER01, SERVER02 -ShowAll -Verbose
- Get disk information for Server01 and Server02 and show all attributes as well as all verbose messages.
- .EXAMPLE
- Get-DiskInformation -Computername ( Get-Content \\NCCAR-SERVER01\c$\Disk Space\Servers.txt") | ? { $_.FreePercent -lt 0.1 }
- Get disk information for all servers listed in the file "Servers.txt" where the free percent is less than 10 (See notes above)
- .INPUTS
- Accepts single or multiple server names
- .OUTPUTS
- Produces a list of disk objects including the attributes detailed above
- .NOTES
- Freepercent and UsedPercent are represented as a value between 0 and 1, this is due to an issue with the percent format
- where it cannot sort correctly as the value is a string.
- #>
- [CmdletBinding()]
- Param
- (
- [Parameter(Mandatory=$True,
- ValueFromPipeline=$True,
- Position=0)]
- [String[]]$ComputerName,
- [Switch]$ShowAll
- )
- Begin
- {
- Write-Verbose "Removing all existing cim sessions"
- Get-CimSession | Remove-CimSession
- Write-Verbose "Authenticating..."
- $Creds = Get-Credential | Out-Null
- $AltComputername = $null
- }
- Process
- {
- Write-Verbose "Creating new cim sessions on all computers"
- $AllCimSessions = New-CimSession -Credential $Creds -ComputerName $ComputerName -SessionOption (New-CimSessionOption -Protocol Dcom) -ErrorAction SilentlyContinue -ErrorVariable Err
- [array]$AltComputerName = $Err.OriginInfo.PSComputerName
- Foreach ( $Computer in $AltComputername )
- {
- Try
- {
- $AllCimSessions += New-Cimsession -ComputerName $Computer -erroraction Stop -Credential $Creds
- }
- Catch
- {
- Write-Output "Unable to create a connection to $Computer"
- $error[0] | Select * | Out-File "C:\temp\DiskInformation LogFile.log" -append
- }
- }
- Write-Verbose "Enumerating all disks on all servers"
- $Disks = Get-CimInstance Win32_LogicalDisk -filter "DriveType = 3" -CimSession $AllCimSessions
- Write-Verbose "Processing disk information"
- Foreach ( $Disk in $Disks )
- {
- [array]$DiskResults += New-Object PSObject -Property @{
- Computer = $Disk.PSComputerName
- Drive = $Disk.DeviceID
- CapacityGB = "{0:N2}" -f ($Disk.Size / 1gb)
- CapacityMB = "{0:N2}" -f ($Disk.Size / 1mb)
- CapacityRaw = $Disk.Size
- UsedGB = "{0:N2}" -f ( ($Disk.Size - $Disk.FreeSpace) / 1gb )
- UsedMB = "{0:N2}" -f ( ($Disk.Size - $Disk.FreeSpace) / 1mb )
- UsedPercent = "{0:N2}" -f ( ( $Disk.Size - $Disk.FreeSpace ) / $Disk.Size)
- UsedRaw = ($Disk.Size - $Disk.FreeSpace)
- FreeGB = "{0:N2}" -f ($Disk.FreeSpace / 1gb)
- FreeMB = "{0:N2}" -f ($Disk.FreeSpace / 1mb)
- FreePercent = "{0:N2}" -f ($Disk.FreeSpace / $Disk.Size )
- FreeRaw = $Disk.FreeSpace
- VolumeName = $Disk.VolumeName
- }
- }
- }
- End
- {
- if( $ShowAll )
- {
- $DiskResults | Select * | Sort Computer
- }
- Else
- {
- $DiskResults | Select Computer, Drive, FreePercent, CapacityGB, UsedGB, FreeGB | Sort Computer
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment