As the title says i'm trying to create a script to run another script each time I receive a new email.
Background: The script that it needs to run is a powershell script (if it could be combined that would be great) but i'm wanting this to alert operators that they have recevied an emergency email. I'm pushing a notification to the app pushover on their phones.
The powershell script that I already have made can be run and does exactly what I want it to do. Sends a request to Pushover's API and then sends the notification to the corresponding smartphones.
I have done some research online and have come up with this script but as I am newer to powershell some the language i'm confused on.
Below is the code I have so far... not sure if it is even in the right direction
$MailboxName = "email@domain.com" $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll" [void][Reflection.Assembly]::LoadFile($dllpath) $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1) $service.TraceEnabled = $false $service.Credentials = New-Object System.Net.NetworkCredential("user","password", "domain") $service.Url="https://abc.local/ews/exchange.asmx" try{ $fldArray = new-object Microsoft.Exchange.WebServices.Data.FolderId[] 1 $Inboxid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName) $fldArray[0] = $Inboxid $stmsubscription = $service.SubscribeToStreamingNotifications($fldArray, [Microsoft.Exchange.WebServices.Data.EventType]::NewMail) $stmConnection = new-object Microsoft.Exchange.WebServices.Data.StreamingSubscriptionConnection($service, 30); $stmConnection.AddSubscription($stmsubscription) Register-ObjectEvent -inputObject $stmConnection -eventName "OnNotificationEvent" -Action { foreach($notEvent in $event.SourceEventArgs.Events){ [String]$itmId = $notEvent.ItemId.UniqueId.ToString() $message = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($event.MessageData,$itmId) IF ($message.Subject -eq "execprocess"){ Start-Process "opsalert.ps1" } } } -MessageData $service }catch [Exception] { Get-Date | Out-File C:\logs\logError.txt -Append "Error : "+ $_.Exception.Message } Register-ObjectEvent -inputObject $stmConnection -eventName "OnDisconnect" -Action {$event.MessageData.Open()} -MessageData $stmConnection $stmConnection.Open()
Error codes i'm getting:
Register-ObjectEvent : Cannot bind argument to parameter 'InputObject' because it is null.At C:\home\dustin\IT\Pushover\emailmonitor.ps1:34 char:34
+ Register-ObjectEvent -inputObject <<<< $stmConnection -eventName "OnDisconnect" -Action {$event.MessageData.Open()}
-MessageData $stmConnection
+ CategoryInfo : InvalidData: (:) [Register-ObjectEvent], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RegisterObj
ectEventCommand
You cannot call a method on a null-valued expression.
At C:\home\dustin\IT\Pushover\emailmonitor.ps1:35 char:20
+ $stmConnection.Open <<<< ()
+ CategoryInfo : InvalidOperation: (Open:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Help would be very appreciated!
Thanks!
- Moved by Bill_Stewart Tuesday, November 19, 2013 4:49 AM Moving to more appropriate forum