如何在我的Azure功能应用程序中的PowerShell脚本中获取Azure AD用户列表



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

我对PowerShell和Azure功能应用程序几乎没有经验,所以我遇到了一些错误。在过去的几天里,我一直在排除故障,但运气不佳。

以下是我可以从本地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功能应用程序中运行的脚本:

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应用程序中运行此脚本时,我会收到以下错误:

azure错误的片段

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

Function应用程序可能缺少包含"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需要以不同的方式导入-由于这个github问题,它已经成为一个问题一段时间了。这似乎对大多数人都有效:

  • 设置应用程序以x64位运行:Function App>配置>常规设置>平台>64位
  • 将应用程序设置为在Powershell 7上运行,而不是在此线程上运行6
  • 用途:Import-Module AzureAD -UseWindowsPowerShell

最新更新