# -----------------------------------------------------------------------------
############################
# Script Function: This sample Windows powershell script calculates free disk
spaces
# in multiple servers and emails copy of csv report.
# Author: Victor Ashiedu
# Date: 16/05/2014
# Website: http://www.iTechguides.com
# -----------------------------------------------------------------------------
############################
## BEGGINNIBF OF SCRIPT ###
#Set execution policy to Unrestricted (-Force suppresses any confirmation)
#Execution policy stopped the script from running via task scheduler
#As a work-around, I added an action in the task scheduler to run first before this
script runs
# Set-ExecutionPolicy Unrestricted -Force
Set-ExecutionPolicy Unrestricted -Force
#delete reports older than 7 days
$OldReports = (Get-Date).AddDays(-7)
#edit the line below to the location you store your disk reports# It might also
#be stored on a local file system for example, D:\ServerStorageReport\DiskReport
Get-ChildItem D:\CheckFreeSpace.v3\*.* | `
Where-Object { $_.LastWriteTime -le $OldReports} | `
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
#Create variable for log date
$LogDate = get-date -f yyyyMMddhhmm
#Define location of text file containing your servers. It might also
#be stored on a local file system for example, D:\CheckFreeSpace.v3
$File = Get-Content -Path D:\CheckFreeSpace.v3\Servers.txt
#Define admin account variable (Uncommented it and the line in Get-WmiObject
#Line 44 below is commented out because I run this script via task schedule.
#If you wish to run this script manually, please uncomment line 44.
#$RunAccount = get-Credential
# $DiskReport = ForEach ($Servernames in ($File))
#the disk $DiskReport variable checks all servers returned by the $File variable
(line 37)
$DiskReport = ForEach ($Servernames in ($File))
{Get-WmiObject Win32_Volume <#-Credential $RunAccount#> `
-ComputerName $Servernames `
-ErrorAction SilentlyContinue |
#write-host "$Servernames"
#return only disks with
#free space less
#than or equal to 0.1 (10%)
Where-Object { {"fsutil fsinfo ntfsinfo " + $drive.Name} }
#create reports
$DiskReport |
Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DriveLetter}},
@{Label = "Drive Name";Expression = {$_.Label}},
@{Label = "Capacity";Expression = {"{0:N1}" -f( $_.capacity/1gb)}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
@{Label = "BytesPerCluster";Expression = {"{0:N1}" -f( $_.blockSize)}} |
#Export report to CSV file (Disk Report)
Export-Csv -path "D:\CheckFreeSpace.v3\DiskReport_$logDate.csv" -NoTypeInformation
##Important note#
# When I ran the script, I received error message
#"Send_mailmessage Unable to connect to remote server"
#This was caused by Mcafee blocking the server from sending email. If this happens,
# please configure Mcafee ePO to allow server to send email.
# If you require help with this, please leave a comment
## END OF SCRIPT ###