0
votes

during the VSTS deploy I am calling the task Azure PowerShell (Preferred Azure PowerShell Version 5.1.1) where I am checking the Event Grid subscriptions and I am adding or updating:

$subscriptions =  Get-AzureRmEventGridSubscription -ResourceGroupName $ResourceGroupName -TopicName $EventGridTopicName  | WHERE EventSubscriptionName -eq $Subscription.Name
if (!$subscriptions) 
{
    Write-Host 'Add new subscription'
    New-AzureRmEventGridSubscription -ResourceGroup $ResourceGroupName -TopicName $EventGridTopicName -Endpoint $Subscription.endpoint -EventSubscriptionName $Subscription.Name
    Write-Host 'New subscription added'
}
else {
    Write-Host 'Update endpoint'
    Update-AzureRmEventGridSubscription -ResourceGroup $resourceGroupName -TopicName $eventGridTopicName -Endpoint $endpoint -EventSubscriptionName $eventGridSubscriptionName
    Write-Host 'Subscription endpoint updated'
}

Running this code from local machine works fine. Running this as a part of VSTS deploy throws exception:

[error]The term 'Update-AzureRmEventGridSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

[EDIT]

[section]Starting: Azure PowerShell script: New-EventGridSubscription

Task : Azure PowerShell Description : Run a PowerShell script within an Azure environment Version : 2.0.11 Author : Microsoft Corporation

Help : More Information

[command]Import-Module -Name C:\Modules\AzureRm_5.1.1\AzureRM\5.1.1\AzureRM.psd1 -Global [command]Add-AzureRMAccount -ServicePrincipal -Tenant * -Credential System.Management.Automation.PSCredential -Environment AzureCloud [command] Select-AzureRMSubscription -SubscriptionId -TenantId [command]& 'D:\a\r1\a\Tools\scripts\New-EventGridSubscription.ps1' -ResourceGroupName -FunctionApps -EventGridTopicName *

[error]The term 'Update-AzureRmEventGridSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. [section]Finishing: Azure PowerShell script: New-EventGridSubscription

1
Import-Module AzureRMgvee
try using the latest version of Azure RM task and VS 2017 hosted agent (if you are using hosted). If you are using your own agent install appropriate AzureRM modules to it4c74356b41
[command]Import-Module -Name C:\Modules\AzureRm_5.1.1\AzureRM\5.1.1\AzureRM.psd1 -GlobalLeszek

1 Answers

1
votes

Refer to these steps:

  1. Add PowerShell task

Script:

Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Get-Module -ListAvailable| where {$_.Name -Like "*AzureRM*"}  | Select Name, Version
Install-Module -Name AzureRM -RequiredVersion 6.0.1 -Force -Scope CurrentUser -AllowClobber
Import-Module AzureRM -Force -Verbose -Scope Local 
Get-Module -ListAvailable| where {$_.Name -Like "*AzureRM*"}  | Select Name, Version
  1. Edit your Azure PowerShell task (Azure PowerShell Version: Specify other version; Preferred Azure PowerShell Versioin: 6.0.1)

Related thread: Upgrade AzureRM Powershell on Hosted 2017 Agent (VSTS - Visual Studio Team Services)