This repository was archived by the owner on May 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreate-ResourceGroup.ps1
More file actions
72 lines (63 loc) · 2.21 KB
/
Create-ResourceGroup.ps1
File metadata and controls
72 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<#
.SYNOPSIS
Run get-help -example Create-ResourceGroup.ps1 for examples
.EXAMPLE
.\Create-ResourceGroup.ps1 -ResourceGroupPrefix 'rg' -ResourceGroupLocation 'northeurope' -LocationShortName 'neu'
Resource Group 'rg-ad-neu' already exists
Resource Group 'rg-network-neu' already exists
Resource Group 'rg-mgmt-neu' already exists
Resource Group 'rg-srv-neu' already exists
Resource Group 'rg-wks-neu' already exists
.EXAMPLE
.\Create-ResourceGroup.ps1 -ResourceGroupPrefix 'rg' -ResourceGroupLocation 'westeurope' -LocationShortName 'weu'
Creating new resource group 'rg-ad-weu' in 'westeurope' region
Creating new resource group 'rg-network-weu' in 'westeurope' region
Creating new resource group 'rg-mgmt-weu' in 'westeurope' region
Creating new resource group 'rg-srv-weu' in 'westeurope' region
Creating new resource group 'rg-wks-weu' in 'westeurope' region
#>
[CmdletBinding()]
param (
[parameter(Mandatory = $true)]
[string] $ResourceGroupPrefix,
[parameter(Mandatory = $true)]
[string] $ResourceGroupLocation,
[parameter(Mandatory = $true)]
[string] $LocationShortName
)
#region StartTranscript
Stop-Transcript -ErrorAction SilentlyContinue
$date = Get-date -format "yyyy_dd_MM_HHmm"
$logname = "PowerShellLog_" + $date + ".log"
Start-Transcript -Path .\$logname
#end region
#region variables
$resourcegroups = @(
'-ad-',
'-network-',
'-mgmt-',
'-srv-',
'-wks-'
)
#endregion
#region AZ Context
$azAccountTest = (Get-AZContext -ErrorAction SilentlyContinue).count
if ($azAccountTest -eq 0) {
Write-Host 'Please Log in to Azure Account'
Connect-AzAccount
}
#endregion
foreach ($rg in $resourcegroups) {
$rgName = $ResourceGroupPrefix + $rg + $LocationShortName
$rgTest = (Get-AzResourceGroup -Name $rgName -Location $ResourceGroupLocation -ErrorAction SilentlyContinue).count
if ($rgTest -eq 0) {
Write-Host "Creating new resource group '$rgName' in '$ResourceGroupLocation' region"
New-AzResourceGroup -Name $rgName -Location $ResourceGroupLocation
}
else {
Write-Host "Resource Group '$rgName' already exists"
}
}
#region StopTranscript
Stop-Transcript
#endregion