【问题标题】:Using the ExchangeOnlineManagement Powershell module inside Powershell Azure Functions在 Powershell Azure Functions 中使用 ExchangeOnlineManagement Powershell 模块
【发布时间】:2021-10-28 16:44:03
【问题描述】:

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

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
}

我第一次执行它时一切正常,但如果我尝试多次运行它,我会收到此错误:

[错误] 错误:出现一个或多个错误。 (在 > 侦听 http://localhost:50397/ 以使系统浏览器完成登录时发生 HttpListenerException。可能 > 原因和缓解:应用程序无法侦听指定的 URL;运行 'netsh http add >iplisten 127.0 .0.1' 来自管理员命令提示符。)异常:类型:>Microsoft.PowerShell.Commands.WriteErrorExceptionMessage

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

【问题讨论】:

    标签: azure powershell azure-functions office365


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多