Making Power Shell script more appealing

210 pts.
Powershell commands
PowerShell Scripts
Could someone please advise me, based on the below code, how to: 1) make the Server name present as "Server" rather then the __SERVER it is supposedly scripted to - and 2) how to change the DriveType -eq 3 to just specify drive type = Hard Drive or HD?

$users = "" # List of users to email your report to (separate by comma) $fromemail = "" $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 = 10

#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 }

Answer Wiki

Thanks. We'll let you know when a new response is added.

Really nothing more than a text file that contains a series of PowerShell commands. Microsoft actually classifies scripts as a type of PowerShell modules. When imported, modules allow administrators to execute code that does not exist within the core command set. In essence, the script that you create has a file name, and that file name can be treated as a PowerShell command the script must be a text file that has a file extension of .PS1.include code that PowerShell can run

Discuss This Question: 1  Reply

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • BigKat
    don't know for sure what limits powerscript places on the SQL statement, but it might be as simple as changing select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' to select __SERVER as Server, case DriveType when 1 then 'xx' when 2 then 'yy' when 3 then 'HD' end as DriveType, VolumeName, Name, @{n='Size (Gb)'
    8,750 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: