微软365年档案图片进行交互

微软365档案图片交互同步

使用PowerShell 365年从微软下载档案图片,然后上传交互。脚本可以使用Windows任务调度器调度运行合适的间隔。

先决条件

  1. 源配置文件交互。见文章一般资料来源
  2. 下载并安装ExchangeOnlineManagement PowerShell包:
    https://www.powershellgallery.com/packages/ExchangeOnlineManagement/2.0.5
  3. 运行以下命令在PowerShell
    Import-Module ExchangeOnlineManagement

您可能需要设置PowerShell执行政策RemoteSigned或无限制使用以下命令Set-ExecutionPolicy RemoteSignedSet-ExecutionPolicy无限制的

365年微软权限

  1. 在365年微软全球管理员或管理员角色交换
  2. 在网上交换ApplicationImpersonation作用

下载脚本

下面的脚本可以用来下载所有用户档案图片并将它们存储在这里C: \ O365 \ AllUserProfilePictures,它将上传进行交互。在这个例子中,ExternalDirectoryObjectId字段将被用于文件名,例如14 f90941 - 8 - 49艺术展d7 - 963 - b - 841962 - dea5e9.jpg。交互将使用这个GUID来将它与它在正确的用户配置文件的映射UMIID。看到这篇文章头像照片更多的例子可以用来导出的照片映射到概要文件交互,用户名为例。

#输入参数:$ folderpath = " c: \ O365 \ AllUserProfilePictures”$ logpath = " c: \ O365 \日志。txt”#连接到交换网络:$ UPN = "(电子邮件保护)密码“$密码= ConvertTo-SecureString -AsPlainText force $凭据=新对象System.Management.Automation。PSCredential (UPN,密码)美元Connect-ExchangeOnline凭证凭证#交互变量interactUrl美元= " https://example.interactgo.com " profileSourceApiKey = " 1234 " profileSourceId美元= " 1000 " # 365年从微软下载所有用户档案图片:新项目itemtype目录路径folderpath force权限美元= Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize无限| Select-Object UserPrincipalName,别名,ExternalDirectoryObjectId foreach(用户权限美元){path = Join-Path folderpath美元“$ (user.ExternalDirectoryObjectId美元). jpg "{$照片= Get-UserPhoto身份用户美元。如果照片($ UserPrincipalName -ErrorAction继续。PictureData - ne零美元){[io。文件]::WriteAllBytes(路径,photo.PictureData美元)美元消息= " $ (user.UserPrincipalName美元)配置文件下载”写输出美元消息添加内容路径logpath价值美元其他消息}{$消息= " $ (user.UserPrincipalName美元)没有证件照”写输出美元消息添加内容路径logpath价值美元消息}}抓住{$ errorMessage =“错误下载配置文件照片$ (user.UserPrincipalName美元):$ (_.Exception.Message美元)"Write-Error $errorMessage Add-Content -Path $logpath -Value $errorMessage } } #UPLOAD TO INTERACT Add-Type -AssemblyName System.Net.Http [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 function ProcessFiles(){ Get-ChildItem $folderpath | Foreach-Object { $personId = $_.BaseName $baseUri = $interactUrl + "/api/umi/" + $profileSourceId + "/upload/umiid/" + $personId + "/picture" SendFile $baseUri $_.FullName } } function SendFile([string]$uri, [string]$path){ $httpClientHandler = New-Object System.Net.Http.HttpClientHandler $httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler $packageFileStream = New-Object System.IO.FileStream @($path, [System.IO.FileMode]::Open) $contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data" $contentDispositionHeaderValue.Name = "fileData" $contentDispositionHeaderValue.FileName = (Split-Path $path -leaf) $streamContent = New-Object System.Net.Http.StreamContent $packageFileStream $streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue $streamContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue "image/jpeg" $content = New-Object System.Net.Http.MultipartFormDataContent $content.Add($streamContent) $httpClient.DefaultRequestHeaders.Add("X-ApiKey", $profileSourceApiKey) try { $response = $httpClient.PostAsync($Uri, $content).Result } catch [Exception] { # log it } finally { if($null -ne $httpClient) { $httpClient.Dispose() } if($null -ne $response) { $response.Dispose() } } } ProcessFiles

Baidu
map