This post explains how to monitor DiskSpace of a group of listed servers in a text file.
The function Get-DiskSpaceReport comprises of various cmdLets and function to monitor Disk Drives.
- Get-Win32LogicalDisks
- HTML Ouptut
- Email Address validation
You can customize it as per your requirement.The Function Get-DiskSpaceReport has six input parameters:-
- ComputerList – List of Servers – Path of a input file where servers are listed
- Warning – Warning Threshold – Default = 25%
- Critical – Critical Threshold – Default =15%
- SMTPMail – SMTP mail address
- FromID – Valid Email ID
- ToID – Valid Email ID
Example 1:- Execute with default threshold values. By default, the threshold are set to 25(Warning) and 15(Critical)
PS:\>Get-DiskSpaceReport -ComputerList c:\computer.txt -To pjayaram@Appvion.com -From pjayaram@appvion.com -SMTPMail qqma01.ppp.com
Example 2:- Customize the default threshold values.
PS:\>Get-DiskSpaceReport -ComputerList c:\computer.txt -warning 15 -critical 10 -To pjayaram@Appvion.com -From pjayaram@appvion.com -SMTPMail qqma01.ppp.com
First create a function Get-DiskSpaceReport using the below code and do a function call. which is shown above
****************************************************************************
Function Get-DiskSpaceReport
{
param(
[String]$ComputerList,[int]$warning,[int]$critical,[String]$To,[String]$From,[string]$SMTPMail
)
$script:list = $ComputerList
$freeSpaceFileName = “C:\FreeSpace.htm”
if ($Warning -eq “$NULL”)
{
$Warning=25
}
if ($critical -eq “$NULL”)
{
$critical=15
}
$critical = $critical
$warning = $warning
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3 -OR $_.drivetype -eq 2 } | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
Add-Content $fileName “<html>”
Add-Content $fileName “<head>”
Add-Content $fileName “<meta http-equiv=’Content-Type’ content=’text/html; charset=iso-8859-1′>”
Add-Content $fileName ‘<title>DiskSpace Report</title>’
add-content $fileName ‘<STYLE TYPE=”text/css”>’
add-content $fileName “<!–”
add-content $fileName “td {”
add-content $fileName “font-family: Tahoma;”
add-content $fileName “font-size: 11px;”
add-content $fileName “border-top: 1px solid #999999;”
add-content $fileName “border-right: 1px solid #999999;”
add-content $fileName “border-bottom: 1px solid #999999;”
add-content $fileName “border-left: 1px solid #999999;”
add-content $fileName “padding-top: 0px;”
add-content $fileName “padding-right: 0px;”
add-content $fileName “padding-bottom: 0px;”
add-content $fileName “padding-left: 0px;”
add-content $fileName “}”
add-content $fileName “body {”
add-content $fileName “margin-left: 5px;”
add-content $fileName “margin-top: 5px;”
add-content $fileName “margin-right: 0px;”
add-content $fileName “margin-bottom: 10px;”
add-content $fileName “”
add-content $fileName “table {”
add-content $fileName “border: thin solid #000000;”
add-content $fileName “}”
add-content $fileName “–>”
add-content $fileName “</style>”
Add-Content $fileName “</head>”
Add-Content $fileName “<body>”
add-content $fileName “<table width=’100%’>”
add-content $fileName “<tr bgcolor=’#CCCCCC’>”
add-content $fileName “<td colspan=’7′ height=’25’ align=’center’>”
add-content $fileName “<font face=’tahoma’ color=’#003399′ size=’4′><strong>DiskSpace Report – $date</strong></font>”
add-content $fileName “</td>”
add-content $fileName “</tr>”
add-content $fileName “</table>”
}
# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName “<tr bgcolor=#CCCCCC>”
Add-Content $fileName “<td width=’10%’ align=’center’>Drive</td>”
Add-Content $fileName “<td width=’50%’ align=’center’>Drive Label</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>Total Capacity(GB)</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>Used Capacity(GB)</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>Free Space(GB)</td>”
Add-Content $fileName “<td width=’10%’ align=’center’>Freespace %</td>”
Add-Content $fileName “</tr>”
}
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName “</body>”
Add-Content $fileName “</html>”
}
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “<tr>”
Add-Content $fileName “<td>$devid</td>”
Add-Content $fileName “<td>$volName</td>”
Add-Content $fileName “<td>$totSpace</td>”
Add-Content $fileName “<td>$usedSpace</td>”
Add-Content $fileName “<td>$frSpace</td>”
Add-Content $fileName “<td>$freePercent</td>”
Add-Content $fileName “</tr>”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “<tr>”
Add-Content $fileName “<td>$devid</td>”
Add-Content $fileName “<td>$volName</td>”
Add-Content $fileName “<td>$totSpace</td>”
Add-Content $fileName “<td>$usedSpace</td>”
Add-Content $fileName “<td>$frSpace</td>”
Add-Content $fileName “<td bgcolor=’#FF0000′ align=center>$freePercent</td>”
#<td bgcolor=’#FF0000′ align=center>
Add-Content $fileName “</tr>”
}
else
{
Add-Content $fileName “<tr>”
Add-Content $fileName “<td>$devid</td>”
Add-Content $fileName “<td>$volName</td>”
Add-Content $fileName “<td>$totSpace</td>”
Add-Content $fileName “<td>$usedSpace</td>”
Add-Content $fileName “<td>$frSpace</td>”
Add-Content $fileName “<td bgcolor=’#FBB917′ align=center>$freePercent</td>”
# #FBB917
Add-Content $fileName “</tr>”
}
}
writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $script:list)
{
if(Test-Connection -ComputerName $server -Count 1 -ea 0) {
Add-Content $freeSpaceFileName “<table width=’100%’><tbody>”
Add-Content $freeSpaceFileName “<tr bgcolor=’#CCCCCC’>”
Add-Content $freeSpaceFileName “<td width=’100%’ align=’center’ colSpan=6><font face=’tahoma’ color=’#003399′ size=’2′><strong> $server </strong></font></td>”
Add-Content $freeSpaceFileName “</tr>”
writeTableHeader $freeSpaceFileName
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3 }
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
Add-Content $freeSpaceFileName “</table>”
}
writeHtmlFooter $freeSpaceFileName
Function sendEmail
{
param($from,$to,$subject,$smtphost,$htmlFileName)
[string]$receipients=”$to”
$body = Get-Content $htmlFileName
$body = New-Object System.Net.Mail.MailMessage $from, $receipients, $subject, $body
$body.isBodyhtml = $true
$smtpServer = $MailServer
$smtp = new-object Net.Mail.SmtpClient($smtphost)
$validfrom= Validate-IsEmail $from
if($validfrom -eq $TRUE)
{
$validTo= Validate-IsEmail $to
if($validTo -eq $TRUE)
{
$smtp.Send($body)
write-output “Email Sent!!”
}
}
else
{
write-output “Invalid entries, Try again!!”
}
}
# Email our report out
function Validate-IsEmail ([string]$Email)
{
return $Email -match “^(?(“”)(“”.+?””@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&’\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$”
}
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
sendEmail -from $From -to $to -subject “Disk Space Report – $Date” -smtphost $SMTPMail -htmlfilename $freeSpaceFileName
}
***************************************************************
Output :-
Download the code here Disk
Thanks for reading my space….
Happy Learning!!!
hi,
i ran the powershell script sucessfully then when i am making a call to the function getting the below error.
Get-DiskSpaceReport : The term ‘Get-DiskSpaceReport’ is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
LikeLike
Hi,Can you once check the arguments that you are passing and make sure that it properly separated by space
Example-
Ps:\>get-disk -com abc -war 10….
If possible, post me a function call
-Prashanth
LikeLike
I just have some basic questions for you. I am new to Power shell I have downloaded the disk file, but which directory on the server it should it go? What name it should have?
Sorry for asking such basics…..
LikeLike
Hello Prashanth. Excellent script! I have one question – how can I edit to send email to multiple users? I have tried wrapping address with quotes, using comma and semicolon, no luck so far.
LikeLike
Modify the mail sending function of the code like below
Function sendEmail
{
param($from,$to,$subject,$smtphost,$htmlFileName)
[string]$receipients=”$to”
$body = Get-Content $htmlFileName
$body = New-Object System.Net.Mail.MailMessage $from, $receipients, $subject, $body
$body.isBodyhtml = $true
$smtpServer = $MailServer
$smtp = new-object Net.Mail.SmtpClient($smtphost)
$smtp.Send($body)
}
Do a function call
$emlst=”pjayaram@app.com,rghi@app.com”
Get-DiskSpaceReport -ComputerList c:\computer.txt -To $emlst -From pjayaram@app.com -SMTPMail mail01.app.com
Let me know if you need full code to be pasted here – Prashanth
LikeLike
First I will like to thank you for this awesome script . Sending multiple users is not working for me . After the replacement the function html format is gone .If you can please post the script.
LikeLike
Hi Coorsktm,
Can you try the below script
Function Get-DiskSpaceReport
{
param(
[String]$ComputerList,[int]$warning,[int]$critical,[String]$To,[String]$From,[string]$SMTPMail
)
$script:list = $ComputerList
$freeSpaceFileName = “c:\FreeSpace.htm”
if ($Warning -eq “$NULL”)
{
$Warning=25
}
if ($critical -eq “$NULL”)
{
$critical=15
}
$critical = $critical
$warning = $warning
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3 -OR $_.drivetype -eq 2 } | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName ‘DiskSpace Report’
add-content $fileName ”
add-content $fileName “”
add-content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “DiskSpace Report – $date”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
}
# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “Drive”
Add-Content $fileName “Drive Label”
Add-Content $fileName “Total Capacity(GB)”
Add-Content $fileName “Used Capacity(GB)”
Add-Content $fileName “Free Space(GB)”
Add-Content $fileName “Freespace %”
Add-Content $fileName “”
}
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “”
}
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
#
Add-Content $fileName “”
}
else
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
# #FBB917
Add-Content $fileName “”
}
}
writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $script:list)
{
if(Test-Connection -ComputerName $server -Count 1 -ea 0) {
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “ $server ”
Add-Content $freeSpaceFileName “”
writeTableHeader $freeSpaceFileName
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3 }
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
Add-Content $freeSpaceFileName “”
}
writeHtmlFooter $freeSpaceFileName
Function sendEmail
{
param($from,$to,$subject,$smtphost,$htmlFileName)
[string]$receipients=”$to”
$body = Get-Content $htmlFileName
$body = New-Object System.Net.Mail.MailMessage $from, $receipients, $subject, $body
$body.isBodyhtml = $true
$smtpServer = $MailServer
$smtp = new-object Net.Mail.SmtpClient($smtphost)
$smtp.Send($body)
}
# Email our report out
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
sendEmail -from $From -to $to -subject “Disk Space Report – $Date” -smtphost $SMTPMail -htmlfilename $freeSpaceFileName
}
Get-DiskSpaceReport -ComputerList E:\OutPut\server.txt -To “pjayaram@app.com,prarthana@app.com” -From pjayaram@appvion.com -SMTPMail mail01.smtp.com
****************************
LikeLike
Thank you for the quick reply and send me the code . It worked now . Thanks again .
LikeLike
You are welcome:-)
LikeLike
Cool/Very useful Script….What If my environment has few Server(s) with Mount Points? Will Get-Win32LogicalDisks pull Mount points info as well?
LikeLike
Thanks Sree. Yes, DriveType =2 includes mount points.
Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3 -OR $_.drivetype -eq 2 } | format-table DeviceID, VolumeName,status,Size,FreeSpace..Try this.
LikeLike
Very nice! I have so many servers. I would only like to see the drives that are warning or critical.
LikeLike
Hello Howard,
Just replace the writeDiskInfo function by a given code.
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -lt $warning -and $freePercent -gt $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
#
Add-Content $fileName “”
}
<#else
{
Add-Content $fileName "”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}#>
}
LikeLike
Thank you Prashanth! Really useful script.
LikeLike
Prashanth, most excellent script. I am getting access denied to Domain servers so rather than logging out and logging in with Admin account, how do I use the -Credential just once in script to give me access to the domain computers? I would supply my domain admin account of course. Thanks, Bruce
LikeLike
Prashanth, I cannot seem to get the critical freespace to come up in red even though they are less than 15% available. I am not entering anything on the PS command parameters, just taking the default values in the script. Any suggestions?
LikeLike
Hello Bruce,
I just executed the script, It came as expected. Lets change input parameter value – Warning -> 50 and critical ->25. Let me know the output..
I hope you are download the script and executing it.
LikeLike
Thanks Prashanth, had a type-o, all fixed on this one. Did you see my other post though regarding:
I am getting access denied to Domain servers so rather than logging out and logging in with Admin account, how do I use the -Credential just once in script to give me access to the domain computers? I would supply my domain admin account of course. Thanks, Bruce
LikeLike
Hello Bruce,
I’m working on it…I’ll update you soon.
LikeLike
Thank you, so much for your script!
LikeLike
You are welcome… Thanks for reading
LikeLike
Hi, this is a great report, but I can’t get it to run no matter what I try. I’ve only edited the following:
$freeSpaceFileName = e:\scripts\diskreport\freespace.html
I’ve tried leaving your default and no luck. I’m not sure if it’s even reading the list of servers I put in, and I’ve tried moving that to different folders. It just runs very quickly, but i get no email or anything else.
Please Help!
LikeLike
Jake,
After creating a function, did you call the function like below
ps:\>Get-DiskSpaceReport -ComputerList c:\computer.txt -To pjayaram@Appvion.com -From pjayaram@appvion.com -SMTPMail qqma01.ppp.com
Change the above input values and try to run it again?
–Prashanth
LikeLike
Yes, I’m not sure if I didn’t create the function correctly, but I copied your code, saved it as get-diskspacereport.ps1 and then put int he parameters as you detailed.
LikeLike
also, thank you VERY MUCH for replying!! I read alot of blogs/forums, and I’ve been reading yours for a while. But it’s very hard to get anyone to reply when you have a question, so I really appreciate the help!
LikeLike
I hope you are able to execute the function.
Thanks for reading my space.
-Prashanth
LikeLike
I’m not sure where I’m going wrong. I copied the code again, pasted into the powershell ise, no errors. I run exactly what you detailed and it goes by so quickly I have to think it’s not pulling in the list of servers. I only update the location of the temp file, but I did try to use it leaving that as the default c:\
I thought if it’s a function you can just do exactly as I did, although you can also add this function to your posh profile standard functions? Is that correct?
LikeLike
It’s working great, thank you!
LikeLike
I am trying to change the “background color” and “font color” of the values put into the script for the drive, drive label, Total Capacity, Free Capacity, but then I wouldn’t change the %FreeSpace so that it shows up with the RED/Yellow highlight.
I can’t figure out how to do this, can someone help me out?
Thank you,
Marie
LikeLike
Hello Marie,
You need to modify writeDiskInfo code like below
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
–Prashanth
LikeLike
Hi Prashant,
Excellent piece of work and thank you so much for the script. I would like to know how I can schedule this script to run everyday and send the alert mails?
Thanks in advance
Vinay
LikeLike
Hi Vinay,
1. Download the script and add the function call with required parameters in the same script file
2. Create a batch file and call this script file
Power.bat
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file C:\DiskSpace.ps1
3. Call the batch file using task scheduler
-Prashanth
LikeLike
Hi there,
Is there any way that you can get the output of the actual mountpoint information as well?
LikeLike
Hello Kruger,
You can use the below function to get mount point
related data
function Get-MountPoint {
param (
[string]$computername = “localhost”
)
Get-WmiObject -Class Win32_MountPoint -ComputerName $computername |
where {$_.Directory -like ‘Win32_Directory.Name=”*”‘} |
foreach {
$vol = $_.Volume
Get-WmiObject -Class Win32_Volume -ComputerName $computername | where {$_.__RELPATH -eq $vol} |
Select @{Name=”Folder”; Expression={$_.Caption}},
@{Name=”Size (GB)”; Expression={“{0:F3}” -f $($_.Capacity / 1GB)}},
@{Name=”Free (GB)”; Expression={“{0:F3}” -f $($_.FreeSpace / 1GB)}},
@{Name=”%Free”; Expression={“{0:F2}” -f $(($_.FreeSpace/$_.Capacity)*100)}}|ft -AutoSize
}
}
Get-MountPoint -Computername
-Prashanth
LikeLike
Hi Prashanth
Great script, it’s working very well for my servers. but for some reason, it can’t not retrieve disk information from any Windows 7 systems.
Thanks
LikeLike
Hi Prasanth,
Its a cool Script Not sure whether people noticed this wheni executed the script, it runs fine bt the issue is with some table alignment when i access the email thorugh outlook client.
LikeLike
Hi am trying to run this the batch script but i am not getting the htm file generated or any mails.
C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell.exe -Command “”
Is there a way to just generate the report as htm file
LikeLike
Rasheed,
Open PowerShell-ISE and copy the code then try to call the function.
You can remove the sendemail function. Let me know if you face any difficulty
–Prashanht
LikeLike
Howdy! We possjbly could have sworn I’ve gone to your blog before but after lookiung at some of the articles I
realized it’s fresh to me. Anyhow, I’m definitely pleased
I found it and I’ll be book-marking iit
and checking back frequently!
LikeLike
Hi Prashanth
the script works great! Now I have an SMTP server that requires authentication, could you help me enabling authentication in the script?
Thanks in advance!
LikeLike
I’ve not tested this though, Can you try the below article?
http://petermorrissey.blogspot.com/2013/01/sending-smtp-emails-with-powershell.html
–Prashanth
LikeLike
Sure, I’ll do and will let you know. Thanks.
LikeLike
HI Prashanth,
It’s a nice script. I need a script which scans the servers in a list for disk space and send an email only for the servers which are low in disk space.
LikeLike
Hi,
Script is really good . But all content looks cornered , Need to align them to center .
New to PS scripting . Please help .
LikeLike
Hi Preethi,
Replace the WriteDiskInfo function with the below code
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
#
Add-Content $fileName “”
}
else
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
# #FBB917
Add-Content $fileName “”
}
}
–Prashanth
LikeLike
Hi ,
No Luck . Pls help ..
LikeLike
Preethi,
What is the issue? It may be access issue. Some times you can’t write to C drive. Change the path to some other path where you’ve full access.
–Prashanth
LikeLike
Hi prashanth
excellent script and it is helping people so many years….
I need one help is there any way we can integrate this script use different creds for different servers
for eg a csv file with server name and credentials reference coloun where we provide creds through thescript ive raised a question for this on stackoverflow but canot get it to work stackoverflow.com/questions/25409163/
now im getting error cannot divide by zero at line freepercent = ($frspace/
your help is much appreciated man
thanks in advance
LikeLike
Can you check this post?
Let me know in case of any difficulties.
–Prashanth
LikeLike
Hey Prashant I need some help with that script…
Please help buddy,,,,
LikeLike
Hi ,
I need to add multiple DLs .Post me the complete script
LikeLike
Can you try this ?
Function Get-DiskSpaceReport
{
param(
[String]$ComputerList,[int]$warning,[int]$critical,[String]$To,[String]$From,[string]$SMTPMail
)
$script:list = $ComputerList
$freeSpaceFileName = “C:\FreeSpace.htm”
if ($Warning -eq “$NULL”)
{
$Warning=25
}
if ($critical -eq “$NULL”)
{
$critical=15
}
$critical = $critical
$warning = $warning
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3 -OR $_.drivetype -eq 2 } | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName ‘DiskSpace Report’
add-content $fileName ”
add-content $fileName “”
add-content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “DiskSpace Report – $date”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
}
# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “Drive”
Add-Content $fileName “Drive Label”
Add-Content $fileName “Total Capacity(GB)”
Add-Content $fileName “Used Capacity(GB)”
Add-Content $fileName “Free Space(GB)”
Add-Content $fileName “Freespace %”
Add-Content $fileName “”
}
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “”
}
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
#
Add-Content $fileName “”
}
else
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
# #FBB917
Add-Content $fileName “”
}
}
writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $script:list)
{
if(Test-Connection -ComputerName $server -Count 1 -ea 0) {
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “ $server ”
Add-Content $freeSpaceFileName “”
writeTableHeader $freeSpaceFileName
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3 }
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
Add-Content $freeSpaceFileName “”
}
writeHtmlFooter $freeSpaceFileName
Function sendEmail
{
param($from,$to,$subject,$smtphost,$htmlFileName)
[string]$receipients=”$to”
$body = Get-Content $htmlFileName
$body = New-Object System.Net.Mail.MailMessage $from, $receipients, $subject, $body
$body.isBodyhtml = $true
$smtpServer = $MailServer
$smtp = new-object Net.Mail.SmtpClient($smtphost)
$smtp.Send($body)
}
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
sendEmail -from $From -to $to -subject “Disk Space Report – $Date” -smtphost $SMTPMail -htmlfilename $freeSpaceFileName
}
LikeLike
Hello Prashanth,
Is there any way to save the output locally instead of emailing?
Regards
LikeLike
Yes, You can. Take out all the mailing sending piece from code to write output locally.
Let me know if you face any difficulty. Sorry for replying late.
–Prashanth
LikeLike
Hi Prashanth,
you are from which place , do you give any trainings on scripts.
am from hyderabad working in bangalore if you are from same place please let me know.
Thanks & Regards
K.Shivashanker
LikeLike
I’m also from Bangalore.I’m in US for long term contract but i can help you in some way 🙂
–Prashanth
LikeLike
Hello Prashanth,
My email is not outputting the disk space, it only shows the header and rest is blank. Do you have any suggsetions?
LikeLike
Hello,
Can you try to run the below script and post me the output also see the threshold values that you are passing meets the condition?
foreach ($server in Get-Content c:\server.txt)
{
if(Test-Connection -ComputerName $server -Count 1 -ea 0) {
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3 }
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
}
–Prashanth
LikeLike
thanks for the quick reply, when I run this script it doesn’t do anything.
LikeLike
Hello,
Did you list the server names in server.txt file and run the script?
Can you explain the steps that you are following to
execute the script?
foreach ($server in Get-Content c:\server.txt)
{
if(Test-Connection -ComputerName $server -Count 1 -ea 0) {
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3 }
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
}
–Prashanth
LikeLike
I saved this script as test.ps1 and I have a txt file with a list of server names
LikeLike
Hello Prashanth,
I tried running the script from different machine and it works, I think there is something wrong with WMI from my machine. Thanks for all your help!
LikeLike
Great!!
Thanks for reading my space.
–Prashanth
LikeLike
Hi Prashanth,
I get the same issue as Desmond. When I run the script it does not do anything. I have the list of server names in c:\servers.txt file one line each. I have tried both examples – with the default threshold and specifying a threshold but still nothing. I have also tried editing the script as mentioned above “foreach ($server in Get-Content c:\servers.txt)” still no joy 😦
LikeLike
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\DSM\DSM.ps1:154 char:8
+ $dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_ …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
LikeLike
Vipin,
The remote server seems to be not reachable.
Can you check the server once?
–Prashanth
LikeLike
Hello Prashanth,
It is a great article. I tried to run the script on my computer but …
PS C:\> ./DiskSpace.ps1
PS C:\> Get-DiskSpace-ComputerList c:\computer.txt -warning 15 -critical 10 -To @test.com -From @test.com -SMTPMail 10.0.0.0
The term ‘Get-Disk-ComputerList’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:22
+ Get-Disk-ComputerList <<<< c:\computer.txt -warning 15 -critical 10 -To jgarcia@grandbeach.com -From jgarcia@grandbe
ach.com -SMTPMail 10.2.16.125
+ CategoryInfo : ObjectNotFound: (Get-Disk-ComputerList:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
How can you help to me?.
Thanks!
JG
LikeLike
Hi JG
There is a space after Get-DiskSpace
-Computerlist is the argument list that you are going to pass.
Try the below command with space in between GetDiskSpace and Computerlist.
Get-DiskSpace -ComputerList c:\computer.txt -warning 15 -critical 10 -To @test.com -From @test.com -SMTPMail 10.0.0.0
LikeLike
Hi Prashanth the script was really Good.
how to Send Report to multiple receipients
LikeLike
Hi Santhosh,
Please go through the comments. In one of my reply I have posted a script to send to multiple ids
Prashanth
LikeLike
Hi Prashanth,
Greetings. It’s very interesting script. I wonder if can enhance further like below :
Site 1
– Server Application 1
– Server Application 2
– Server Application 3
– Server Application 4
Site 2
– Server Application 1
– Server Application 2
– Server Application 3
– Server Application 4
– Server Application 5
Site 3
– Server Application 1
– Server Application 2
– Server Application 3
Hope to hear from you and following you 🙂
LikeLike
Are you looking for something like this?
Input file name ServerList.csv
ServerName,ApplicationName
SITE1,APP1
SITE1,APP2
SITE2,App1
SITE3,APP1
CODE
# First lets create a text file, where we will later save the freedisk space info
$ServerList = “f:\Powersql\ServerList.csv”
$freeSpaceFileName = “f:\Powersql\Output.htm”
$emlist=”pjayaram@abc.com”
$mailServer=”abcdv.datamail.com”
$warning = 25
$critical = 10
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName ‘Daily DiskSpace Report’
add-content $fileName ”
add-content $fileName “”
add-content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “DiskSpace Report – $date”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
}
# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “Drive”
Add-Content $fileName “Drive Label”
Add-Content $fileName “Total Capacity(GB)”
Add-Content $fileName “Used Capacity(GB)”
Add-Content $fileName “Free Space(GB)”
Add-Content $fileName “Freespace %”
Add-Content $fileName “”
}
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “”
}
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
#
Add-Content $fileName “”
}
else
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
# #FBB917
Add-Content $fileName “”
}
}
Function sendEmail
{
param($from,$to,$subject,$smtphost,$htmlFileName)
$body = Get-Content $htmlFileName
$body = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$body.isBodyhtml = $true
$smtpServer = $MailServer
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($body)
}
writeHtmlHeader $freeSpaceFileName
Import-Csv $ServerList |ForEach-Object {
$server=$_.ServerName
$AppName=$_.ApplicationName
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “ $server – $AppName”
Add-Content $freeSpaceFileName “”
writeTableHeader $freeSpaceFileName
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3}
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
write-host $item.VolumeName
if($item.VolumeName -like “BMC Patrol” -or $item.VolumeName -like “Maestro” -or $item.VolumeName -like “Swap” -or $item.VolumeName -like “PageFile” -or $item.VolumeName -like “Data” )
{
write-host 1
}
else
{
writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
Add-Content $freeSpaceFileName “”
}
writeHtmlFooter $freeSpaceFileName
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
sendEmail pjayaram@abc.com $emlist ” Disk Space Report – $Date” $mailServer $freeSpaceFileName
LikeLike
Hi Prashanth,
When I call the function, the htm file is created with all the disk information. I have a problem with sending the e-mail it says,the client is not authenticated. Please help. Thanks
LikeLike
Hi Maria,
I think this is some problem with the exchange setting.
Please check with exchange admin
–Prashanth
LikeLike
Hi Prashanth
I hope you are still replying to Questions on this thread, I could really use your help.
I am not having much with luck with this Script.
Could you help me edit it Please.
I simply want the Script to read a list of Servers from my C: Drive
C:\Mark\ServerList.txt
I only need it E-mailed to myself:
Marky.mark@yahoo.com
SMTP Server:
smtprelay.marky.com
LikeLike
Hi Mark,
First you need to create the function and call the function with the parameters like below
PS:\>Get-DiskSpaceReport -ComputerList c:\Mark\ServerList.txt -warning 15 -critical 10 -To Marky.mark@yahoo.com -From Marky.mark@yahoo.com -SMTPMail smtprelay.marky.com
Try the below line and let me know the outcome. Also,
Can you let me know on how you are calling the function?
–Prashanth
LikeLike
Hi Prashanth,
Thanks for your post and it help me to learn powershell bit easier and also your code is working like charm. But mail function getting failed with below error.
Exception calling “Send” with “1” argument(s): “Failure sending mail.”
At D:\Personal\PSTools\ControllerReport.ps1:179 char:1
+ $smtp.Send($body)
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
i’ve passed the arguments like below
Get-DiskSpaceReport -ComputerList D:\Personal\PSTools\computers.txt -warning 15 -critical 10 -To Siva_sankara_rao_bac@dell.com -From Siva_sankara_rao_bac@app.com -SMTPMail App.xyz.abc.COM
Please help me here.
Thanks,
Siva.
LikeLike
Hi Sica,
I think its a problem with SMTP server address. Can you recheck and ensure its working fine?
Just try to send only email portion of the code.
–Prashanth
LikeLike
Hi Prashant ,
Did you work on bruce’s issue with the Access denied stuffs, As I am also Facing the same issue.
I am getting access denied to Domain servers so rather than logging out and logging in with Admin account, how do I use the -Credential just once in script to give me access to the domain computers? I would supply my domain admin account of course.
LikeLike
Hi Kyo,
Please refer my other link where i have credentials. Follow the same for this as well.
Let me know if you end up with any issue
Prashanth
LikeLike
Hi prashant,
I have that access denied issue with the same as brad was facing.Can you please look into this .
The script is just aesthetic as it has to be .any help will be much appreciated
LikeLike
Can I just generate the HTML without emailing out ? Where do I begin ?
LikeLike
Hi B Koo,
Try the below script
Code:-
Function Get-DiskSpaceReport
{
param(
[String]$ComputerList,[int]$warning,[int]$critical
)
$script:list = $ComputerList
$freeSpaceFileName = “f:\PowerSQL\FreeSpace.htm”
if ($Warning -eq “$NULL”)
{
$Warning=25
}
if ($critical -eq “$NULL”)
{
$critical=15
}
$critical = $critical
$warning = $warning
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk | Where-Object {$_.drivetype -eq 3 -OR $_.drivetype -eq 2 } | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString(‘yyyy/MM/dd’)
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
Add-Content $fileName ‘DiskSpace Report’
add-content $fileName ‘’
add-content $fileName “”
add-content $fileName “”
Add-Content $fileName “”
Add-Content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “DiskSpace Report – $date”
add-content $fileName “”
add-content $fileName “”
add-content $fileName “”
}
# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “Drive”
Add-Content $fileName “Drive Label”
Add-Content $fileName “Total Capacity(GB)”
Add-Content $fileName “Used Capacity(GB)”
Add-Content $fileName “Free Space(GB)”
Add-Content $fileName “Freespace %”
Add-Content $fileName “”
}
Function writeHtmlFooter
{
param($fileName)
Add-Content $fileName “”
Add-Content $fileName “”
}
Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace – $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
Add-Content $fileName “”
}
elseif ($freePercent -le $critical)
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
#
Add-Content $fileName “”
}
else
{
Add-Content $fileName “”
Add-Content $fileName “$devid”
Add-Content $fileName “$volName”
Add-Content $fileName “$totSpace”
Add-Content $fileName “$usedSpace”
Add-Content $fileName “$frSpace”
Add-Content $fileName “$freePercent”
# #FBB917
Add-Content $fileName “”
}
}
writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $script:list)
{
if(Test-Connection -ComputerName $server -Count 1 -ea 0) {
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “”
Add-Content $freeSpaceFileName “ $server ”
Add-Content $freeSpaceFileName “”
writeTableHeader $freeSpaceFileName
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3 }
foreach ($item in $dp)
{
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
Add-Content $freeSpaceFileName “”
}
writeHtmlFooter $freeSpaceFileName
<#
Function sendEmail
{
param($from,$to,$subject,$smtphost,$htmlFileName)
[string]$receipients=”$to”
$body = Get-Content $htmlFileName
$body = New-Object System.Net.Mail.MailMessage $from, $receipients, $subject, $body
$body.isBodyhtml = $true
$smtpServer = $MailServer
$smtp = new-object Net.Mail.SmtpClient($smtphost)
$validfrom= Validate-IsEmail $from
if($validfrom -eq $TRUE)
{
$validTo= Validate-IsEmail $to
if($validTo -eq $TRUE)
{
$smtp.Send($body)
write-output “Email Sent!!”
}
}
else
{
write-output “Invalid entries, Try again!!”
}
}
# Email our report out
function Validate-IsEmail ([string]$Email)
{
return $Email -match “^(?(“”)(“”.+?””@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&’\*\+/=\?\^`\{\}\|~\w])*)(?
}
Get-DiskSpaceReport -ComputerList F:\PowerSQL\server.txt
LikeLike
Hi Prashant,
Thanks for your script, works great. I’ve been using it for a few months now for monitoring space on our Exchange servers and have now been asked to implement it for our SQL servers as well.
I am able to do this, however in the email report for the SQL servers the information in the Freespace field is misaligned. Nothing different has been done on this script other that changing the input parameters.
If it would help, I can send you a screen grab to show the issue I am having.
Thanks for your help.
LikeLike
Hi Seni Williams,
Please send me a screenshot.
Prashanth
LikeLike
You can access it via this Dropbox link – https://www.dropbox.com/s/tm6j5427feflbv1/DriveSpaceReport.docx?dl=0
Also noticed my output format looks very different from the image you posted. Didn’t change any of the html code so no sure why.
LikeLike
Hi,
Can you download the code and run it?
If that doesn’t work, Please send the screenshot to powershellsql@gmail.com
Prashanth
LikeLike
Hi Prashanth,
I get below error can you please help me
The term ‘Get-DiskSpaceReport’ is not recognized as the name of a cmdlet, function, script file, or operable program. C
heck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:20
+ Get-DiskSpaceReport <<<< -ComputerList c:\computer.txt -To shkvenka@in.ibm.com -From shkvenka@in.ibm.com -SMTPMail
BRIDGEHEADS.CPWPLC.COM
+ CategoryInfo : ObjectNotFound: (Get-DiskSpaceReport:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
LikeLike
Hi Shiva,
Have you created the function? You need to create the function first and then call the function.
Can you tell me how you are executing the code?
Prashanth
LikeLike
Hi Prashant,
This script is working fine and my requirements is only to get the disk/mount free space if it is critical or below threshold value …and I want to exclude C and P drives … which is not required.
LikeLike
Hi Umesh, Please check out few of my recent posts.
Prashanth
LikeLike
Hi Prashanth,
Thanks for your code. i have a requirement like the list of servers using different username and passwords. COuld you Please help with this?
LikeLike
Hi Thejesh,
You can refer the below article to get an idea on how to pass the credential information to fetch the data.
https://social.technet.microsoft.com/wiki/contents/articles/35926.windows-server-step-by-step-disk-space-monitoring-guide-using-powershell.aspx
or you also refer the below one
Prashanth
LikeLike