Microsoft Graph API/Powershell:如何通过 Powershell 使用 Graph 创建多个 PUTS?

Microsoft GraphAPI / Powershell: How do i create multiple PUTS with Graph over Powershell?

我基本上是 GraphAPI 的新手(和一般的“编码”),我有一个任务需要完成。我需要通过 Powershell 在其他用户日历上添加多个事件。我已经设法将一个事件输入到一个用户日历中。但是我无法通过一个事件并添加更多事件...我确实阅读了有关使用 GraphAPI 进行批处理的信息,但我不明白。

 #Variables
$ClientID = "xxx"
$ClientSecret = "xxx"
$tenantID = "xxx"
$scope = "https://graph.microsoft.com/.default"
$authority = "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token"

$Body = @{
  "grant_type"    = "client_credentials";
  "client_id"     = "$ClientID";
  "client_secret" = "$ClientSecret";
  "scope"      = "$scope";
}

#Get AccessToken
$result = Invoke-RestMethod -Method POST -uri $authority -Body $body
$AccessToken = $result.access_token

#Create Event
$mailbox = "user@domain.com"
$headers = @{
 "Authorization" = "Bearer "+ $AccessToken;
 "Prefer" = 'outlook.timezone="W. Europe Standard Time"';
 "Content-type"  = "application/json"
}

#Create JSON Object
$json = @"
{
  "subject":"Event over Graph API",
  "body": {
    "contentType" : "HTML",
    "content" : "Write Graph API Powershell Script"
  },
  "start": {
      "dateTime" : "2021-07-23T00:00:00",
      "timeZone" : "W. Europe Standard Time"
  },
  "end": {
      "dateTime" : "2021-07-24T00:00:00",
      "timeZone" : "W. Europe Standard Time"
  },
  "location":{
      "displayName" : "HomeOffice"
  }
}
"@

$uri = "https://graph.microsoft.com/v1.0/users/$Mailbox/calendar/events"
Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $json 

我刚刚在网上找到这个并进行了测试,S/O 给创作者。

提前感谢您的帮助或意见。

Powershell Function 的帮助下,您可以多次调用特定的代码段 - 使其与动态对象一起运行,即 - 同一段代码对不同的对象值进行操作。

您创建了一个函数 update-userevent 并定义了根据已作为参数传递的邮件地址来阻止用户事件。

代码片段

#The below piece will be global. 
#Variables
$ClientID = "xxx"
$ClientSecret = "xxx"
$tenantID = "xxx"
$scope = "https://graph.microsoft.com/.default"
$authority = "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token"

$Body = @{
  "grant_type"    = "client_credentials";
  "client_id"     = "$ClientID";
  "client_secret" = "$ClientSecret";
  "scope"      = "$scope";
}

#Get AccessToken
$result = Invoke-RestMethod -Method POST -uri $authority -Body $body
$AccessToken = $result.access_token
$Accesstoken 


$headers = @{
 "Authorization" = "Bearer "+ $AccessToken;
 "Prefer" = 'outlook.timezone="W. Europe Standard Time"';
 "Content-type"  = "application/json"
}

#making it as a function to invoke it multiple times.
function update-userevent($mailbox)
{
#Create Event
$mailbox = "user@domain.com"
#Create JSON Object
$json = @"
{
  "subject":"Event over Graph API",
  "body": {
    "contentType" : "HTML",
    "content" : "Write Graph API Powershell Script"
  },
  "start": {
      "dateTime" : "2021-07-23T00:00:00",
      "timeZone" : "W. Europe Standard Time"
  },
  "end": {
      "dateTime" : "2021-07-24T00:00:00",
      "timeZone" : "W. Europe Standard Time"
  },
  "location":{
      "displayName" : "HomeOffice"
  }
}
"@

$uri = "https://graph.microsoft.com/v1.0/users/$Mailbox/calendar/events"
Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $json

}


#getting the users from Graph...
#if you are getting the mail address from a predefined list you could ignore the 
#below 
$users = Invoke-webRequest 'https://graph.microsoft.com/v1.0/users' -Headers $headers
#converting the response to JSON
$json= $users.Content | ConvertFrom-Json
$mails = $json.value.mail #read from the predefined list
#iterating through each mailbox
foreach ($mail in $mails)
{
#calling through the above function.
update-userevent -mailbox $mail
}