PowerShell-使用Exchange Server 2016 REST API获取日历事件

时间:2019-03-13 12:08:40

标签: rest powershell exchange-server

我具有使用REST API的资源邮箱的脚本日历项-但在Exchange Server 2016中遇到此错误-不确定是什么问题-我的帐户具有应用程序模拟角色。

错误消息:

 Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:2 char:28
+ ...              $events = Invoke-RestMethod -Uri $uri -Credential $creds
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

脚本:

$Username = 'Contoso\administrator'
$Password = 'xxxxxxx'
$SecureString = ConvertTo-SecureString -AsPlainText $Password -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString 
$mtgroom = @("meeting@contoso.com") 
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$start = (get-date).ToShortDateString()
$end = (get-date).ToShortDateString()
$uri = "https://mail.contoso.com/api/v2.0/users/$mtgroom/calendar/calendarView?startDateTime=$($start)T08:00:00&endDateTime=$($end)T19:30:00"
 $events = Invoke-RestMethod -Uri $uri -Credential $creds

1 个答案:

答案 0 :(得分:0)

我认为这很可能是因为日期时间字符串的形成方式。在我的环境中,我使用(get-date).ToShortDateString()得到了“ 3/13/2019”。这可能会导致网址格式错误。根据您的本地化设置,我可能是错的。

您从get-date -Format "MM-dd-yyyy"的开始日期和结束日期获得更好的结果吗?


docs.microsoft.com中的示例也使用带连字符的日期

GET https://outlook.office.com/api/v2.0/me/calendarview?startDateTime=2014-10-01T01:00:00&endDateTime=2014-10-31T23:00:00&$select=Subject