Advertisement
maximillianx

Start-ExchangePush.ps1

Jul 17th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -Version 2.0
  2. <#
  3. .SYNOPSIS
  4.     Push exchange 2007 (or newer) mailbox statistics to a Spiceworks server for reporting and dashboard widgets.
  5. .DESCRIPTION
  6.     This script gathers Exchange mailbox statistics using the get-MailboxStatistics cmdlet and converts this data to a CSV
  7.     table and sends it to a spiceworks server using an HTTP POST request.  The script requires a Spiceworks and Exchange
  8.     server name to be specified using the 'Server' and 'ExchangeServer' parameter, respectively.
  9. .PARAMETER Test
  10.     Perform a dry run of the script with results going to stdout onscreen instead of pushing to the spiceworks server.
  11. .PARAMETER SSL
  12.     Use HTTPS to communicate with the Spiceworks server.
  13. .PARAMETER Port
  14.     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).
  15. .PARAMETER ID
  16.     Specify the ID that Spiceworks uses to track this server.  This is normally generated by the script and should be left alone.
  17. .PARAMETER ExchangeServer
  18.     The name of the Exchange server to query.
  19. .PARAMETER MailDatabase
  20.     The Exchange database to query.  If no database is specified, all mail databases on the Exchange server will be queried.
  21. .PARAMETER File
  22.     Read data from a file instead of the Exchange database (for troubleshooting).
  23. .LINK
  24.     http://community.spiceworks.com
  25. .NOTES
  26.     Requires PowerShell 2.0 and Exchange 2007 SP2 (at least).
  27.  
  28.     Copyright 2006-10 Spiceworks, Inc.  All Rights Reserved.  http://www.spiceworks.com
  29.     Spiceworks versions: 7.4
  30.     Creation date:  4/10/2010
  31.     Last modified:  7/16/2015 - Rob Dunn
  32. .EXAMPLE
  33.     Test data extract against Exchange server named 'Mail-001.'  Output is shown on screen.
  34.  
  35.     PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Test -ExchangeServer Mail-001
  36. .EXAMPLE
  37.     Push mailbox statistics from Mail-001 to spiceworksserver.mydomain.com over port 80.
  38.  
  39.     PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Port 80 -ExchangeServer Mail-001
  40. .EXAMPLE
  41.     Push mailbox statistics from Mail-001 to spiceworksserver.mydomain.com over port 443 using SSL.
  42.  
  43.     PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Port 443 -SSL -ExchangeServer Mail-001
  44. .EXAMPLE
  45.     Push mailbox statistics from Mail-001 to spiceworksserver.mydomain.com over port 9001 using SSL.
  46.  
  47.     PS C:\> .\Start-ExchangePush.ps1 -Server spiceworksserver.mydomain.com -Port 9001 -SSL -ExchangeServer Mail-001
  48. #>
  49. [CmdletBinding()]
  50. param(
  51.     [parameter(Mandatory=$true)][String]$Server,
  52.     [Int]$Port = 80,
  53.     [String]$Id = $null,
  54.     [switch]$SSL,
  55.     [parameter(Mandatory=$true)][String]$ExchangeServer = $null,
  56.     [String]$MailDatabase = $null,
  57.     [Switch]$Test,
  58.     [String]$File
  59. )
  60.  
  61. Begin {
  62. }
  63.  
  64. Process {
  65.     $Items = @()
  66.  
  67.     # Take a shot at figuring out an ID for this windows server. spiceworks will not accept this data
  68.     # without knowing the server. The actual spiceworks ID generation algorithm is a bit more complex
  69.     Function Get-MachineID {
  70.       if ($Id) {return $Id}
  71.       $Id = (Get-WmiObject Win32_ComputerSystemProduct).IdentifyingNumber
  72.       if ($Id) {return $Id}
  73.       $Id = (Get-WmiObject Win32_SystemEnclosure).SerialNumber
  74.       if ($Id) {return $Id}
  75.       $Id = (Get-WmiObject Win32_OperatingSystem).CSName
  76.       if ($Id) {return $Id}
  77.  
  78.     }
  79.  
  80.     # Might need an exchange identifier for the cmdlet to run correctly or just to segregate data
  81.     Function Get-ExchangeTarget {
  82.       if ($ExchangeServer) {
  83.         return "-Server $ExchangeServer"
  84.       }
  85.       if ($MailDatabase) {
  86.         return "-Database $MailDatabase"
  87.       }
  88.       return $nil
  89.     }
  90.  
  91.     $Spiceworks_Version = '4.7'
  92.     $AssetID            = (Get-MachineID).trim()
  93.     $Return_Type        = 'MicrosoftExchangeMailbox'
  94.     $Exchange_Target    = Get-ExchangeTarget
  95.  
  96.     $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"
  97.    
  98.     $Properties = @{
  99.         AssetID = $AssetID
  100.         Version = $Spiceworks_Version
  101.         SSL = $SSL
  102.         Port = $Port
  103.         ExchangeServer = $ExchangeServer
  104.         ExchangeDatabase = $MailDatabase
  105.     }
  106.  
  107.     # Load the Exchange Snapin and query for mailbox statistics
  108.  
  109.     Write-Verbose "Script parameters:"
  110.     Write-Verbose $Properties
  111.  
  112.     $Assetname = (Get-WmiObject win32_ComputerSystem -ComputerName $ExchangeServer).Name
  113.  
  114.     add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue; #2007
  115.     add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue; #2010
  116.  
  117.     [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null # load urlEncode
  118.  
  119.     $Return_Data = $Trace = $ErrMsg = ""
  120.  
  121.     Try {
  122.       If ($File) {
  123.         Write-Output "Importing data from CSV file: $File"
  124.         $Results = Import-Csv $file
  125.       } Else {
  126.         Write-Output "Executing query: `n`n$Query"
  127.         $Results = Invoke-Expression $Query  
  128.    
  129.       }
  130.       If (!($Results)) {
  131.         # Cannot rely on the try block to catch exchange cmdlet invocation errors like invalid parameters.
  132.         # Possibly because it's a 1.0 cmdlet. If the results are null it's very likely an error so grab
  133.         # the last error off the stack and hope there are no trace conditions for that error
  134.         $Trace = "trace=Exception encountered on $Assetname - " + $Error[0]
  135.         $Return_Data = $Trace
  136.       } ElseIf ($Results.length -eq "0") {
  137.         Write-Warning "No data retrieved"
  138.       } Else {
  139.         Write-Verbose "Mailboxes found: $($Results.length)"
  140.         $Return_Data = "data=" + ($Results | ConvertTo-CSV -NoTypeInformation | out-string)
  141.       }
  142.     } Catch {
  143.         Write-Warning "Error: $_"
  144.         $Trace = $Return_Data = "trace=Exception encountered on $Assetname - " + $_
  145.         $Return_Data = $Trace
  146.     }
  147.  
  148.     If ($Port -eq $nil) {
  149.         If ($SSL) {
  150.             $Port = "443"
  151.         } else {
  152.             $Port = "80"
  153.         }
  154.     }
  155.  
  156.     # form HTTP post request and execute it
  157.     $Http = "http"
  158.     If ($SSL) {
  159.         $Http="https"
  160.     }
  161.     $Base_Uri = $Http + "://" + $Server + ":" + "$Port"
  162.  
  163.     If ($trace) {
  164.         $URI = $Base_Uri + "/dataloader/create_exception?assetUUID=$AssetID&spiceworks_version=$Spiceworks_Version"
  165.            
  166.     } Else {
  167.         $URI = $Base_Uri + "/dataloader/loaddata?assetUUID=$AssetID&dataType=$Return_Type&assetName=$Assetname&version=$Spiceworks_Version&is_csv=true"
  168.     }
  169.  
  170.     #Write-Verbose $URI
  171. }
  172.  
  173. End {
  174.     If ($Test) {
  175.         Write-Output "--- DRY RUN: The following would normally be posted to spiceworks ---"
  176.         Write-Output "URL: $uri"
  177.         Write-Output "BODY: $return_data"
  178.     } Else {
  179.         $wc = New-Object net.WebClient;
  180.         $wc.uploadString($URI, $Return_Data);
  181.         $wc.close
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement