Guest User

Untitled

a guest
Jan 3rd, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-DiskInformation
  2. {
  3.  
  4.  
  5. <#
  6. .SYNOPSIS
  7.    Tool to get disk information from a computer or series of computers
  8.  
  9. .DESCRIPTION
  10.    Gives a quick way to query multiple computers at once for disk statistics.
  11.    This requires the user to input a domain admin credential to work correctly across all servers.
  12.    Attributes brought back include (when used with -ShowAll switch)
  13.         Computer
  14.         Drive
  15.         CapacityGB
  16.         CapacityMB
  17.         CapacityRaw
  18.         UsedGB
  19.         UsedMB
  20.         UsedRaw
  21.         FreeGB
  22.         FreeMB
  23.         FreeRaw
  24.         VolumeName
  25. .PARAMETER Computername
  26.     List of computers you want disk information for
  27. .EXAMPLE
  28.    Get-DiskInformation -Computername Server01
  29.    Get disk information for server01, returning default attributes
  30. .EXAMPLE
  31.    Get-DiskInformation -computername SERVER01, SERVER02 -ShowAll -Verbose
  32.    Get disk information for Server01 and Server02 and show all attributes as well as all verbose messages.
  33. .EXAMPLE
  34.     Get-DiskInformation -Computername ( Get-Content \\NCCAR-SERVER01\c$\Disk Space\Servers.txt") | ? { $_.FreePercent -lt 0.1 }
  35.     Get disk information for all servers listed in the file "Servers.txt" where the free percent is less than 10 (See notes above)
  36. .INPUTS
  37.     Accepts single or multiple server names
  38. .OUTPUTS
  39.     Produces a list of disk objects including the attributes detailed above
  40. .NOTES
  41.     Freepercent and UsedPercent are represented as a value between 0 and 1, this is due to an issue with the percent format
  42.     where it cannot sort correctly as the value is a string.
  43. #>
  44.  
  45.  
  46.     [CmdletBinding()]
  47.     Param
  48.     (
  49.         [Parameter(Mandatory=$True,
  50.                    ValueFromPipeline=$True,
  51.                    Position=0)]
  52.         [String[]]$ComputerName,
  53.         [Switch]$ShowAll  
  54.     )
  55.  
  56.     Begin
  57.     {
  58.     Write-Verbose "Removing all existing cim sessions"
  59.     Get-CimSession | Remove-CimSession
  60.     Write-Verbose "Authenticating..."
  61.     $Creds = Get-Credential | Out-Null
  62.     $AltComputername = $null
  63.     }
  64.     Process
  65.     {
  66.     Write-Verbose "Creating new cim sessions on all computers"
  67.            
  68.            
  69.             $AllCimSessions = New-CimSession -Credential $Creds -ComputerName $ComputerName -SessionOption (New-CimSessionOption -Protocol Dcom) -ErrorAction SilentlyContinue -ErrorVariable Err
  70.             [array]$AltComputerName = $Err.OriginInfo.PSComputerName
  71.            
  72.             Foreach ( $Computer in $AltComputername )
  73.             {
  74.                 Try
  75.                 {
  76.                 $AllCimSessions += New-Cimsession -ComputerName $Computer -erroraction Stop -Credential $Creds
  77.                 }
  78.                 Catch
  79.                 {
  80.                 Write-Output "Unable to create a connection to $Computer"
  81.                 $error[0] | Select * | Out-File "C:\temp\DiskInformation LogFile.log" -append
  82.                 }
  83.             }
  84.        
  85.  
  86.     Write-Verbose "Enumerating all disks on all servers"
  87.     $Disks = Get-CimInstance Win32_LogicalDisk -filter "DriveType = 3" -CimSession $AllCimSessions
  88.  
  89.     Write-Verbose "Processing disk information"
  90.     Foreach ( $Disk in $Disks )
  91.         {
  92.        
  93.             [array]$DiskResults += New-Object PSObject -Property @{
  94.                 Computer = $Disk.PSComputerName
  95.                 Drive = $Disk.DeviceID
  96.                 CapacityGB = "{0:N2}" -f ($Disk.Size / 1gb)
  97.                 CapacityMB = "{0:N2}" -f ($Disk.Size / 1mb)
  98.                 CapacityRaw = $Disk.Size
  99.                 UsedGB = "{0:N2}" -f ( ($Disk.Size - $Disk.FreeSpace) / 1gb )
  100.                 UsedMB = "{0:N2}" -f ( ($Disk.Size - $Disk.FreeSpace) / 1mb )
  101.                 UsedPercent = "{0:N2}" -f ( ( $Disk.Size - $Disk.FreeSpace ) / $Disk.Size)
  102.                 UsedRaw = ($Disk.Size - $Disk.FreeSpace)
  103.                 FreeGB = "{0:N2}" -f ($Disk.FreeSpace / 1gb)
  104.                 FreeMB = "{0:N2}" -f ($Disk.FreeSpace / 1mb)
  105.                 FreePercent = "{0:N2}" -f  ($Disk.FreeSpace / $Disk.Size )
  106.                 FreeRaw = $Disk.FreeSpace
  107.                 VolumeName = $Disk.VolumeName
  108.                 }
  109.         }
  110.    
  111.     }
  112.  
  113.     End
  114.     {
  115.         if( $ShowAll )
  116.         {
  117.         $DiskResults | Select * | Sort Computer
  118.         }
  119.         Else
  120.         {
  121.         $DiskResults | Select Computer, Drive, FreePercent, CapacityGB, UsedGB, FreeGB | Sort Computer
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment