0

I have a powershell task that is used to run a script which involves creating azure resources (Example: Resource group, Azure Key Vault, Function App...). When the pipeline is being run and it arrives to the powershell task in the deploy stage, it shows the following message:

enter image description here

The problem here, it says Finishing:Powershell but it didn't execute the script and did not create any azure resource.

Here is a sample of the powershell script:

$vaultName = "key vault name"
$blobstorageName = "blob storage name"
$Location = "Location Name"
$resourceGroupName = "Resource Group Name"

try {

    #Creation of Resource Group
    $resourceGroup = Get-AzResourceGroup -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue 

    if($null -eq $resourceGroup)
    {
        New-AzResourceGroup -Name $resourceGroupName -Location $Location
    }

    else
    {
        Write-Host "The ResourceGroup with the name: $resourceGroupName already exists."
    }

    # Creation of Storage Account

    $checkBlobStorage = (Get-AzStorageAccountNameAvailability -Name $blobstorageName) | Select-Object NameAvailable
    if ($checkBlobStorage.NameAvailable)
    {
        New-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $blobstorageName -Location $Location -SkuName Standard_LRS -Kind StorageV2 -AccessTier Hot
    }

    else 
    {
        Write-Host "The name $blobStorageName is not available. Suggest a new globally unique name!"
    }


catch 
{

}

Does anyone have a clue what is wrong ? Am I missing something in the powershell script (Maybe I don't have direct access to the azure portal from azure devops) or maybe something is missing in the Yaml file ?

10
  • "Am I missing something in the powershell script" - yes, you have 0 error handling - you just swallow any errors raised with the empty catch{} block, so you'll never know what actually happens. Start by adding Write-Host "Error occurred: $_" inside the catch block and see where it takes you :) Commented Jan 5, 2022 at 15:18
  • @MathiasR.Jessen You are definitely right. Thanks for that. I got the following error: Error occurred: The term 'Get-AzResourceGroup' is not recognized as the name of a cmdlet, function, script file, or operable program. But what should I do in this case ? Commented Jan 5, 2022 at 15:33
  • Add a previous step with a script that calls Install-Module Az.Resources to the pipeline. Commented Jan 5, 2022 at 15:35
  • @MathiasR.Jessen Can I just add Install-Module Az.Resources in the same script at the top ? Is it also possible to Install the Az Module ? Commented Jan 5, 2022 at 15:39
  • Give it a try :) Commented Jan 5, 2022 at 15:41

1 Answer 1

1

Two major issues:

  1. you seem to be using the Powershell Task, which is not designed for communication with Azure. You should use the Azure Powershell task for this kind of script, because it already has the right modules loaded and the authentication prepared.
  2. your script is swallowing the error so it is hiding what went wrong. It's usually more useful not to catch exceptions; if your script is erroring then let it error, and let the pipeline show you in its log what has happened.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.