forked from fabferri/az-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvnet-peering.ps1
69 lines (58 loc) · 2.76 KB
/
vnet-peering.ps1
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
# Before running the script set the variables:
#
# $adminUsername : administrator username
# $adminPassword : adminsitrator password
# $subscriptionName : name of the Azure subscription
# $location : Azure region when deployed the resource group
# $rgName : resource group name
# $armTemplateFile : ARM template file
# tags:
# $RGTagExpireDate : expire date
# $RGTagContact : contact email
# $RGTagUsage : deployment purpose
#
#
################# Input parameters #################
$location = "westus"
$deploymentName = "vnetpeering-dev"
$armTemplateFile = "vnet-peering.json"
##
## tags
$RGTagExpireDate =((Get-Date).AddMonths(1)).ToString('yyyy-MM-dd')
$RGTagContact = 'user1@contoso.com'
$RGTagAlias = 'user1'
$RGTagUsage = 'test vnet peering'
####################################################
$pathFiles = Split-Path -Parent $PSCommandPath
$templateFile = "$pathFiles\$armTemplateFile"
#Reading the resource group name from the file init.txt
If (Test-Path -Path $pathFiles\init.txt) {
Get-Content $pathFiles\init.txt | Foreach-Object{
$var = $_.Split('=')
Try {New-Variable -Name $var[0].Trim() -Value $var[1].Trim() -ErrorAction Stop}
Catch {Set-Variable -Name $var[0].Trim() -Value $var[1].Trim()}
}
}
Else {Write-Warning "init.txt file not found, please change to the directory where these scripts reside ($pathFiles) and ensure this file is present.";Return}
if (!$azSubscriptionName) { Write-Host "variable $azSubscriptionName is null"; Exit }
$subscriptionName=$azSubscriptionName
write-host "reading Azure subscription name $azSubscriptionName from the file init.txt " -ForegroundColor yellow
if (!$ResourceGroupName) { Write-Host "variable $ResourceGroupName is null"; Exit }
$rgName=$ResourceGroupName
write-host "reading Resource Group name $ResourceGroupName from the file init.txt " -ForegroundColor yellow
$subscr=Get-AzSubscription -SubscriptionName $subscriptionName
Select-AzSubscription -SubscriptionId $subscr.Id
# Create Resource Group
Write-Host (Get-Date)' - ' -NoNewline
Write-Host "Creating Resource Group $rgName " -ForegroundColor Cyan
Try {$rg = Get-AzResourceGroup -Name $rgName -ErrorAction Stop
Write-Host ' resource exists, skipping'}
Catch {$rg = New-AzResourceGroup -Name $rgName -Location $location
Set-AzResourceGroup -Name $rgName `
-Tag @{Expires=$RGTagExpireDate; Contacts=$RGTagContact; Owner=$RGTagAlias; Usage=$RGTagUsage} | Out-Null
}
$runTime=Measure-Command {
write-host "running ARM template:"$templateFile
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $rgName -TemplateFile $templateFile -Verbose
}
write-host "runtime: "$runTime.ToString() -ForegroundColor Yellow