azure - 如何在 Azure Function App 内的 PowerShell 脚本中获取 Azure AD 用户列表?

标签 azure powershell azure-functions powershell-core

一些上下文:我有一个 PowerShell 脚本,用于获取有关 Azure 上的用户及其许可证的信息,然后将该信息保存到 CSV 文件。它在本地工作。我的目标是让这个脚本每月在 Azure 上自动运行一次(我试图在 Azure Function App 中运行),然后将创建的 CSV 文件通过电子邮件发送到指定的电子邮件。然而我现在想弄清楚的是如何获取用户列表,以便脚本至少可以无错误地运行。

我对 PowerShell 和 Azure Function Apps 的经验很少,所以我遇到了一些错误。过去几天我一直在排除故障,但没有成功。

以下是我可以从本地 PowerShell 运行的脚本的开头:

Function main()
{
 #Clean up session
 Get-PSSession | Remove-PSSession
 #Connect AzureAD from PowerShell
 Connect-MsolService
 #Set output file
 $ExportCSV=".\DetailedO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
 $ExportSimpleCSV=".\SimpleO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"

 #FriendlyName list for license plan and service - txt file on local computer
 $FriendlyNameHash=Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction Stop | ConvertFrom-StringData

 #txt file on local computer
 $ServiceArray=Get-Content -Path .\ServiceFriendlyName.txt -ErrorAction Stop

 #Hash table declaration
 $Result=""
 $Results=@()
 $output=""
 $outputs=@()


 $LicensedUserCount=0

 #Get all licensed users

  Get-MsolUser -All | where{$_.islicensed -eq "true"} | Foreach{

  #this is another function that handles grabbing the user info and writing it to the CSV file
  Get_UsersLicenseInfo

  $LicensedUserCount++
  }
 

 . main

对于上面的脚本,它需要一些用户输入来输入凭据。我希望这个脚本能够在 Azure 中自动运行,无需任何用户输入,因此我一直在尝试修改它来做到这一点。我发现名称中带有“Msol”的任何命令在 Azure Function Apps/Powershell Core 中都不起作用,因此我找到了一个显然可以工作的不同模块。

这是我目前在 Azure Function App 中运行脚本的位置:

Import-Module AzureAD

Function main()
{
 #Clean up session
 Get-PSSession | Remove-PSSession
 
 $password = ConvertTo-SecureString "{my password here}" -AsPlainText -Force
 $UserCredential = New-Object System.Management.Automation.PSCredential ("myusernamehere", $password)

 Connect-AzureAD -Credential $UserCredential
 
 #Set output file
 $ExportCSV=".\DetailedO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
 $ExportSimpleCSV=".\SimpleO365UserLicenseReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
 #FriendlyName list for license plan and service - hash table here
 $FriendlyNameHash= @{AAD_BASIC = "Azure Active Directory Basic"; AAD_PREMIUM= "Azure Active Directory Premium"; AAD_PREMIUM_P1= "Azure Active Directory Premium P1"; AAD_PREMIUM_P2= "Azure Active Directory Premium P2" }
    
 #array of strings, used when getting user info
 $ServiceArray= "MCOEV", "Cloud PBX", "MCOPSTN2", "PSTN International", "mcomeetadv"

 #Hash table declaration
 $Result=""
 $Results=@()
 $output=""
 $outputs=@()


 $LicensedUserCount=0


  
Get-AzureADUser -All | where{$_.islicensed -eq "true"} | Foreach{

 Get_UsersLicenseInfo
 $LicensedUserCount++}


}
 . main

首先,我不确定是否需要验证此脚本是否从我的 Azure 帐户中运行。其次,也是我的主要问题是,当我尝试在 Azure Function App 中运行此脚本时,出现以下错误:

snippet of the azure error

如果图片不起作用,它会显示:

函数应用可能缺少包含“Connect-AzureAD”命令定义的模块。如果此命令属于 PowerShell 库中可用的模块,请将此模块的引用添加到requirements.psd1。确保此模块与 PowerShell 7 兼容。有关更多详细信息,请参阅 https://aka.ms/functions-powershell-managed-dependency 。如果已安装模块但仍然收到此错误,请尝试通过在产生错误的命令之前调用 Import-Module 来显式导入模块:这不会解决问题,但会暴露根本原因。

2021-06-08T16:48:00.377 [错误] 错误:术语“Connect-AzureAD”未被识别为 cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。异常

对于“Get-AzureADUser”行,我也遇到相同的错误。我遵循了这个指南:https://tech.nicolonsky.ch/azure-functions-powershell-modules/将 AzureAD 模块添加到我的托管依赖项中,但我仍然遇到相同的错误。

如果有任何需要澄清的地方,请告诉我。如有任何帮助,我们将不胜感激!

最佳答案

实际上,AzureAD 的导入方式需要稍有不同 - 根据 this github issue 这已经成为一个问题一段时间了。 。这似乎对大多数人都有效:

  • 将应用程序设置为以 x64 位运行:Function App> 配置 > 常规设置 > 平台 > 64 位
  • 将应用设置为在此线程上的 Powershell 7(而不是 6)上运行
  • 使用:导入模块 AzureAD -UseWindowsPowerShell

关于azure - 如何在 Azure Function App 内的 PowerShell 脚本中获取 Azure AD 用户列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67892345/

相关文章:

c# - 具有 Http 触发功能的连续 Azure WebJob

azure - 如何在 kusto 中查找表的来源

powershell - 在Get-ADComputer中排序

powershell - 获取外部对象的属性

c# - SignalR 服务(无服务器)- Azure 函数

mysql - Kubernetes 内部的通信 Wordpress 和外部 MySQL 服务器。 - MySQL 连接错误 : (2002)

c# - Azure Web 角色中的后台线程

sql-server - 如何获取数据集中表的特定列?

Express.js 应用程序无服务器,使用 Lambda 或函数 - 一个好主意?

c# - Azure 函数未由 EventHubTrigger 触发