Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Generates a csv file of Free drive space for a given list of servers.
- <#
- Output in the form of:
- Date(yyyymmdd), Time (HHMM), ServerName, Drive Letter, Drive Size, Free Space
- This will only list drives that have letters assocaited with them, and of type: 'WMI Drive type 3'. (Usually fixed disk)
- Parameters:
- ComputerList - list of computer names or IP Addresses to check
- Output file - the csv that will be written to
- #>
- $ComputerList="Server24","Server25","Server27","Server31"
- $OutputFile="\\FileServer\Datashare\Reporting\drivespace.csv"
- $TodaysDate=get-date -UFormat "%Y%m%d"
- $TodaysTime=get-date -UFormat "%H%M"
- # Setup temp file and remove if it already exists
- $TempFile="$env:TEMP`\Drivespacecheck$TodaysDate$TodaysTime.csv"
- if (Test-Path -path $TempFile)
- {
- Remove-item $TempFile
- }
- foreach ($computer in $Computerlist)
- {
- # Get the computer drives that are drive type 3 and have a drive letter associated
- # Get the Name, drive letter, capacity and free space (round to 0 decimal and conver to GB)
- # Export to a CSV file. This will also include the select-object labels.
- # Repeat for every computer
- 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
- }
- # Remove the select-object label (first line)
- $DriveSpace=get-content $TempFile | select-object -skip 1
- # Add the date time columns to the start of each line, and a CRLF at the end of the line
- foreach ($line in $DriveSpace)
- {
- $line="`"$TodaysDate`",`"$TodaysTime`","+$line
- $full=$full+$line+"`r`n"
- }
- # Remove empty lines (the excess CRLF at the end)
- $full=$full.trim()
- # Remove the speech marks and write to the output file.
- $full -replace "`"","" | out-file $OutputFile -append
Add Comment
Please, Sign In to add comment