PowerShell监控outlook新邮件到达PowerShell监控outlook新邮件到达

2021-12-06 17:41:24 浏览数 (1)

PowerShell监控outlook新邮件到达(单封,多封可能要分解id(旧版需分解?新版不需要?新版就是每个邮件出发一次newmailex?看如下documents))

代码语言:javascript复制
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.
代码语言:javascript复制
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
}

0 人点赞