---
# System prepended metadata

title: Scripting Azure RG creation
tags: [cloud]

---

---
tags: cloud
---
# Scripting Azure RG creation

## Powershell method

https://www.ntweekly.com/2020/05/19/create-multiple-azure-resource-groups-with-powershell/

### Creating a new RG

https://docs.microsoft.com/en-us/powershell/module/az.resources/new-azresourcegroup?view=azps-7.1.0

`New-AzResourceGroup -name "uol_soc_test" -location "uksouth"`

### Removing a RG

https://docs.microsoft.com/en-us/powershell/module/az.resources/remove-azresourcegroup?view=azps-7.1.0

`Remove-AzResourceGroup -name "uol_soc_test"`

and 

## Adding an RBAC role to a RG

### Powershell




`az role assignment create --assignee "scsjh@leeds.ac.uk" --role "Owner" --resource-group "uol_feps_soc_moduleid_user2"`



```
$ cat <<EOF > my_rgs
magic-rg-1
magic-rg-2
magic-rg-3

$ cat my_rgs | xargs -I{} "az group create --location uksouth \
                                         -g {} 
$ cat my_rgs | xargs -I{} az role assignment create --role "Owner" \
                                                  --assignee "scsjh@leeds.ac.uk" \
                                                  -g {}

```

https://docs.microsoft.com/en-us/powershell/module/az.resources/set-azresourcegroup?view=azps-7.1.0

```                             
1        0.795 get-AzSubscription
   2        0.662 Set-AzContext -SubscriptionId "UoL-Teaching-SOC-MCC"
   3        0.084 ls
   4        0.131 Import-CSv ./comp5850m_short.csv | foreach{New-AzResourceGroup -name $_rg_name -location "uksouth"…
   5        5.959 Import-CSv ./comp5850m_short.csv | foreach{New-AzResourceGroup -name $_.rg_name -location "uksouth"}
   6     1:57.027 Import-CSv ./comp5850m_all.csv | foreach{New-AzResourceGroup -name $_.rg_name -location "uksouth"}
   7        0.057 NewAzRoleAssignment -SignInName issmcal@leeds.ac.uk -RoleDefinitionName "Owner" -ResourceGroupName "uol_feps_soc_comp5850M_scsk…
   8        3.368 New-AzRoleAssignment -SignInName issmcal@leeds.ac.uk -RoleDefinitionName "Owner" -ResourceGroupName "uol_feps_soc_comp5850M_scs…
   9       12.787 Import-CSv comp5850m_short.csv | foreach{New-AzRoleAssignment -SignInName $_.rg_uid -RoleDefinitionName "Owner" -ResourceGroupN…
  10     4:33.038 Import-CSv comp5850m_all.csv | foreach{New-AzRoleAssignment -SignInName $_.rg_uid -RoleDefinitionName "Owner" -ResourceGroupNam…
  11       13.109 Import-CSv comp5850m_short.csv | foreach{New-AzRoleAssignment -SignInName scskd@leeds.ac.uk -RoleDefinitionName "Owner" -Resour…
  12     4:27.683 Import-CSv comp5850m_all.csv | foreach{New-AzRoleAssignment -SignInName scskd@leeds.ac.uk -RoleDefinitionName "Owner" -Resource…
  13        0.437 (Get-AzResourceGroup -Name uol_feps_soc_comp5850M_scskd).tags
  14        1.229 $Tags = (Get-AzResourceGroup -Name "uol_feps_soc_comp5850M_scskd").Tags
  15        0.002 $Tags
  16        4.676 Import-CSv comp5850m_short.csv | foreach{Set-AzResourceGroup -ResourceGroupName $_.rg_name -Tag $Tags}
  17     1:27.524 Import-CSv comp5850m_all.csv | foreach{Set-AzResourceGroup -ResourceGroupName $_.rg_name -Tag $Tags}        
```



Updated:

Set the right subscription:
Set-AzContext -SubscriptionId "UoL-Teaching-SOC-MCC" 

Create one RG by hand with the right tags:
(Get-AzResourceGroup -Name uol_feps_soc_comp5850m_scskd).tags

Name                           Value
----                           -----
Expiry Date                    2023-06-30
IT-Environment                 Teaching
Module Leader                  Karim Djemame (scskd)
Business Unit                  School of Computing
Criticality                    Teaching
Budget Code                    34000043
Application                    Student RG for COMP5850M module
Cost Centre                    School of Computing
School Contact                 Martin Callaghan (issmcal)
Owner                          School of Computing: See module leader and school contact



Create the RG in the right place:

```
Import-CSv ./comp3211-short-2324.csv | foreach{New-AzResourceGroup -ResourceGroupName $_.rg_name -Tag $Tags -Location "uksouth"}

Allocate an owner for each RG:
Import-CSv ./comp5850_short.csv | foreach{New-AzRoleAssignment -SignInName scskd@leeds.ac.uk -RoleDefinitionName "Owner" -ResourceGroupName $_.rg_name}

Import-CSv ./comp5850_short.csv | foreach{New-AzRoleAssignment -SignInName $_.rg_uid -RoleDefinitionName "Owner" -ResourceGroupName $_.rg_name}       
```

## Checking for active VMs:
```
 az vm list -d --query "[?powerState=='VM running']" --output table | wc -l
```

## Bulk delete
https://blog.jongallant.com/2020/05/azure-delete-multiple-resource-groups/

## Spreadsheet Formulae
=LEFT(B2,SEARCH("@",B2)-1)

=CONCAT("uol_feps_soc_comp3211_",C2)

```
az group list --tag delete=true --query "[].[name]" --output tsv | xargs --no-run-if-empty -l az group delete --yes --name && \
az resource list --tag delete=true=true --query "[].[id]" --output tsv | tr '\n' ' ' | xargs --no-run-if-empty az resource delete --ids
```

