使用Powershell Azure Functions中的exchangeonlinmanagement Powers



我一直在尝试使用Powershell Azure函数中的exchangeonlinmanagement模块。这是到目前为止的整个脚本:

using namespace System.Net
param($Request, $TriggerMetadata)
$userPrincipalName = $Request.Body.userPrincipalName
$organization = $Request.Body.organization
$groupNames = $Request.Body.groupNames
try {
Connect-ExchangeOnline -AppId 00000000-0000-0000-0000-000000000000 `
-Organization $organization `
-CertificateThumbprint $certThumbPrint `
-ErrorAction Stop `
-ShowBanner:$false `
-ShowProgress:$false `
-CommandName Add-DistributionGroupMember

foreach ($group in $groupNames) {
try {
Write-Information "Adding $($userPrincipalName) to the group $($group)"
Add-DistributionGroupMember -Identity "$($group)" `
-Member "$($userPrincipalName)" `
-BypassSecurityGroupManagerCheck
}
catch {
Write-Error $_
}
}
$successMessage = "$($userPrincipalName) has been added to the distribution groups."
Write-Information $successMessage
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body       = @{
Success = $true
Message = $successMessage
}
})
}
catch {
Write-Error $_.Exception.Message
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body       = @{
Success = $false
Message = $_.Exception.Message
}
}) 
}
finally {
Disconnect-ExchangeOnline -Confirm:$false
}

第一次执行时一切正常,但是如果我尝试多次运行,就会得到这个错误:

[Error] Error:发生一个或多个错误。当>在http://localhost:50397/上监听系统浏览器完成登录时,发生了一个HttpListenerException。可能的原因和缓解:应用程序无法监听指定的URL;从Admin命令提示符运行'netsh http add>iplisten 127.0.0.1'。异常:Type:> microsoft . powershell . commands . writeerrorrexceptionmessage

当我在本地调试函数时不会发生此问题,它只会在Azure上托管时发生。

我使用在文件profile.ps1上定义的变量$certThumbPrint传递证书拇指指纹。事实证明,函数执行一次后信息就丢失了。我将拇指指纹移动到应用程序设置中,使用$env:CERT_THUMBPRINT从脚本中检索它,并解决了问题。

相关内容

  • 没有找到相关文章

最新更新