我试图制作一个PowerShell脚本,可以通过Exchange Online REST API修改Exchange Online用户。我需要通过API设置标题,城市,部门和经理字段。根据交换在线文档,联系对象具有我想设置的所有必填字段。但是,它看起来像API不允许我对用户杠杆进行更改。这有点令人困惑。通过Exchange Online REST API修改用户
如果我尝试访问端点的用户我得到错误:
Invoke-RestMethod : {"error":{"code":"ErrorAccessDenied","message":"Access is denied. Check credentials and try again."}}
但是我设置在Azure中的Active Directory应用程序的所有权限。
这里是脚本,我使用:
Add-Type -Path ".\Microsoft.IdentityModel.Clients.ActiveDirectory.dll";
$clientId = "<<Application Client ID>>";
$certFileFullName = "<<Path to certificate file>>";
$certFilePassword = "<<Password to certificate file>>";
$tenantId = "<<Tenant ID>>";
$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("https://login.windows.net/$tenantId/oauth2/authorize", $false);
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 ($certFileFullName, $certFilePassword, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet);
$clientAssertionCertificate = new-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate($clientId, $cert);
$authenticationResult = $authContext.AcquireToken("https://outlook.office365.com", $clientAssertionCertificate);
$token = $authenticationResult.AccessToken;
$headers = @{
"Authorization" = ("Bearer {0}" -f $token);
"User-Agent" = "SyncTool/0.1PowerShell";
"client-request-id" = [System.Guid]::NewGuid().ToString();
"Date" = Get-Date -Format r;
"Accept" = "application/json";
}
Invoke-RestMethod -Method Get -Uri "https://outlook.office365.com/api/v1.0/Users" -Headers $headers;
是否有人做到这一点通过Exchange Online的REST API?它甚至有可能吗?
我该如何开发一个守护进程应用程序,它使用App-Only AAD身份验证来管理Exchange Online用户?
-Dmitry
其一,PowerShell不会在每行的结尾需要分号,所以你可以在这方面的清理你的代码第一。唯一需要分号的地方就在你的散列表中($ headers @ {...}) – FoxDeploy
我发现MS已经发布了一些新的API文档。你看过吗?你的一些代码似乎让人想起了旧事物的做法,这在键盘上完全是一种痛苦。 https://msdn.microsoft.com/Library/Azure/Ad/Graph/api/users-operations#AssignUsersManager – FoxDeploy
我不知道你是否知道,但有一个Azure AD模块可用于这些操作。这将整个过程减少到四到五行代码,并且完全不需要REST。 – FoxDeploy