Skip to content

Instantly share code, notes, and snippets.

@brucedkyle
Last active September 12, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucedkyle/79cf8530d8cd0456a0158083d08a93b7 to your computer and use it in GitHub Desktop.
Save brucedkyle/79cf8530d8cd0456a0158083d08a93b7 to your computer and use it in GitHub Desktop.
Resource Group Lifecycle
Connect-AzAccount
# You may have more than one subscription.
$SUBSCRIPTION_ID= Read-Host -p "Enter your subscription id"
Select-AzSubscription -Subscription $SUBSCRIPTION_ID
# Set environment variables
$LOCATION = Read-Host -p "Enter your region, such as WestUS "
$RESOURCE_GROUP_NAME = Read-Host -p "Enter your resource group name "
$RESOURCE_NAME = Read-Host -p "Enter the name for your app service plan "
$TAGS = @{"Environment"="dev"; "Location"=$Location; "Cost Center"="111222"}
# create a resource group
New-AzResourceGroup -Name $RESOURCE_GROUP_NAME -Location $LOCATION `
-Tag $TAGS
# Create a resource, in this case an App Service Plan
New-AzAppServicePlan -Name $RESOURCE_NAME `
-Location $LOCATION -ResourceGroupName $RESOURCE_GROUP_NAME `
-Tag $TAGS
# Get the resource as a PowerShell object
$plan = Get-AzAppServicePlan -Name $RESOURCE_NAME -ResourceGroupName $RESOURCE_GROUP_NAME
Write-Host $plan
az login
# set environment variables
read -p "Enter your subscription id: " SUBSCRIPTION_ID
read -p "Enter your region, such as WestUS (no spaces): " LOCATION
read -p "Enter your resource group name, such as 'rg-wus-testme-01': " RESOURCE_GROUP_NAME
read -p "Enter the name for your app service plan, such as 'plan-wus-testme-01': " RESOURCE_NAME
# sets the default subsciption
az account set $SUBSCRIPTION_ID
TAGS=("Cost Center = 111222" "Location=West US")
az group create --name $RESOURCE_GROUP_NAME --location $LOCATION --tags "${TAGS[@]}"
az appservice plan create --name $RESOURCE_NAME --resource-group $RESOURCE_GROUP_NAME --tags "${TAGS[@]}"
az group delete --name $RESOURCE_GROUP_NAME
Remove-AzResourceGroup -Name $RESOURCE_GROUP_NAME
TODAY=`date +%Y-%m-%d`
TAGS=("Date Created=$TODAY" "Owner=bruce")
az group update --name $RESOURCE_GROUP_NAME --tags "${TAGS[@]}"
TAG="Cost Center='Account-56'"
az group update --name $RESOURCE_GROUP_NAME --set tags."$TAG"
$resourceGroup = Get-AzResourceGroup -Name $RESOURCE_GROUP_NAME
$today = Get-Date -Format "yyyy-MM-dd"
$tags = @{"Date Created"=$today; "Owner"="bruce"}
Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge
TAGS=("Cost Center=Finance-1222" "Location=West US")
az group update --name $RESOURCE_GROUP_NAME --tags "${TAGS[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment