Azure Pipelines Powershell脚本中出现“找不到类型”错误
在Azure管道中解决Powershell脚本中的以下错误时遇到问题:Azure Pipelines Powershell脚本中出现“找不到类型”错误,powershell,azure-devops,azure-active-directory,azure-pipelines,Powershell,Azure Devops,Azure Active Directory,Azure Pipelines,在Azure管道中解决Powershell脚本中的以下错误时遇到问题: Cannot find type [Microsoft.Open.AzureAD.Model.RequiredResourceAccess]: verify that the assembly containing this type is loaded. 背景如下: 我已经编写了一个脚本,用于为web应用程序创建和连接Azure广告应用程序注册。我不想进入门户并手动委派该权限,因此我正在尝试向脚本中添加一个部分,该部分将
Cannot find type [Microsoft.Open.AzureAD.Model.RequiredResourceAccess]: verify that the assembly containing this type is loaded.
背景如下:
我已经编写了一个脚本,用于为web应用程序创建和连接Azure广告应用程序注册。我不想进入门户并手动委派该权限,因此我正在尝试向脚本中添加一个部分,该部分将标识登录用户所需的graph API委派权限。读取并自动将其分配给应用程序注册
如果我直接在Azure Portal Cloud Shell中运行以下脚本,它将正常工作:
Write-Output "No App Registration found. We need to create one."
New-AzADApplication -DisplayName $AppService -IdentifierUris "http://$AppService"
Get-AzADApplication -DisplayName $AppService | Update-AzADApplication -ReplyUrl @("https://$AppService.azurewebsites.net/$LoginRoute", "https://$HostName/$LoginRoute")
$reg = Get-AzADApplication -DisplayName $($AppService)
Write-Output "Now we need to give the app permissions."
# We need to give the app permission to read the user profile
$graphPerms = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess"
$graphPerms.ResourceAppId = "00000003-0000-0000-c000-000000000000" # graph id, not app registration id
$readUser = New-Object -TypeName "Microsoft.Open.AzureAD.Model.ResourceAccess"
$readUser.Id = "e1fe6dd8-ba31-4d61-89e7-88639da4683d" # this is the permanent GUID of the User.Read permission in Graph
$readUser.Type = "Role"
$graphPerms.ResourceAccess = $readUser
Set-AzureADApplication -ObjectId $reg.ObjectId -RequiredResourceAccess $graphPerms
但是,当我尝试通过Azure Powershell发布任务运行它时,它会给我一个找不到类型错误的消息
所以,我知道找不到类型意味着什么,我的问题是为什么管道不能使用它,而Azure门户云Shell却可以使用它
而且,作为一个明显的后续行动,是否有任何明显的我遗漏的东西,我应该尽可能地使该类型可用于我的脚本
所以,我知道找不到类型意味着什么,我的问题是为什么会这样
不适用于管道,但适用于Azure门户云Shell
1.Azure中的Azure Portal Cloud Shell包含所需的Azure相关模块,因此您可以使用Azure Portal Cloud Shell轻松做到这一点
二,。是Azure Devops服务中的一个简单powershell任务Azure Devops服务与Azure不同,后者默认不包含这些az模块
3.与Powershell任务相比,默认包含一些az模块:
Azure PowerShell任务使用Azure/AzureRM/Az PowerShell模块与Azure订阅交互
但它不包含缺少的类型所属的。这就是为什么在Azure Devops管道中找不到类型错误
而且,作为一个明显的后续行动,有什么明显的我
遗漏了我应该尽可能地让我的客户使用该类型
剧本
快速解决方法是安装缺失的AzureAD模块。试着这样做:
Install-Module AzureAD -Scope CurrentUser -Force
Import-Module AzureAD -Force
$graphPerms = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess"
在脚本开始时安装并导入模块。这个问题会消失。我使用VS2017-windows2016托管代理和PS任务对其进行了测试
希望有帮助:如果在Azure Powershell任务中仍然存在相同的结果,则问题的原因可能是缺少。嗨,朋友,我的回答可以帮助解决您的难题和问题吗?任何更新都可以随时让我知道~我今天要测试它,如果它解决了问题,我会告诉你。但是,是的,我正在使用Azure Powershell任务。只想添加一个我发现的问题:带引号的CurrentUser在语法上是有效的。但这会导致模块无法导入。