繁体   English   中英

如何在我的 Azure Z86408593C34AF726FDD9 应用程序中的 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 文件中。 它在本地工作。 My goal is to have this script automatically run on Azure (I'm trying to do it in an Azure Function App) once a month, and then have the created CSV file be emailed to a specified email. 但是,我现在想弄清楚的是如何获取用户列表,以便脚本至少可以正常运行而不会出错。

我对 PowerShell 和 Azure Function 应用程序的经验很少,所以我遇到了一些错误。 最近几天我一直在排除故障,但没有成功。

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

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、function、脚本文件或可运行程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。异常

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

如果有什么需要澄清的,请告诉我。 任何帮助表示赞赏!

实际上,AzureAD 需要以不同的方式导入 - 根据此 github 问题,这一直是一个问题 这似乎适用于大多数人:

  • 将应用程序设置为以 x64 位运行:Function App> Configuration > General Settings > Platform > 64 Bit
  • 在此线程上将应用程序设置为在 Powershell 7 而不是 6 上运行
  • 使用: Import-Module AzureAD -UseWindowsPowerShell

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM