Powershell、EWS、OAuth2 和自动化

2024-01-27

我正在尝试查找有关如何使用 PowerShell 对 EWS 实施非交互式 Oauth2 身份验证的文档,但我可能没有使用正确的搜索词,因为我找不到任何有用的内容。我在 OAuth2 上找到的 Microsoft 文档只有 C# 文档。

那么,有谁知道如何实现这一点?

  • 没有用户输入,应该使用可以作为脚本输入提供的输入
  • 应该在 PowerShell 中,而不是 C# 中
  • 细节!细节!不是“现在生成登录令牌”,而是生成该令牌的实际代码。

以下博客对此有非常好的概述:https://ingogegenwarth.wordpress.com/2018/08/02/ews-and-oauth/#more-5139 https://ingogegenwarth.wordpress.com/2018/08/02/ews-and-oauth/#more-5139

我使用上面的博客让它在我们的 PowerShell 脚本中运行 - 经过大量的试验和错误。以下示例脚本使用在 Azure AD 中注册的应用程序的 ClientID。如果您尚未在 Azure AD 中注册应用程序,则必须先执行此操作。网络上有各种指南可用于在 Azure AD 中注册新应用程序。为了将 EWS 与 OAuth 结合使用,您注册的应用程序必须在 Azure AD 中拥有正确的权限。对于 EWS,您有两种选择:

  1. 使用委派权限并请求 Azure AD 中的“EWS.AccessAsUser.All”API 权限 - 旧版 API |交流 |委派权限 | EWS.AccessAsUser.All(通过 Exchange Web 服务以登录用户身份访问邮箱)。此权限使您的注册应用程序具有与登录用户相同的 Exchange 邮箱访问权限。如果您使用此权限,则任何服务或用户帐户第一次使用您的应用程序的 ClientID 来访问 Exchange Online 时,相关帐户必须通过交互式弹出通知批准 ClientID。因此,在以自动方式使用此脚本之前,您必须使用注册应用程序的 ClientID 以交互方式访问 Exchange Online Service,并批准授权弹出窗口。最简单的方法是使用 Microsoft 的免费“EWS Editor”应用程序登录邮箱并指定应用程序的 ClientID。一旦您的应用程序的 ClientID 获得批准,您的脚本就可以完全自动化运行,无需任何交互。
  2. 使用应用程序权限并在 Azure AD 中请求“full_access_as_app”API 权限 - 旧版 API |交流 |委派权限 | EWS.AccessAsUser.All(通过 Exchange Web 服务以登录用户身份访问邮箱)。此权限使您注册的应用程序无需登录用户即可通过 Exchange Web 服务完全访问所有邮箱。此类权限使应用程序能够完全访问 Exchange Online 服务中的任何邮箱,并且必须得到提供“管理员同意”的 Azure AD 全局管理员的批准。然后,您的脚本将使用注册的 Azure AD 应用程序客户端 ID(实际上是用户名)和客户端密钥(实际上是密码)对 Exchange Online 进行身份验证。

下面的示例使用选项 1。我还没有测试选项 2。无论您选择哪个选项,您都需要处理从 Azure AD 请求 OAuth 令牌(下面代码中的示例)并定期检查和刷新令牌(不例子)。我还没有这样做,因为我们所有的 EWS 脚本都很简单,可以快速运行脚本,这些脚本在需要刷新令牌之前(通常在 60 分钟内)完成。如果这是您需要的,您需要向其他人寻求帮助。希望这至少可以帮助您走上正轨......

这是示例脚本(脚本的主体调用“Get-EWSOAuthToken”函数):

#Variables
$UserPrincipalName = "Enter the UPN of your Service Account ID"
$Password = "Password of your Service Account ID - store this securely"
$ClientIDfromAzureAD = "Client ID of your registered application in Azure AD"
$errRecip = "Email address of recipients to notify via email if errors occur"
$script = "Name of script"
$sender = "Email address of sender - normally the server name where your script runs"
$logfile = "Path and filename to log file"
$smtpServer = "Your SMTP server"

Function Get-EWSOAuthToken
{
    <#
        .SYNOPSIS
            Request an OAuth EWS token from Azure AD using supplied Username and Password

        .DESCRIPTION
            Request an OAuth EWS token from Azure AD using supplied Username and Password

        .PARAMETER UserPrincipalName
            The UPN of the user that will authenticate to Azure AD to request the OAuth Token

        .PARAMETER Password
            The Password (SecureString) of the user that will authenticate to Azure AD to request the OAuth Token

        .PARAMETER ADALPath
            The full path and filename on the local file system to the ADAL (Active Directory Authentication Library) DLL. This library is installed as part of various modules such as Azure AD, Exchange Online, etc.

        .PARAMETER ClientId
            Identifier of the client application that is requesting the token. You must register your calling application in Azure AD. This will provide you with a ClientID and RedirectURI

        .PARAMETER ConnectionUri
            The URI of the Exchange Online EWS endpoint. Default URI of 'https://outlook.office365.com/EWS/Exchange.asmx' is used

        .PARAMETER RedirectUri
            Address to return to upon receiving a response from the authority. You must register your calling application in Azure AD. This will provide you with a ClientID and RedirectURI

        .EXAMPLE
            $token = Get-EWSOAuthtokenFromCredential -UserPrincipalName "[email protected] /cdn-cgi/l/email-protection" -Password $mySecurePassword -ClientId "123444454545454767687878787" -RedirectUri "https://dummyredirectdomain.com"
            $ews = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2013_SP1 -ErrorAction Stop
            $ews.UseDefaultCredentials = $False
            $ews.Credentials = [Microsoft.Exchange.WebServices.Data.OAuthCredentials]$token
    #>

    [CmdletBinding()]
    Param
    (
        [System.String]$UserPrincipalName,
        [System.Security.SecureString]$Password,
        [System.String]$ADALPath,
        [System.String]$ClientId = "123444454545454767687878787",
        [System.Uri]$ConnectionUri = "https://outlook.office365.com/EWS/Exchange.asmx",
        [System.Uri]$RedirectUri = "https://dummyredirectdomain.com"
    )

    Begin
    {
        Write-Host "Starting Get-EWSOAuthTokenFromCredential function..." -ForegroundColor Yellow
        #Determine ADAL location based on Azure AD module installation path
        If([System.String]::IsNullOrEmpty($ADALPath)) 
        {
            Write-Host "Attempting to locate ADAL library..." -ForegroundColor Yellow

            $ADALPath = (Get-InstalledModule -Name "AzureAD" -ErrorAction SilentlyContinue | Select-Object InstalledLocation).InstalledLocation
            $ADALPath = Join-Path -Path $ADALPath -ChildPath "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
            Write-Host "Located library @ '$ADALPath'" -ForegroundColor Yellow
            If([System.String]::IsNullOrEmpty($ADALPath))
            {
                #Get List of installed modules and check Azure AD DLL is available
                $tmpMods = Get-Module -ListAvailable | Where-Object {$_.Name -eq "AzureAD"}

                If($tmpMods)
                {
                    $ADALPath = Split-Path $tmpMods.Path
                    $ADALPath = Join-Path -Path $ADALPath -ChildPath "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
                    Write-Host "Located library @ '$ADALPath'" -ForegroundColor Yellow
                }
                Else
                {
                    $err = "$($myinvocation.mycommand.name) requires the ADAL Library DLL files ('Microsoft.IdentityModel.Clients.ActiveDirectory.dll') that are installed as part of the 'AzureAD' module! Please install the AzureAD module from the Powershell Gallery. See: 'https://www.powershellgallery.com/packages/AzureAD' for more information"
                    Throw "$err"
                }
            }
        }

        #Load 'Microsoft.IdentityModel.Clients.ActiveDirectory' DLL
        Try
        {
            Import-Module $ADALPath -DisableNameChecking -Force -ErrorAction Stop
            Write-Host "Successfully imported ADAL Library" -ForegroundColor Yellow
        }
        Catch
        {
            $err = "$($myinvocation.mycommand.name): Could not load ADAL Library DLL '$ADALPath'. Error: $_"
            Throw "$err"
        }
    }
    Process
    {
        try
            {
            $resource = $connectionUri.Scheme + [System.Uri]::SchemeDelimiter + $connectionUri.Host
            $azureADAuthorizationEndpointUri = "https://login.windows.net/common/oauth2/authorize/"
            $AuthContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext($azureADAuthorizationEndpointUri) -ErrorAction Stop
            $AuthCredential = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserPasswordCredential($UserPrincipalName, $Password) -ErrorAction Stop
            Write-Host "$($myinvocation.mycommand.name): Requesting a new OAuth Token..." -ForegroundColor Yellow
            $authenticationResult = ([Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions]::AcquireTokenAsync($AuthContext, $resource, $clientId, $AuthCredential))

            If ($authenticationResult.Status.ToString() -ne "Faulted") {
                Write-Host "$($myinvocation.mycommand.name): Successfully retrieved OAuth Token" -ForegroundColor Yellow
            }
            else {
                $err = "$($myinvocation.mycommand.name): Error occurred calling ADAL 'AcquireTokenAysnc' : $authenticationResult.Exception.ToString()"
                Throw "$err"
            }
        }
        catch
        {
            #create object
            $returnValue = New-Object -TypeName PSObject

            #get all properties from last error
            $ErrorProperties =$Error[0] | Get-Member -MemberType Property

            #add existing properties to object
            foreach ($Property in $ErrorProperties)
            {
                if ($Property.Name -eq 'InvocationInfo')
                {
                    $returnValue | Add-Member -Type NoteProperty -Name 'InvocationInfo' -Value $($Error[0].InvocationInfo.PositionMessage)
                }
                else
                {
                    $returnValue | Add-Member -Type NoteProperty -Name $($Property.Name) -Value $($Error[0].$($Property.Name))
                }
            }
            #return object
            $returnValue
            break
        }
    }
    End
    {
        return $authenticationResult
    }
}


###### Main script

#Ensure TLS 1.2 protocol is enabled
try {
    If ([Net.ServicePointManager]::SecurityProtocol -notmatch 'Tls12') {
        [Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
        Write-Host "Enabled Tls1.2 in '[Net.ServicePointManager]::SecurityProtocol'" -ForegroundColor Yellow
    }
    else {
        Write-Host "Tls1.2 is enabled in '[Net.ServicePointManager]::SecurityProtocol'" -ForegroundColor Yellow
    }
}
Catch {
    $err = "An error occurred enabling TLS1.2. Error: $_"
    Write-Host "`n$err" -ForegroundColor Red
    Send-MailMessage -To $errRecip -Subject "$script - Error occurred during processing" -Body $err -From $sender -Attachment $logfile -SmtpServer $smtpServer
    Exit
}

#CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT
$EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Exchange\Web Services'|Sort-Object Name -Descending | Select-Object -First 1 -ExpandProperty Name)).'Install Directory') + "Microsoft.Exchange.WebServices.dll")
If (Test-Path $EWSDLL)
{
    Try
    {
        Import-Module $EWSDLL -DisableNameChecking -ErrorAction Stop
    }
    Catch 
    {
        $err = "An error occurred importing the Exchange Web Services DLL '$EWSDLL'. Error: $_"
        Write-Host "`n$err" -ForegroundColor Red
        Send-MailMessage -To $errRecip -Subject "$script - Error occurred during processing" -Body $err -From $sender -Attachment $logfile -SmtpServer $smtpServer
        Exit
    }
}
Else
{
    $err = "This script requires the EWS Managed API 1.2 or later. Please download and install the current version of the EWS Managed API from http://go.microsoft.com/fwlink/?LinkId=255472"
    Write-Host "`n$err" -ForegroundColor Red
    Send-MailMessage -To $errRecip -Subject "$script - Error occurred during processing" -Body $err -From $sender -Attachment $logfile -SmtpServer $smtpServer
    Exit
}


#Create EWS Object
$ews = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList "Exchange2013_SP1" -ErrorAction Stop

#Authenticate EWS using OAuth
Try {
    $ews.UseDefaultCredentials = $False
    Write-Host "Requesting EWS OAuth Token using registered Client ID" -ForegroundColor Yellow

    $OAuthResult = Get-EWSOAuthToken -UserPrincipalName $UserPrincipalName -Password $Password -ClientId "$ClientIDfromAzureAD" -ErrorAction Stop
    $token = $OAuthResult.Result.AccessToken

#Check if we successfully retrieved an Oauth Token
If ([System.String]::IsNullOrEmpty($token))
        {
            $err = "Get-EWSOAuthtoken returned an empty Auth Token. Aborted. Latest error details:`n$_error $($OAuthResult.Exception)"
            Write-Host "`n$err" -ForegroundColor Red
            $OAuthResult | Format-List -Force
            $OAuthResult.Result | Format-List -Force
            Send-MailMessage -To $errRecip -Subject "$script - Error occurred during processing" -Body "$err" -From $sender -Attachment $logfile -SmtpServer $smtpServer
            Exit
        }
        else
        {
            $OAuthchk = $true
            $ews.Credentials = [Microsoft.Exchange.WebServices.Data.OAuthCredentials]$token
            Write-Host "Set EWS credentials to OAuth token" -ForegroundColor Yellow
        }
    }
Catch
{
    $err = "An error occurred creating a new EWS object. Error:`n $_"
    write-host "`n$err" -ForegroundColor Red
    Send-MailMessage -To $errRecip -Subject "$script - Error occurred during processing" -Body "$err" -From $sender -Attachment $logfile -SmtpServer $smtpServer
    Exit
}

# Do your processing using EWS
....
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell、EWS、OAuth2 和自动化 的相关文章

随机推荐

  • Gradle 禁用所有增量编译和并行构建

    在一小部分 sbt 项目中 我们需要 protobuf grpc 编译 并且因为只有 Gradle 对此有正常支持 所以我们用它来执行 protobuf 相关任务 有时它会随机失败编译相同的东西 但重试时会成功 我们确定这是因为增量 Jav
  • Firestore:多个条件 where 子句

    例如 我的图书列表有动态过滤器 我可以在其中设置特定的颜色 作者和类别 该过滤器可以一次设置多种颜色和多个类别 Book gt Red Blue gt Adventure Detective 如何有条件地添加 where firebase
  • 使用 PHP 获取 DOM 元素

    我正在努力理解如何在 PHP 中使用 DOMElement 对象 我找到了这段代码 但我不确定它是否适用于我 dom new DOMDocument dom gt loadHTML index php div dom gt getEleme
  • 查看已完成的 Elasticsearch 任务

    我正在尝试使用 Elasticsearch 的更新查询 API 来运行日常任务 我可以找到当前正在运行的任务 但需要一种方法来查看所有任务 包括已完成的任务 我已经查看了 Update By Query API 的 ES 文档 https
  • 在 BeautifulSoup 中使用多个条件

    我们使用此代码查找包含文本 Fiscal 的标签 soup find class label text re compile Fiscal 我如何在这里放置多个条件 假设标签都包含 财政 和 年度 或者包含 财政 而不是 年份 的标签 如果
  • Android 如何在安排后更改 TimerTask 间隔?

    timerUploadTime new Timer timerUploadTimeTask new TimerTask Override public void run mHandler post new Runnable Override
  • Google 测试输出报告附加

    我在 VC 中使用 GTest 进行单元测试 我有5套测试服 我想生成 Google 测试返回的结果的测试报告 我在 InitGoogleTest 方法之前使用了 testing GTEST FLAGS output xml filenam
  • Sailsjs 套接字 IO

    我是 SailsJs 和 Socket IO 的新手 我想在 Sailsjs 中执行下面的 Socket IO 示例 在服务器端 我需要执行以下代码 但我不知道该代码放在哪里 var io require socket io listen
  • 我已抑制 knit 输出中的警告,但警告并未按预期显示在 Rmarkdown 控制台中。我该如何看待这些?

    我正在使用 Knit 和 rmarkdown 我在 pdf 输出中抑制了警告 然后通常警告会在 rmarkdown 控制台中列出 然而 在一份特定报告的情况下 我没有得到 rmarkdown 控制台中列出的警告 而是收到以下消息 有 15
  • 具有紧轴和匹配纵横比的散景图像图

    我在 Django 应用程序中使用 bokeh 1 0 1 版本 我想将微观表面图像显示为具有颜色编码高度和颜色条的可缩放图像图 原则上这是可行的 但我在获取具有正确纵横比的绘图时遇到问题 仅显示图像而周围没有空间 这是我想要实现的示例 结
  • 什么是服务总线?我什么时候需要服务总线?

    我听说过关于N服务总线 http particular net nservicebus 但我还没有真正理解它是什么 他们声称自己是 最流行的 net 开源服务总线 什么是服务总线 我什么时候需要服务总线 您可以将服务总线视为 SOA 面向服
  • 通过虚拟地址刷新/无效范围; ARMv8;缓存;

    我正在为 32 位模式下运行的 ARMv8 Cortex A53 实现缓存维护功能 当我尝试使用虚拟地址 VA 刷新内存区域时出现问题 DCacheFlushByRange看起来像这样 some init kDCacheL1 0 kDCac
  • 如何更新 PostgreSQL 中的大量行?

    我需要更新表中的数千行 例如 我有 1000 行 id 为 1 2 1000 mytable id value1 value2 1 Null Null 2 Null Null 1000 Null Null 现在我需要更改前 10 行 我可以
  • PHP数据库不会保存数据

    我有从视频教程系列中获得的以下代码 我听说下面的代码的某些部分被认为是不好的做法 我一直试图让它保存我的注册信息 但它不起作用 它可以很好地检测填写表单时的错误 它还显示 请登录以开始 当我正确提交所有内容时 但它不会将任何数据保存到数据库
  • XAML 解析异常:属性 {StaticResource PhoneAccentBrush} 值超出范围

    我遇到异常问题 找不到如何修复它的答案 属性 StaticResource PhoneAccentBrush 值超出范围 System Windows Application LoadComponent this new System Ur
  • 具有可选参数的 ViewComponent

    我正在创建一组代表不同视图上的过滤器的视图组件 到目前为止 它们工作得很好 但我不明白我所经历的这种行为 如果我使用声明两个 InvokeAsync public async Task
  • 我如何从 Rational(或任何没有构造函数的类)继承?

    我可以很容易地继承 比如说 String例如 像这样 class MyString lt String def stuff self and stuff end end This works MyString new things stuf
  • 想要执行sp_send_dbmail并以excel格式发送结果

    我想执行 sp send dbmail 并以 Excel 格式通过电子邮件发送结果 我尝试过 csv 和 txt 但结果没有组织在整齐的列中 这是我的代码 EXEC msdb dbo sp send dbmail profile name
  • 当 Toolkit.getDefaultToolkit() 抛出 AWTError 时,如何在 Java 中读取图像?

    我正在使用 Java 读取图像文件 java awt Image img Toolkit getDefaultToolkit createImage filePath 在某些系统上这不起作用 它会抛出一个 AWTError 抱怨 sun a
  • Powershell、EWS、OAuth2 和自动化

    我正在尝试查找有关如何使用 PowerShell 对 EWS 实施非交互式 Oauth2 身份验证的文档 但我可能没有使用正确的搜索词 因为我找不到任何有用的内容 我在 OAuth2 上找到的 Microsoft 文档只有 C 文档 那么