通过Exchange EWS API和PowerShell导入ICS日历

时间:2017-01-05 16:16:31

标签: powershell exchange-server exchangewebservices icalendar exchange-server-2016

我正在将邮件帐户从zimbra迁移到2016年交换。邮件和联系人不是一个大问题,可以使用简单的脚本完成。

但是,当我想通过脚本导入日历时,日历会有点问题。 Exchange OWA界面具有从文件导入的日历。如果每个用户从.ics文件导入他自己的日历,则导入几乎完美。但是,通过网络界面导入很多日历并不是一个可行的选择。

所以我编写了一个powershell脚本,用于解析ics文件并通过EWS API导入所有日历事件。出现的问题是,对于每个事件,邮箱的所有者将被设置为事件的组织者。(通过Web界面导入时不会发生这种情况)

我可以使用用户的凭据或使用adm帐户进行模拟。结果是一样的。 我无法在导入之前更改组织器,因为该变量是只读的。

有没有办法从CLI或通过脚本使用webinterface导入功能?或者有没有办法设置组织者变量?

这是我写的剧本。

提前致谢

$EWSManagedApiPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
if (!(Get-Item -Path $EWSManagedApiPath -ErrorAction SilentlyContinue))
{
    throw "EWS Managed API could not be found at $($EWSManagedApiPath)."
}
[void][Reflection.Assembly]::LoadFile($EWSManagedApiPath)

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$logfile = $scriptPath + '\importAllIcsCalendars.log'
if(Test-Path $logfile) {
    # logfile exists
} else {
    $null = New-Item $logfile -type file -force
}

function LogWrite {
    Param ([string]$logString)

    Add-content $logfile -value "$logString"
}

$Username = "USER"
$Password = "PASSWD"
$EwsUrl = "https://exchange.domain.com/ews/exchange.asmx"

Copy-Item \\host\zimbra\*.ics \\host\zimbra\ICS
$ICSpath="\\host\zimbra\ICS"
$ICSlist = get-childitem $ICSPath

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016)
$service.Credentials = New-Object  Microsoft.Exchange.WebServices.Data.WebCredentials($Username,$Password)
$service.URL = New-Object Uri($EwsUrl)
$CalendarFolder = [Microsoft.Exchange.WebServices.Data.CalendarFolder]::Bind($service, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar)

#LogWrite ('User: ' + $EmailAddress) 
#Write-Host $EmailAddress

#Read ICS Files
Foreach ($i in $ICSList) {
    $filename= $i.Name
    $EmailAddress = $filename.Split("##")[0]
    $Address = $EmailAddress + "@domain.com"
    $CalendarName = $filename.Split("##")[2]
    $CalendarName = $CalendarName.substring(0,$CalendarName.ToString().Length-4)
    #$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $Address)

    $FullName = $i.FullName
    $j = Get-Content $FullName



    #Read VCAL Prefix
    Foreach ($line in $j ){
        if ($flag) {
            $calbegin = $calbegin + $line + "`r`n"
            #Write-Host $line
        }
        switch ($line) {
            "BEGIN:VCALENDAR" {
                $flag = $true
                $calbegin = "BEGIN:VCALENDAR`r`n"
            }
            "BEGIN:VEVENT" {
                $flag = $false
                break
            }
        }
    }
    #Write-Host "======================================="

    $flag = $false
    $organizer = ""

    Foreach ($line in $j ){

        if ($flag) {
            $calEvent = $calEvent + $line + "`r`n"
            #Write-Host $line
        }
        if ($line -eq "BEGIN:VEVENT" ) {
            $flag = $true
            #Write-Host $line + "BEGIN #############"
        }
        if ($line -match "ORGANIZER;\.*"){
            #Write-Host "Organizer: " $line
            $organizer = $line.Split(";")[1]
            #Write-Host "cut "$organizer
        }
        if ($line -eq "END:VEVENT" ) {
            $flag = $false
            $calEvent = $calEvent + "END:VCALENDAR" 

            $enc = [system.Text.Encoding]::UTF8
            $data1 = $enc.GetBytes($calEvent) 

            #Write-Host $calEvent
            #LogWrite ($calEvent)
            #Logwrite ("################################")

            $Appointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment($service)
            $Appointment.MimeContent = New-Object Microsoft.Exchange.WebServices.Data.MimeContent("UTF-8", $data1)
            #$Appointment.Organizer = $organizer
            $Appointment.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar)

            Write-Host $calEvent

            $calEvent = $calbegin
            Write-Host "####################"
        }
    }
    $calbegin = ""
}

0 个答案:

没有答案