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 ?

Noobie2021
  • 281
  • 4
  • 23
  • "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 :) – Mathias R. Jessen Jan 05 '22 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 ? – Noobie2021 Jan 05 '22 at 15:33
  • Add a previous step with a script that calls `Install-Module Az.Resources` to the pipeline. – Mathias R. Jessen Jan 05 '22 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 ? – Noobie2021 Jan 05 '22 at 15:39
  • Give it a try :) – Mathias R. Jessen Jan 05 '22 at 15:41
  • @MathiasR.Jessen It says again : WARNING: User declined to install module (Az.Resources). Error occurred: The term 'Get-AzResourceGroup' 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. – Noobie2021 Jan 05 '22 at 15:49
  • My next shot would be `Install-Module Az.Resources -Force` – Mathias R. Jessen Jan 05 '22 at 15:50
  • @MathiasR.Jessen It worked however now it requires the following: Error occurred: Run Connect-AzAccount to login. How is it possible to directly login to the account in the pipeline ? Is it possible ? The problem in this case is that we are going to have the username and password stored in the script and that is a bad practice – Noobie2021 Jan 05 '22 at 15:59
  • Please [consult the documentation](https://learn.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount?view=azps-7.1.0), or [ask a new question](https://stackoverflow.com/questions/ask), I'm not a virtual helpdesk :) – Mathias R. Jessen Jan 05 '22 at 16:03
  • @MathiasR.Jessen Thank you very much for your help. Sorry for asking so many questions – Noobie2021 Jan 05 '22 at 16:09

1 Answers1

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.
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56