Thursday, October 10, 2013

Powershell Script - Remote Drivespace (Mountpoints) Emailed Report

10/09/2013: Today's post will outline a remote drivespace reporting script that I routinely use to monitor and report on the available Transaction Log space on my Exchange servers. The script leverages WMI to remotely query for the current freespace & volume size metrics (on either specified drive letters, or all Volume Mount Points on the target system), calculates the space in terms of gigabytes and percentage-free, outputs a report file, and emails the results to an admin mailbox.

I'd generally design this type of script to leverage Exchange Management Shell and dynamically query all servers in the appropriate site in the mailbox role (to automatically accommodate server adds and removals over time). But in this case I wanted this script to be 'blind' to Exchange version differences and also avoid a dependency on Exchange Management Shell. A specific goal in this case was to provide a tool to enable junior admins to evaluate the drive status after backup failure alerts, without the need to run the queries from a machine with a specific version of the Exchange Management Tools installed. So I instead use semicolon-delmited string variables to statically store arrays of server names for processing within the script itself.

Here's a broad outline of the script's processing:
  1. Prompt for any missing parameters
  2. Split my pre-defined delimited string varibles of target servers per site into arrays
  3. Use the Get-Wmiobject cmdlet to retrieve the name, driveletter, capacity, & freespace attributes of matching local drives (drivetype=3), filtering on either the drive letter (for non-mountpoint local drives), or on no-drive letter, to identify mountpoint drives, and then re-filter for a distintive Transaction-Log-identifying folder name substring, to target the Tlog drives in isolation from the array of all mountpoints (including database drives) on the mountpoint root drive.
  4. For flexibility, I chose to build my WMI query as a string -- adding and dropping appropriate syntax for the query, depending on the type of target server -- and then use the Invoke-Expression cmdlet to execute the command. Note: I use the tee cmdlet to feed the resulting output to both the console and an email-able attachment file.
  5. From there, the script uses the standard send-mailmessage cmdlet to email the output report to the specified admin address. 
UPDATE: 11/30/2016: Going through and updating the old code to Gists. Also updated posted version to the version I'm using these days.

No comments:

Post a Comment