Search code examples
azurepowershellazure-devopsazure-pipelinesazure-powershell

Azure DevOps - Set-Azcontext - The term 'Set-Azcontext' is not recognized


I have created one Azure DevOps pipeline.

99% of this pipeline calls PowerShell scripts (PowerShell Core script type).

Within the Power Shell scripts are calls to the AZ module - this all works fine.

I now have to add a new step within the DevOps pipeline to set the 'VulnerabilityAssessment' on an SQL server - however this time I need to call module 'Set-Azcontext' as part of the PowerShell script which is shown below...

[CmdletBinding()]

param(
    $prgname,
    $psqlservername,
    $psaname,
    $pnotificationmmail = 'john.doe@hotmail.com',
    $psubscriptionname = 'ABC'
)

#debug
#Get-Module -Name AZ -ListAvailable

# working in powershell and need to set the correct subscription in powershell (ABC)
Set-Azcontext -Subscription "ABC"

#Enable-AzSqlServerAdvancedDataSecurity -ResourceGroupName $prgname -ServerName $pservername -DoNotConfigureVulnerabilityAssessment

# https://learn.microsoft.com/en-us/powershell/module/az.sql/update-
azsqlservervulnerabilityassessmentsetting?view=azps-7.5.0

#Update-AzSqlServerVulnerabilityAssessmentSetting -ResourceGroupName $prgname -ServerName $pservername -StorageAccountName $psaname -ScanResultsContainerName "vulnerability-assessment" -RecurringScansInterval Weekly -EmailAdmins $true -NotificationEmail @pnotificationmmail

When the Azure devOps pipeline runs it fails on step...Set-Azcontext -Subscription "ABC"

update-vulnerability-assessment.ps1 : The term 'Set-Azcontext' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

The devOps process is running on a Azure agent. It looks like a module is not available or loaded.

Maybe I need to install/load the module containing 'Set-Azcontext' as part of the powershell script ? (if so how do I do this)

OR

Install a new capibility of the agent to have the module containing 'Set-Azcontext' installed?(if so how do I do this)


Solution

  • Going to instead via an ARM template in the Azure pipeline for setting the vulneratibilty of Azure SQL server