Repository navigation
Expand file tree
/
Copy pathNew-AzureSubscription.ps1
More file actions
68 lines (55 loc) · 1.84 KB
/
Copy pathNew-AzureSubscription.ps1
File metadata and controls
68 lines (55 loc) · 1.84 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
[CmdletBinding()]
param (
# BillingScope
[Parameter(Mandatory)]
[string]
$BillingScope,
# Workload
[Parameter(Mandatory)]
[string]
$Workload,
# ManagementGroupId
[Parameter(Mandatory)]
[string]
$ManagementGroupId,
# Identifier
[Parameter(Mandatory)]
[string]
$Identifier,
# Environment
[Parameter(Mandatory)]
[string]
$EnvironmentShortName,
# DisplayName
[Parameter(Mandatory)]
[string]
$DisplayName
)
# The script requires the Az PowerShell Module
if (! (Get-Module 'Az.Subscription' -ListAvailable)) {
Throw 'Please install the Az PowerShell Module "https://www.powershellgallery.com/packages/Az.Subscription"'
}
Import-Module .\Az.Subscription
$params = @{
AliasName = "$Identifier-$EnvironmentShortName".toLower()
SubscriptionName = "$DisplayName-$EnvironmentShortName".toLower()
BillingScope = $BillingScope
Workload = $Workload
ManagementGroupId = $ManagementGroupId
}
Write-Verbose "Attempting to list any Azure Subscription" -Verbose
$SubAliases = Get-AzSubscriptionAlias
Write-verbose "Found a total of $($SubAliases.Count) Subscription Aliases." -Verbose
$SubAliases | Select-Object AliasName, SubscriptionId
if ($SubAliases.AliasName -Contains "$($params.AliasName)") {
$SubscriptionInfo = $SubAliases | Where-Object {$_.AliasName -eq "$($params.AliasName)"}
Write-Verbose "The subscription ""$($SubscriptionInfo.AliasName)"" already exists with id: '$($SubscriptionInfo.SubscriptionId)', Skipping creation." -Verbose
} else {
try {
$SubscriptionInfo = New-AzSubscriptionAlias @params
Write-Output $SubscriptionInfo
Write-verbose "Successfully created the subscription '$($SubscriptionInfo.AliasName)' with id: '$($SubscriptionInfo.SubscriptionId)'" -Verbose
} catch {
throw
}
}