daily pastebin goal
62%
SHARE
TWEET

Create a CSV of free space on servers with Powershell

anonit Apr 27th, 2015 (edited) 16 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Generates a csv file of Free drive space for a given list of servers.
  2.  
  3. <#
  4. Output in the form of:
  5. Date(yyyymmdd), Time (HHMM), ServerName, Drive Letter, Drive Size, Free Space
  6. This will only list drives that have letters assocaited with them, and of type: 'WMI Drive type 3'.  (Usually fixed disk)
  7.  
  8. Parameters:
  9. ComputerList - list of computer names or IP Addresses to check
  10. Output file - the csv that will be written to
  11. #>
  12.  
  13. $ComputerList="Server24","Server25","Server27","Server31"
  14. $OutputFile="\\FileServer\Datashare\Reporting\drivespace.csv"
  15.  
  16. $TodaysDate=get-date -UFormat "%Y%m%d"
  17. $TodaysTime=get-date -UFormat "%H%M"
  18.  
  19. # Setup temp file and remove if it already exists
  20. $TempFile="$env:TEMP`\Drivespacecheck$TodaysDate$TodaysTime.csv"
  21. if (Test-Path -path $TempFile)
  22. {
  23.     Remove-item $TempFile
  24. }
  25.  
  26. foreach ($computer in $Computerlist)
  27. {
  28.     # Get the computer drives that are drive type 3 and have a drive letter associated
  29.     # Get the Name, drive letter, capacity and free space (round to 0 decimal and conver to GB)
  30.     # Export to a CSV file.  This will also include the select-object labels.
  31.     # Repeat for every computer
  32.     Get-WmiObject win32_volume -ComputerName $Computer -filter "Drivetype=3 AND DriveLetter != NULL" | select __Server,driveletter,{[math]::round($_.capacity/1024/1024/1024)}, {[math]::round($_.freespace/1024/1024/1024)} | export-csv $Tempfile -append -NoTypeInformation
  33. }
  34.  
  35. # Remove the select-object label (first line)
  36. $DriveSpace=get-content $TempFile | select-object -skip 1
  37.  
  38. # Add the date time columns to the start of each line, and a CRLF at the end of the line
  39. foreach ($line in $DriveSpace)
  40. {
  41.     $line="`"$TodaysDate`",`"$TodaysTime`","+$line
  42.     $full=$full+$line+"`r`n"
  43. }
  44.  
  45. # Remove empty lines (the excess CRLF at the end)
  46. $full=$full.trim()
  47.  
  48. # Remove the speech marks and write to the output file.
  49. $full -replace "`"","" | out-file $OutputFile -append
RAW Paste Data
Top