Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set up management groups
#Requires -Version 7.0
#Requires -Modules PowerShellGet, Az.Resources
<#
.SYNOPSIS
Creates a management group
.DESCRIPTION
Creates a management group
.PARAMETER OrganizationName
Used to create the management group name
.OUTPUTS
If the creation was successful, it return the management group name; otherwise, null.
.NOTES
Version: 1.0
Author: Bruce Kyle
Creation Date: 6/25/2020
Purpose/Change: Initial script development
Copyright 2020 Stretegic Datatech LLC
License: MIT https://opensource.org/licenses/MIT
.EXAMPLE
.\New-ManagementGroup.ps1 -OrganizationName "Strategic Datatech LLC"
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory)] [string] $OrganizationName
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
try {
$ManagementGroupName = $OrganizationName -replace '\s',''
$ManagementGroupName = $ManagementGroupName'Root'
Write-Host "Creating management group for $OrganizationName"
New-AzManagementGroup -GroupName $ManagementGroupName -DisplayName "$OrganizationName Root Group"
}
catch
{
Write-Host "An error occurred creating management group:"
Write-Host $_
$ManagementGroupName = null;
}
return $ManagementGroupName
#Requires -Version 7.0
#Requires -Modules PowerShellGet, Az.Resources
<#
.SYNOPSIS
Creates a management group
.DESCRIPTION
Creates a management group
.PARAMETER OrganizationName
Used to create the management group name
.OUTPUTS
If the creation was successful, it return the management group name; otherwise, null.
.NOTES
Version: 1.0
Author: Bruce Kyle
Creation Date: 6/25/2020
Purpose/Change: Initial script development
Copyright 2020 Stretegic Datatech LLC
License: MIT https://opensource.org/licenses/MIT
.EXAMPLE
$OrganizationName = "Strategic Datatech LLC"
$TeamName = "Dev"
$ParentManagementGroupName = @(.\New-ManagementGroup.ps1 -OrganizationName $OrganizationName)
.\New-ManagementSubGroup.ps1 -ParentManagementGroup $ParentManagementGroupName `
-OrganizationName $OrganizationName -TeamName $TeamName
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory)] [string] $ParentManagementGroup,
[Parameter(Mandatory)] [string] $OrganizationName,
[Parameter(Mandatory)] [string] $TeamName
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
try {
$OrganizationName = $OrganizationName -replace '\s',''
$TeamName = $TeamName -replace '\s',''
$parentGroup = Get-AzManagementGroup -GroupName $ParentGroupName
$groupName = ({0}{1} -f $OrganizationName,$TeamName)
Write-Host "Creating management group $groupName "
New-AzManagementGroup -GroupName $groupName -DisplayName "$TeamName Group" `
-ParentId $parentGroup.id
}
catch
{
Write-Host "An error occurred creating management group:"
Write-Host $_
$ManagementGroupName = null;
}
return $ManagementGroupName
#Requires -Version 7.0
#Requires -Modules PowerShellGet, Az.Resources
<#
.SYNOPSIS
Sets the subscription to a management group
.DESCRIPTION
Sets the subscription to a management group
.PARAMETER ManagementGroupName
Used to create the management group name
.PARAMETER SubscriptionID
The subscription id
.NOTES
Version: 1.0
Author: Bruce Kyle
Creation Date: 6/25/2020
Purpose/Change: Initial script development
Copyright 2020 Stretegic Datatech LLC
License: MIT https://opensource.org/licenses/MIT
.EXAMPLE
.\Set-ManagementGroup.ps1 -ManagementGroupName $ManagementGroupName `
-SubscriptionId 9f241d6e-16e2-4b2b-a485-cc546f04799b
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory)] [string] $ManagementGroupName,
[Parameter(Mandatory=$false)] [string] $SubscriptionId
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
try {
if ($SubscriptionId -eq $null) {
$SubscriptionId = (Get-AzContext).Subscription.SubscriptionId
}
Write-Host "Assigning subscription '$SubscriptionId' to management group '$ManagementGroupName'"
New-AzManagementGroupSubscription -GroupName $ManagementGroupName -SubscriptionId $SubscriptionId
}
catch
{
Write-Host "An error occurred assigning management group:"
Write-Host $_
$OrganizationName = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment