Created
December 1, 2017 14:30
-
-
Save esarabadani/7a2d2ce0dd76f21641bfaf724ae6dd71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$azureCredential = Get-AutomationPSCredential -Name "AzMonkey" | |
if($azureCredential -ne $null) | |
{ | |
Write-Output "Attempting to authenticate as: [$($azureCredential.UserName)]" | |
} | |
else | |
{ | |
throw "No automation credential name was specified..." | |
} | |
Connect-MsolService -Credential $azureCredential | |
Login-AzureRmAccount -Credential $azureCredential | |
Connect-AzureAD -Credential $azureCredential | |
$subs = Get-AzureRmSubscription | |
$groupID = "Here you will need to enter the Object ID of the Azure AD group" | |
foreach($sub in $subs) | |
{ | |
$subID = $sub.SubscriptionId | |
Select-AzureRmSubscription -SubscriptionId $subID | |
$substring = "/subscriptions/" + $subID | |
$usersList = (Get-AzureRmRoleAssignment -IncludeClassicAdministrators -scope $substring) | |
foreach($user in $usersList){ | |
if(($user.ObjectType -eq "User") -and ($user.ObjectId -ne "00000000-0000-0000-0000-000000000000")) | |
{ | |
$userGroups = Get-AzureADUserMembership -ObjectId $user.ObjectID | |
} | |
if(($user.ObjectType -eq "User") -and ($user.SignInName -ne "[email protected]") -and ($userGroups.ObjectID -notcontains $groupID) -and ($user.ObjectID -ne "00000000-0000-0000-0000-000000000000")) | |
{ | |
$userID = $user.objectID | |
Add-AzureADGroupMember -ObjectId $groupID -RefObjectId $userID | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment