#Requires -Version 2.0 # Copyright 2006-10 Spiceworks, Inc. All Rights Reserved. http://www.spiceworks.com
# Spiceworks versions: 4.7
# Creation date: 4/10/2010
# Last modified: 8/16/2010
<#
.SYNOPSIS
Push exchange 2007 mailbox statistics to a spiceworks server
.DESCRIPTION
This script gathers Exchange mailbox statistics using the getMailboxStatistics
cmdlet, converts this data to a CSV table and sends it up to a spiceworks server
using an HTTP POST request. The script requires a spiceworks server to be
specified. Other options available:
-test - do a dry run of the script with results going to stdout instead
of being pushed to a spiceworks server
-ssl - Use HTTPS to communicate with a spiceworks server
-port - specify a port (defaults to 80 for HTTP and 443 for HTTPS)
-id - specify the id that spiceworks uses to track this server. This
is normally generated by the script and should be left alone.
-exchangeServer - specify an exchange server to query
-exchangeDatabase - specify an exchange database to query
-file - read data from a file instead of the exchange database (for troubleshooting)
Requires: powershell 2.0 and Exchange Server 2007 sp2.
See: "help get-mailboxstatistics -detailed" for valid exchange server/db entries
.EXAMPLE
exchange_push cumen.myorg.com -test
exchange_push cumen.myorg.com -port 80
exchange_push cumen.myorg.com -port 443 -ssl
exchange_push cumen.myorg.com -port 9001 -ssl -exchangeServer myexchange
#>
param([String]$server, [int]$port="80", [String]$id=$null, [switch]$ssl, [String]$exchangeServer=$null, [String]$exchangeDatabase=$null, [switch]$test,
[string]$file)
if (!$server) {
echo "Format: exchange_push.ps1 server [-port port_number] [-ssl] [-spiceid machineID] [-exchangeServer exchange_server | -exchangeDatabase
exchange_database] [-test][-file filename]"
exit
}
# Take a shot at figuring out an ID for this windows server. spiceworks will not accept this data
# without knowing the server. The actual spiceworks ID generation algorithm is a bit more complex
function get_machine_id {
if ($id) {return $id}
$id = (Get-WmiObject Win32_ComputerSystemProduct).IdentifyingNumber
if ($id) {return $id}
$id = (Get-WmiObject Win32_SystemEnclosure).SerialNumber
if ($id) {return $id}
$id = (Get-WmiObject Win32_OperatingSystem).CSName
if ($id) {return $id}
}
# Might need an exchange identifier for the cmdlet to run correctly or just to segregate data
function get_exchange_target {
if ($exchangeServer) {return "-Server $exchangeServer"}
if ($exchangeDatabase) {return "-Database $exchangeDatabase"}
return $nil
}
$spiceworks_version = '4.7'
$assetid = (get_machine_id).trim()
$return_type = 'MicrosoftExchangeMailbox'
$exchange_target = (get_exchange_target)
$query = "get-mailboxstatistics $exchange_target|add-member -MemberType ScriptProperty -Name size -Value {`$this.totalitemsize.value.ToBytes()} -PassThru|
select-object DisplayName,ItemCount,size,LastLoggedOnUserAccount,StorageGroupName,DatabaseName"
# Load the exchange snapin and query for mailbox statistics
echo "script parameters: assetid=$assetid,version=$spiceworks_version,ssl=$ssl,port=$port,exchangeServer=$exchangeServer,exchangeDatabase=$exchangeDatabase"
$assetname = (Get-WmiObject win32_ComputerSystem).Name
add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue; #2007
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue; #2010
[System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null # load urlEncode
$return_data = $trace = $errmsg = ""
try {
if ($file) {
echo "Importing data from CSV file: $file"
$results = Import-Csv $file
} else {
echo "Executing query: $query"
$results = invoke-expression $query
}
if (!($results)) {
# Cannot rely on the try block to catch exchange cmdlet invocation errors like invalid parameters.
# Possibly because it's a 1.0 cmdlet. If the results are null it's very likely an error so grab
# the last error off the stack and hope there are no race conditions for that error
$trace = "trace=Exception encountered on $assetname - " + $error[0]
$return_data = $trace
} elseif ($results.length -eq "0") {
echo "No data retrieved"
} else {
echo "Mailboxes found: $($results.length)"
$return_data = "data=" + ($results|ConvertTo-CSV -NoTypeInformation|out-string)
}
} catch {
echo "Error: $_"
$trace = $return_data = "trace=Exception encountered on $assetname - " + $_
$return_data = $trace
}
if ($port -eq $nil) {
if ($ssl) { $port = "443" } else { $port = "80" }
}
# form HTTP post request and execute it
$http = "http"
if ($ssl) { $http="https" }
$base_uri = $http + "://" + $server + ":" + "$port"
if ($trace) {
$uri = $base_uri + "/dataloader/create_exception?assetUUID=$assetid&spiceworks_version=$spiceworks_version"
} else {
$uri = $base_uri + "/dataloader/loaddata?assetUUID=$assetid&dataType=$return_type&assetName=$assetname&version=$spiceworks_version&is_csv=true"
}
if ($test) {
echo "--- DRY RUN: The following would normally be posted to spiceworks ---"
echo "URL: $uri"
echo "BODY: $return_data"
} else {
$wc = new-object net.WebClient;
$wc.uploadString($uri, [System.Web.HttpUtility]::UrlEncode($return_data));
$wc.close
}