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

PowerShell - Using Exchange Server 2016 REST API to get Calendar Events

我有一个使用 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

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

您从 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