0

我试图制作一个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

+0

其一,PowerShell不会在每行的结尾需要分号,所以你可以在这方面的清理你的代码第一。唯一需要分号的地方就在你的散列表中($ headers @ {...}) – FoxDeploy

+0

我发现MS已经发布了一些新的API文档。你看过吗?你的一些代码似乎让人想起了旧事物的做法,这在键盘上完全是一种痛苦。 https://msdn.microsoft.com/Library/Azure/Ad/Graph/api/users-operations#AssignUsersManager – FoxDeploy

+0

我不知道你是否知道,但有一个Azure AD模块可用于这些操作。这将整个过程减少到四到五行代码,并且完全不需要REST。 – FoxDeploy

回答

1

以下是使用PowerShell模块简单方法攻击此问题的方法。由于您已经可以访问功能强大且功能全面的PowerShell工具,因此根据我的经验,实际上没有理由通过REST API手动执行此操作。

使用MSONline模块

我们可以通过使用此模块

  • 首先,您需要设置除外经理做的一切,安装MSOnline模块。
  • 接下来,用自己的凭据替换第3行和第4行的信息。

    import-module MSOnline 
    
    $secpasswd = ConvertTo-SecureString '[email protected]!' -AsPlainText -Force 
    $credGuest = New-Object System.Management.Automation.PSCredential ('[email protected]', $secpasswd) 
    
    
    connect-msolservice -credential $credGuest 
    
    Get-MsolUser | ? DisplayName -eq 'JSamson' | 
    Set-MsolUser -Department 'Ham Research Team' -Title "Lead Pork Architect" -City "Hamburg" 
    
    Get-MsolUser | ? DisplayName -eq 'JSamson' | Select *Name,Title,City,Depar* | fl 
    
    
    
    DisplayName  : JSamson 
    FirstName   : Joe 
    LastName   : Sampson 
    SignInName  : [email protected] 
    UserPrincipalName : [email protected] 
    Title    : Lead Pork Architect 
    City    : Hamburg 
    Department  : Ham Research Team 
    

设置管理器属性

原来的经理属性不是从这个模块进行访问。不知道为什么是这样的情况。我正在为你制作这件作品的解决方案,并在完成后更新这个答案。


当没有路,使自己的

集成了一些关于这个博客帖子http://goodworkaround.com/node/73马吕斯索尔巴肯Mellum发现精彩的提示,我们有基础连接到Azure的AD。接下来,使用Azure AD Graph DLL中提供的美妙API(您可以使用nuget命令'。\ nuget.exe安装Microsoft.IdentityModel.Clients.ActiveDirectory'和Azure AD Rest API参考指南获取它,这组功能已建成。

此工具包含许多工作者函数,如Get-MSOLGraphUser和Get-MsSOLGraphUserManager。它们主要设计为由Set-MSOLGraphUserManager cmdlet本身使用,但您可以随意修改并将它们用于您的内容。

New versions of this project will live in GitHub at this url

例子:

Set-MSOLGraphUserManager -targetUser Test -targetManager Stephen 
>Successfully found user to modify PSTest 
>Successfully looked up manager Stephen Owen 
>Updating the manager 
>Verifying manager now set for PSTest 
>Getting user manager 
>PSTest's manager is now Stephen Owen           
+0

谢谢,福克斯。这种方法很好,但并不理想: 1.对于MSOL模块,您无法更改管理器。没有这样的领域。要更改管理器,您应该使用Exchange Online远程模块。 2.我不想在远程服务器上的PowerShell脚本中使用我的个人凭证。通过安全最佳实践,我需要使用有限的权限创建特殊用户。在azure中,我不能对用户做这件事,但可以用AD应用程序做。 可能是使用资源作用域权限的帮助,但我不确定。 – PyroJoke

+0

好的,我完成了我的统一管理器设置方法,完成了一些新功能供您使用。我会更新我的帖子。欲了解更多信息,请这个博客帖子:http://goodworkaround.com/node/73 – FoxDeploy

+0

另外,如果你只是想下载的cmdlet,在这里,他们是:HTTPS://github.com/1RedOne/AzureAD – FoxDeploy

1

你应该Office 365 unified API这是一个公开预览枚举或修改Azure中的Active Directory用户的信息。此API处于公开预览中,我们正在积极努力将其发布到GA。同时,如果您需要在生产环境中配置用户,请拨打Azure AD Graph API。这些是用于管理目录中用户信息的支持端点。端点https://outlook.office365.com/api允许您指定要访问哪个用户的邮箱,但不允许您枚举用户,因为这是目录功能。

+0

统一API不支持仅应用程序认证,是吗? – PyroJoke

+0

不是现在,但它是在路线图 –

相关问题