Skip to content

Instantly share code, notes, and snippets.

@brucedkyle
Last active June 27, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucedkyle/23ee35fae7bfbf3863d882e5b757ab18 to your computer and use it in GitHub Desktop.
Save brucedkyle/23ee35fae7bfbf3863d882e5b757ab18 to your computer and use it in GitHub Desktop.
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