在Powershell Azure函数中使用ExchangeOnlineManagement Powershell模块 [英] Using the ExchangeOnlineManagement Powershell module inside Powershell Azure Functions

查看:35
本文介绍了在Powershell Azure函数中使用ExchangeOnlineManagement Powershell模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在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上时才会出现此问题。

推荐答案

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

这篇关于在Powershell Azure函数中使用ExchangeOnlineManagement Powershell模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆