Powershell Script to Report on Drive Space Usage in another domain.

This is something I worked up to scan a list of servers, determine their drive space usage, and then calculate a cost to charge a customer.  The script obviously was written with a client-customer, hosted solution in mind.  All that said I scrubbed the code of anything that was actually used by the client/customer but the script has some pretty interesting features that took me a while to put together using hints from others on the web.

The most interesting aspect of this script is the ability to remote into a remote domain and then function as if you were a member of that domain.

This script has a specific purpose where I used it but it could be modified to do something else, so I’m posing it here if nothing else as a reference/example.  Also keep in mind this script was chopped up a good bit to publish to the web so it might be a little buggy now.

Purpose:
Report on remote server hard drive usage when said server is in another domain and alert when usage hits a ceiling in GB.

Requirements:
Quest.ActiveRoles.ADManagement on the system running the script
Domain admin or at least rights to log onto the servers to be scanned
The PC or Server running the script must be able to resolve the foreign domains DNS and Servers FQDN

Other:
The script can also be used with some minor modifications to generate a cost based on Storage usage, this is how I used it.
Line 183 “Some Cost” is the line I used to input a bit of math to determine a cost.  That line along with 133 could be removed if so desired.
The script is designed to log into a remote domain or work group, if this wasn’t the case it could be converted into a much simpler/smaller script.


#=====================================================================================================#
#    Copyright 2011 Robert Stacks
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#=====================================================================================================#
# Author: Robert Stacks
# URL: RandomTechMinutia.wordpress.com
# Date: 7/31/2012
# Updated: 7/31/2011
# Version: 0.1
#
# Purpose:
# Scan and report on storage used on a list of servers.  Can also be used to calculate costs if the
# Drive usage goes over a given amount.  I used 20GB in this example.
#=====================================================================================================#

#Add Snapins
Add-PSSnapin "Quest.ActiveRoles.ADManagement" -ErrorAction SilentlyContinue

#Get todays Date
$date = Get-Date

#===========================#
#User Adjustable Variables  #
#===========================#

#Mail Server Variables
$SMTPserver = "mail.yourdomain.com"
$from = "ScriptReporter@Yourdomain.com"
$to = "adminteam@yourdomain.com"
$EmailSubject = "Drive Space Report"

#Account with rights to remotely log into domain
$AdminAcct = 'domain\admin'
$Adminpwd = 'password'

#Domain to Log into
$domain = 'Remotedomain'

#OU container to get list of Servers to check
$OUStoSearch= 'Yourremotedomain/OU'

#Alert if Drive Space Goes over this amount figured in GB
$DriveCeiling = 20

#===========================#
#Main Script                #
#===========================#

#Set User account to be used to log into cross site domain without AD Trust
$username = $AdminAcct
$password = ConvertTo-SecureString $Adminpwd -AsPlainText -Force
$livecrd = New-Object System.Management.Automation.PSCredential $username, $password

# Format html report
$htmlReport = @"
<style type='text/css'>
.heading {
color:#0B1ABF;
font-size:14.0pt;
font-weight:700;
font-family:Verdana, sans-serif;
text-align:left;
vertical-align:middle;
height:20.0pt;
width:416pt;
}
.colnames {
color:white;
font-size:10.0pt;
font-weight:700;
font-family:Tahoma, sans-serif;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#730000;
}
.lblueback {
color:windowtext;
font-size:10.0pt;
font-family:Arial;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#B2E6FF;
}
.greyback {
color:windowtext;
font-size:10.0pt;
font-family:Arial;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#EAEAEA;
}
.charge {
color:#730000;
font-size:10.0pt;
font-family:Arial;
font-weight:bold;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:#FFFF00;
}

</style>
<table border=.5 cellpadding=.5 cellspacing=.5 width=556
style='border-collapse:collapse;table-layout:auto;width:600pt'>
<tr style='height:15.0pt'>
<th colspan=6 height=40 width=600>
<center>Drive Space Report</center></th>
</tr>
<tr>
<th>Server Name</th>
<th>Drive</th>
<th>Size(GB)</th>
<th>Used(GB)</th>
<th>Freespace(GB)</th>
<th>Over Amount (15GB)</th>
<th>Amount to Charge</th>
</tr>
"@

#Connect to the specific domain
Connect-QADService -Service $domain -Credential $livecrd

#Get a LIst of Servers from a OU
$servers=get-qadcomputer -SearchRoot $OUStoSearch | foreach { $_.Name} | Sort

#counter to manage alternating row color
$colorcounter = 0

foreach ($server in $servers)
{

#If loop to determine color of Rows related each system.
if ($colorcounter%2 -eq 0){
$tablecell = @"
<td class='lblueback'>
"@
}
else {
$tablecell = @"
<td class='greyback'>
"@
}
#format the server name with its FQDN
$server=$server + '.' + $domain
# Get a list of Hard Drives
$DISKS=Get-WmiObject Win32_LogicalDisk -computerName $server -Credential $livecrd -Filter "DriveType = 3"

# For each disk print out its stats
foreach ($DISK in $DISKS)
{
$htmlReport=$htmlReport +
"<tr>" + $tablecell + $server + "</td>" +
$tablecell + ($DISK.DeviceID) + "</td>" +
$tablecell + [Math]::round(($DISK.size/1gb),2) + "</td>" +
$tablecell + [Math]::round((($DISK.size-$DISK.freespace)/1gb),2) + "</td>" +
$tablecell + [Math]::round(($DISK.freespace/1gb),2) + "</td>"

#Determine if more that X GB is being used.
$GBOver = (($DISK.size-$DISK.freespace)/1gb) - $DriveCeiling

#If more than X GB is being used we change the color of the table and calulate the cost
if($GBOver -gt 0 )
{
#You can change the item "Some Cost" to a actual formual to determine a cost if you so desire.
$htmlReport=$htmlReport +
"<td class='charge'>" + [Math]::round($GBOver,2) + "</td>" +
"<td class='charge'>" + "$" + "Some Cost" + "</td></tr>"
}

#If they are not using more than X GB then we just report a zero and no charge
else
{
$htmlReport=$htmlReport +
$tablecell + "0" + "</td>" +
$tablecell + "No Charge" + "</td></tr>"

}

}
# value used in conjuction with if loop to rotate color in table
$colorcounter = $colorcounter + 1
}

$htmlReport = $htmlReport + "</table>"

# Send email
$subject = $EmailSubject + $date
$emailbody = $htmlReport
$mailer = New-Object Net.Mail.SMTPclient($SMTPserver)
$msg = New-Object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
$msg.IsBodyHTML = $true
$mailer.send($msg)

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: