CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Conditional Logic in Azure Function App Deployment Script

πŸ‘€ Views: 29 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-13
powershell azure function-app PowerShell

I'm collaborating on a project where I'm migrating some code and I'm currently working on a PowerShell script to deploy an Azure Function App, and I'm working with issues with the conditional logic in my deployment process. The script is supposed to check if a resource group exists before attempting to create a new one, but I keep receiving an behavior that says `Resource group 'myResourceGroup' not found` even when I believe it should exist. Here’s the relevant part of my script: ```powershell $resourceGroupName = 'myResourceGroup' $location = 'East US' # Check if the resource group exists $resourceGroup = Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue if (-not $resourceGroup) { Write-Host "Resource group does not exist. Creating new resource group..." New-AzResourceGroup -Name $resourceGroupName -Location $location } else { Write-Host "Resource group already exists." } ``` When I execute this script, it runs without any issues, indicating that the resource group was created successfully. However, when I run the deployment command afterwards, I still encounter the behavior about the resource group not being found. I suspect that the Azure context may not be set properly or that there might be a delay in the Azure portal reflecting the new resource group creation. I've tried adding a `Start-Sleep -Seconds 10` after the resource group creation, but that doesn't seem to help. I’ve also confirmed that my Azure PowerShell module is up to date with version 7.3.1. Could there be an scenario with how I'm retrieving the resource group? Any suggestions on ensuring that my deployment script correctly identifies the existing resource group before proceeding? I'd really appreciate any guidance on this. I've been using Powershell for about a year now. Is this even possible?