Skip to content

Instantly share code, notes, and snippets.

@PCfromDC
Created December 15, 2017 20:59
Show Gist options
  • Save PCfromDC/20baafee6ddc645a7075e7227ee7868d to your computer and use it in GitHub Desktop.
Save PCfromDC/20baafee6ddc645a7075e7227ee7868d to your computer and use it in GitHub Desktop.
Create MSOL User and Add to Azure Service RBAC
Param (
[string]$msolDisplayName = "SVC-Nework-Gateway-Updater",
[string]$msolFirstName = "Network-Gateway",
[string]$msolLastName = "Updater-Account",
[string]$msolUpn = "[email protected]",
[string]$msolPassword = "MyPassword#12345",
[string]$msolUsageLocation = "US",
[string]$azureSubscriptionName = "mySubscription",
[string]$azureResourceGroupName = "Networking",
[string]$azureLocalGatewayName = "LocalGateway"
)
#region Login to O365 and Create User
$cred = Get-Credential -Message "Enter Admin Credentials for O365..."
Connect-MsolService -Credential $cred
$user = New-MsolUser -DisplayName $msolDisplayName `
-FirstName $msolFirstName `
-LastName $msolLastName `
-UserPrincipalName $msolUpn `
-UsageLocation $msolUsageLocation `
-ForceChangePassword:$false `
-PasswordNeverExpires:$true `
-Password $msolPassword
#endregion
#region Login to Azure and Add User to Local Network Gateway RBAC
Login-AzureRmAccount -SubscriptionName $azureSubscriptionName
$ID = Get-AzureRmResourceGroup $azureResourceGroupName | Get-AzureRmLocalNetworkGateway -Name $azureLocalGatewayName | select Id
New-AzureRmRoleAssignment -Scope $ID.Id -ObjectId $user.ObjectId -RoleDefinitionName "Contributor"
<# Remove User from EVERYWHERE!
Get-AzureRmRoleAssignment | Where-Object {$_.ObjectId -eq $userID} | Remove-AzureRmRoleAssignment
#>
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment