Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires -Version 2.0
- <#
- .SYNOPSIS
- Push exchange 2007 (or newer) mailbox statistics to a Spiceworks server for reporting and dashboard widgets.
- .DESCRIPTION
- This script gathers Exchange mailbox statistics using the get-MailboxStatistics cmdlet and converts this data to a CSV
- table and sends it to a spiceworks server using an HTTP POST request. The script requires a Spiceworks and Exchange
- server name to be specified using the 'Server' and 'ExchangeServer' parameter, respectively.
- .PARAMETER Test
- Perform a dry run of the script with results going to stdout onscreen instead of pushing to the spiceworks server.
- .PARAMETER SSL
- Use HTTPS to communicate with the Spiceworks server.
- .PARAMETER Port
- Specify the destination port to send the push data to (default for HTTP is 80, 443 for HTTPS - note that you need to specify SSL if using HTTPS).
- .PARAMETER ID
- Specify the ID that Spiceworks uses to track this server. This is normally generated by the script and should be left alone.
- .PARAMETER ExchangeServer
- The name of the Exchange server to query.
- .PARAMETER MailDatabase
- The Exchange database to query. If no database is specified, all mail databases on the Exchange server will be queried.
- .PARAMETER File
- Read data from a file instead of the Exchange database (for troubleshooting).
- .LINK
- http://community.spiceworks.com
- .NOTES
- Requires PowerShell 2.0 and Exchange 2007 SP2 (at least).
- Copyright 2006-10 Spiceworks, Inc. All Rights Reserved. http://www.spiceworks.com
- Spiceworks versions: 7.4
- Creation date: 4/10/2010
- Last modified: 7/16/2015 - Rob Dunn
- .EXAMPLE
- Test data extract against Exchange server named 'Mail-001.' Output is shown on screen.
- PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Test -ExchangeServer Mail-001
- .EXAMPLE
- Push mailbox statistics from Mail-001 to spiceworksserver.mydomain.com over port 80.
- PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Port 80 -ExchangeServer Mail-001
- .EXAMPLE
- Push mailbox statistics from Mail-001 to spiceworksserver.mydomain.com over port 443 using SSL.
- PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Port 443 -SSL -ExchangeServer Mail-001
- .EXAMPLE
- Push mailbox statistics from Mail-001 to spiceworksserver.mydomain.com over port 9001 using SSL.
- PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Port 9001 -SSL -ExchangeServer Mail-001
- #>
- [CmdletBinding()]
- param(
- [parameter(Mandatory=$true)][String]$Server,
- [Int]$Port = 80,
- [String]$Id = $null,
- [switch]$SSL,
- [parameter(Mandatory=$true)][String]$ExchangeServer = $null,
- [String]$MailDatabase = $null,
- [Switch]$Test,
- [String]$File
- )
- Begin {
- }
- Process {
- $Items = @()
- # 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-MachineID {
- 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-ExchangeTarget {
- if ($ExchangeServer) {
- return "-Server $ExchangeServer"
- }
- if ($MailDatabase) {
- return "-Database $MailDatabase"
- }
- return $nil
- }
- $Spiceworks_Version = '4.7'
- $AssetID = (Get-MachineID).trim()
- $Return_Type = 'MicrosoftExchangeMailbox'
- $Exchange_Target = Get-ExchangeTarget
- $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"
- $Properties = @{
- AssetID = $AssetID
- Version = $Spiceworks_Version
- SSL = $SSL
- Port = $Port
- ExchangeServer = $ExchangeServer
- ExchangeDatabase = $MailDatabase
- }
- # Load the Exchange Snapin and query for mailbox statistics
- Write-Verbose "Script parameters:"
- Write-Verbose $Properties
- $Assetname = (Get-WmiObject win32_ComputerSystem -ComputerName $ExchangeServer).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) {
- Write-Output "Importing data from CSV file: $File"
- $Results = Import-Csv $file
- } Else {
- Write-Output "Executing query: `n`n$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 trace conditions for that error
- $Trace = "trace=Exception encountered on $Assetname - " + $Error[0]
- $Return_Data = $Trace
- } ElseIf ($Results.length -eq "0") {
- Write-Warning "No data retrieved"
- } Else {
- Write-Verbose "Mailboxes found: $($Results.length)"
- $Return_Data = "data=" + ($Results | ConvertTo-CSV -NoTypeInformation | out-string)
- }
- } Catch {
- Write-Warning "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"
- }
- #Write-Verbose $URI
- }
- End {
- If ($Test) {
- Write-Output "--- DRY RUN: The following would normally be posted to spiceworks ---"
- Write-Output "URL: $uri"
- Write-Output "BODY: $return_data"
- } Else {
- $wc = New-Object net.WebClient;
- $wc.uploadString($URI, $Return_Data);
- $wc.close
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement