PowerShell監控outlook新郵件到達

PowerShell監控outlook新郵件到達(單封,多封可能要分解id(舊版需分解?新版不需要?新版就是每個郵件出發一次newmailex?看如下documents))

This event fires once for every received item that is processed by Microsoft Outlook.
The item can be one of several different item types, 
for example, **[MailItem](https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem)**, **[MeetingItem](https://docs.microsoft.com/en-us/office/vba/api/outlook.meetingitem)**, or **[SharingItem](https://docs.microsoft.com/en-us/office/vba/api/outlook.sharingitem)**. 
The *EntryIDsCollection* string contains the Entry ID that corresponds to that item. 
Note that this behavior has changed from earlier versions of the event when the *EntryIDCollection* contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired.

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
add-type -assembly "System.Runtime.Interopservices"

try{
    $outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
    $outlookWasAlreadyRunning = $true
}catch{
    try{
        $Outlook = New-Object -comobject Outlook.Application
        $outlookWasAlreadyRunning = $true
    }catch{
        write-host "You must exit Outlook first."
        exit
    }
}

$test ='abc'
$vars = Get-Variable

#Write-Host ($vars | Format-List | Out-String)

try{
    if($outlookWasAlreadyRunning){
        write-host "ok..."

        $Job = Register-ObjectEvent -InputObject $Outlook -EventName NewMailEx `
        -Action {
            #($Event,$EventSubscriber,$Sender,$SourceEventArgs,$SourceArgs)
            #$Event, $EventSubscriber, $Sender, $EventArgs, and $Args
            #$EventArgs | Format-List -Property *
            #$Args | Format-List -Property *
            #-Sender $sender
            #$test ='abc'
            #write-host "testaaa123.."
            #write-host ""
            #$outline = Get-Event
            #$vars = Get-Variable
            #write-host '$(outline)'
            #Write-Host ($Args | Format-List | Out-String)
            #Write-Host ($args | Format-List | Out-String)
            #Write-Host ($vars | Format-List | Out-String)
            #$sender | Format-List -Property *
            #Write-Host ($args | Format-List)

            $mail = $Outlook.Session.GetItemFromID($args)
            #Write-Host ($mail | Format-List | Out-String)
            Write-Host $mail.Subject
            Write-Host $mail.Body
            write-host ""
        }

        #$Job | Format-List -Property *

        while($true){
        }
    }
}finally{
   Unregister-Event -SubscriptionId $Job.Id
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章