Solved

Powershell DiskMonitor Script

Posted on 2014-10-07
9
225 Views
Last Modified: 2014-10-07
Hi there

I have a script that i found on the web (cant remember where).  Ive used it and it does exactly what i want which is report back on disks that are belwo a specified %.

However Ive noticed that there are around a handfull of servers that it doesnt report on.  The servers exist, can be pinged and the wmi-object commands work.

THe report emails out, so if if these servers are in a lkist with others the email still comes out just with those servers missing.  If i run the script with only the servers that dont work then theres no email or any errors?

$users = "Email Address" # List of users to email your report to (separate by comma)
$fromemail = "Email Address"
$server = "SMTP Server" #enter your own SMTP server DNS name / IP address here
$list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
# Set free disk space threshold below in percent (default at 10%)
[decimal]$thresholdspace = 5

#assemble together all of the free disk space data from the list of servers and only include it if the percentage free is below the threshold we set above.
$tableFragment= Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
| ConvertTo-HTML -fragment 

# assemble the HTML for our body of the email report.
$HTMLmessage = @"
<font color=""black"" face=""Arial, Verdana"" size=""3"">
<u><b>Disk Space Storage Report</b></u>
<br>This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed.
<br>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;padding: 0 1.5em;}
table{color:#FFF;background:#C00;border-collapse:collapse;width:647px;border:5px solid #900;}
thead{}
thead th{padding:1em 1em .5em;border-bottom:1px dotted #FFF;font-size:120%;text-align:left;}
thead tr{}
td{padding:.5em 1em;}
tfoot{}
tfoot td{padding-bottom:1.5em;}
tfoot tr{}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
$tableFragment
</body>
"@ 

# Set up a regex search and match to look for any <td> tags in our body. These would only be present if the script above found disks below the threshold of free space.
# We use this regex matching method to determine whether or not we should send the email and report.
$regexsubject = $HTMLmessage
$regex = [regex] '(?im)<td>'

# if there was any row at all, send the email
if ($regex.IsMatch($regexsubject)) {
				send-mailmessage -from $fromemail -to $users -subject "Disk Space Monitoring Report" -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $server
}

Open in new window

0
Comment
Question by:Colchester_Institute
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 19

Expert Comment

by:Miguel Angel Perez Muñoz
ID: 40365619
Could you check execution policy on servers where work and where not work?

Do a get-executionpolicy and set same on servers where script is failing. Additionally, can search on powershell event viewer what is happening.
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 40365630
The policys are set as unrestricted on all servers......also no errors within event viewer?
0
 
LVL 16

Accepted Solution

by:
Rajitha Chimmani earned 500 total points
ID: 40366109
Try to run just these commands (one by one not as a script) just on those servers and see if you get any results. There is some condition to filter the results based on free space threshold. Possible that those servers dont have any drives which satisfy the condition

$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
[decimal]$thresholdspace = 5
$tableFragment= Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}}

$tablefragment #print the output from above command

Then, try the following command to see if you get any results

$tablefragment | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Colchester_Institute
ID: 40366140
I know the drives meet the condition set as its regarding space being less than 5% and they have drives that have space that is less than 5%

Im a bit confused on how to run

"$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
 [decimal]$thresholdspace = 5
 $tableFragment= Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
 | select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}}"


as no txt file specified?
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40366160
Ohh..yeah..my bad

$computers = get-content <path to txt file>
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 40366175
ok so if i run

$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
  [decimal]$thresholdspace = 5
  $tableFragment= Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
  | select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}}

nothing appears.

I then run

$tablefragment

and the server in the list appears

i then run

$tablefragment | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace}

and get nothing
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 40366202
i think its the drive type....is there a way i can tell this to search for 1,2,3,4,5?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40366214
you should be able to inclose the range in a bracket to use a regular expression like so.

$tablefragment | Where-Object {$_.DriveType -eq [1-5] -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace}
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 40366230
Its ok the info i was given was wrong and the script is working, but thank you fo showing me how to break it down and test it.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question