如何在 Azure Function App 的 PowerShell 脚本中获取 Azure AD 用户列表?
How can I get a list of Azure AD Users in my PowerShell script within my Azure Function App?
一些上下文:我有一个 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
首先,如果此脚本是 运行ning 来自我的 Azure 帐户,我不确定是否需要进行身份验证。其次,我的主要问题是,当我尝试在我的 Azure Function App 中 运行 这个脚本时,我得到这个错误:
snippet of the azure error
如果图片不对,则表示:
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、函数、脚本文件或可操作的名称 program.Check 拼写名称,或者如果包含路径,请验证路径是否正确并尝试 again.Exception
我也对带有 'Get-AzureADUser' 的行得到同样的错误。我遵循了本指南:https://tech.nicolonsky.ch/azure-functions-powershell-modules/ 将 AzureAD 模块添加到我的托管依赖项中,但我仍然遇到同样的错误。
如果有任何需要澄清的地方,请告诉我。感谢您的帮助!
实际上,AzureAD 需要以不同方式导入 - 根据 this github issue 一段时间以来,这一直是个问题。这似乎对大多数人都有效:
- 将应用程序设置为 运行 作为 x64 位:Function App>
配置 > 常规设置 > 平台 > 64 位
- 在 Powershell 7 上将应用程序设置为 运行 而不是此线程上的 6
- 使用:
Import-Module AzureAD -UseWindowsPowerShell
一些上下文:我有一个 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
首先,如果此脚本是 运行ning 来自我的 Azure 帐户,我不确定是否需要进行身份验证。其次,我的主要问题是,当我尝试在我的 Azure Function App 中 运行 这个脚本时,我得到这个错误:
snippet of the azure error
如果图片不对,则表示:
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、函数、脚本文件或可操作的名称 program.Check 拼写名称,或者如果包含路径,请验证路径是否正确并尝试 again.Exception
我也对带有 'Get-AzureADUser' 的行得到同样的错误。我遵循了本指南:https://tech.nicolonsky.ch/azure-functions-powershell-modules/ 将 AzureAD 模块添加到我的托管依赖项中,但我仍然遇到同样的错误。
如果有任何需要澄清的地方,请告诉我。感谢您的帮助!
实际上,AzureAD 需要以不同方式导入 - 根据 this github issue 一段时间以来,这一直是个问题。这似乎对大多数人都有效:
- 将应用程序设置为 运行 作为 x64 位:Function App> 配置 > 常规设置 > 平台 > 64 位
- 在 Powershell 7 上将应用程序设置为 运行 而不是此线程上的 6
- 使用:
Import-Module AzureAD -UseWindowsPowerShell