Added Files

This commit is contained in:
DistractADD
2021-07-06 13:16:46 +10:00
parent f2475a3bdd
commit cfddd4fad2
393 changed files with 65842 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
# Script to add user to an AzureAD role
<#
You can find a list of available roles in the following Microsoft article
https://docs.microsoft.com/en-us/azure/active-directory/active-directory-assign-admin-roles-azure-portal
Double check using Get-AzureADDirectoryRole as they don't always have the same name in PowerShell as the GUI
Built from the code example on the following Mircosoft page
https://docs.microsoft.com/en-us/powershell/module/azuread/add-azureaddirectoryrolemember?view=azureadps-2.0
#>
# User UPN to assign role to
$roleUser = ''
# Role Name to Assign
$roleName = ''
# Import AzureAD module and Connect
Import-Module AzureAD
Connect-AzureAD
# Fetch user to assign to role
$roleMember = Get-AzureADUser -ObjectId $roleUser
# Fetch role instance
$role = Get-AzureADDirectoryRole | Where-Object {$_.displayName -eq $roleName}
# If role instance does not exist, instantiate it based on the role template
if (!($role)) {
# Instantiate an instance of the role template
$roleTemplate = Get-AzureADDirectoryRoleTemplate | Where-Object {$_.displayName -eq $roleName}
Enable-AzureADDirectoryRole -RoleTemplateId $roleTemplate.ObjectId
# Fetch role instance again
$role = Get-AzureADDirectoryRole | Where-Object {$_.displayName -eq $roleName}
}
# Add user to role
Add-AzureADDirectoryRoleMember -ObjectId $role.ObjectId -RefObjectId $roleMember.ObjectId
# Fetch role membership for role to confirm
Get-AzureADDirectoryRoleMember -ObjectId $role.ObjectId | Get-AzureADUser

View File

@@ -0,0 +1,45 @@
# Bulk add a new licence to users on the basis of what licence they currently have.
#
# An updated version of my previous MSOL script, now using AzureAD.
# This is useful for something like adding Office 365 ATP to everyone who currently has E3, for example.
#
# List of available SKUs can be obtained with (Get-AzureADSubscribedSku).SkuPartNumber
#
# What licence do the users currently have?
$existingLicence = 'ENTERPRISEPACK'
# What licence are we adding?
$licenceToAdd = 'ATP_ENTERPRISE'
# Import AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Get all available licence skus
$allSkus = Get-AzureADSubscribedSku
# Get sku ID for existing licence
$existingSkuID = ($allSkus | Where-Object {$_.SkuPartNumber -eq $existingLicence}).SkuId
# Get sku ID for new licence
$newSkuID = ($allSkus | Where-Object {$_.SkuPartNumber -eq $licenceToAdd}).SkuId
# Find everyone who has the existing licence but not the licence we're adding
$users = Get-AzureADUser -All $true | Where-Object {$_.AssignedLicenses.SkuId -match $existingSkuID -and !($_.AssignedLicenses.SkuId -match $newSkuID)}
# Create a new licence object for the licence we're adding
$newLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$newLicense.SkuId = $newSkuID
# Create a new assigned licenses object and add the licence we're adding to add licenses
$newLicenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$newLicenses.AddLicenses = $newLicense
# Add the new licence to each user in the list
foreach ($user in $users) {
Set-AzureADUserLicense -ObjectId $user.UserPrincipalName -AssignedLicenses $newLicenses
}
# Disconnect from AzureAD
Disconnect-AzureAD

View File

@@ -0,0 +1,53 @@
# Bulk add additional licences to a list of users.
#
# An updated version of my previous MSOL script, now using AzureAD.
#
# List of available SKUs can be obtained with (Get-AzureADSubscribedSku).SkuPartNumber
#
# Where is the list of user UPN's?
$userListPath = 'C:\Temp\UserList.txt'
# What licences are we adding?
$licencesToAdd = @('ENTERPRISEPACK','ATP_ENTERPRISE','EMSPREMIUM')
# Import AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Import list of users from file
$userList = Get-Content -Path $userListPath | Sort-Object
# Get all available licence skus
$newSkuIDs = (Get-AzureADSubscribedSku | Where-Object {$_.SkuPartNumber -in $licencesToAdd}).SkuId
# Create a licenses object
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
# Add the licences to add to the licences object
foreach ($newSkuID in $newSkuIDs) {
$newLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$newLicense.SkuId = $newSkuID
$licenses.AddLicenses += $newLicense
}
# Add the licenses
foreach ($username in $userList) {
try {
$user = Get-AzureADUser -ObjectId $username -ErrorAction Stop
if ($user.AccountEnabled -eq $true) {
Set-AzureADUserLicense -ObjectId $user.UserPrincipalName -AssignedLicenses $licenses -ErrorAction Stop
Write-Output -InputObject ('Licence(s) added for user account ' + $user.UserPrincipalName + '.')
}
else {
Write-Output -InputObject ('User account ' + $user.UserPrincipalName + ' is disabled.')
}
}
catch {
Write-Output -InputObject ('There was a problem updating licences for user account ' + $user.UserPrincipalName + '.')
Write-Output -InputObject $Error[0]
}
}
# Disconnect from AzureAD
Disconnect-AzureAD

View File

@@ -0,0 +1,58 @@
# Script to add multiple users to multiple AzureAD roles
<#
You can find a list of available roles in the following Microsoft article
https://docs.microsoft.com/en-us/azure/active-directory/active-directory-assign-admin-roles-azure-portal
Double check using Get-AzureADDirectoryRole as they don't always have the same name in PowerShell as the GUI
Built from the code example on the following Mircosoft page
https://docs.microsoft.com/en-us/powershell/module/azuread/add-azureaddirectoryrolemember?view=azureadps-2.0
#>
# User UPNs to assign roles to
$roleUsers = @('')
# Role Names to Assign
$roleNames = @('')
# Import AzureAD module and Connect
Import-Module AzureAD
Connect-AzureAD
# Run through the list of users
foreach ($roleUser in $roleUsers) {
# Fetch user to assign to role
$roleMember = Get-AzureADUser -ObjectId $roleUser
# Run through the list of roles
foreach ($roleName in $roleNames) {
# Fetch User Account Administrator role instance
$role = Get-AzureADDirectoryRole | Where-Object { $_.displayName -eq $roleName }
# If role instance does not exist, instantiate it based on the role template
if (!($role)) {
# Instantiate an instance of the role template
$roleTemplate = Get-AzureADDirectoryRoleTemplate | Where-Object { $_.displayName -eq $roleName }
Enable-AzureADDirectoryRole -RoleTemplateId $roleTemplate.ObjectId
# Fetch User Account Administrator role instance again
$role = Get-AzureADDirectoryRole | Where-Object { $_.displayName -eq $roleName }
}
# Get existing users with the role assigned
$existingRoleUsers = (Get-AzureADDirectoryRoleMember -ObjectId $role.ObjectId).UserPrincipalName
# Assign role to new user if not already in the list
if ($roleMember.UserPrincipalName -notin $existingRoleUsers) {
Add-AzureADDirectoryRoleMember -ObjectId $role.ObjectId -RefObjectId $roleMember.ObjectId -ErrorAction Stop
}
}
}
# Uncomment to fetch role membership for each role to confirm
#foreach ($roleName in $roleNames) {
# Write-Output -InputObject ('Members for role: ' + $roleName)
# Get-AzureADDirectoryRoleMember -ObjectId $role.ObjectId | Get-AzureADUser | Where-Object {$_.UserPrincipalName -in $roleUsers} | Format-List -Property DisplayName,UserPrincipalName
#}

View File

@@ -0,0 +1,29 @@
# Script to bulk disable guest accounts in Azure AD, either all or for a specific domain
#
# Are we disabling for a specific guest domain? Leave blank for all guests.
$guestDomain = ''
# Install the AzureAD module, uncomment if required
#Install-Module -Name AzureAD
# Import the AzureAD module and connect to Azure AD
Import-Module -Name AzureAD
Connect-AzureAD
# Get a list of all guests from Azure AD either matching the domain or all guests
if ($guestDomain -ne '') {
$guestAccounts = Get-AzureADUser -All $true | Where-Object { $_.AccountEnabled -eq $true -and $_.UserType -eq 'Guest' -and $_.Mail -like ('*' + $guestDomain) }
}
else {
$guestAccounts = Get-AzureADUser -All $true | Where-Object { $_.AccountEnabled -eq $true -and $_.UserType -eq 'Guest' }
}
# Disable the guest accounts
try {
$guestAccounts | Set-AzureADUser -AccountEnabled $false -ErrorAction Stop
Write-Output -InputObject ('The specified guest accounts have been disabled.')
}
catch {
Write-Output -InputObject ('Failed - The specified guest accounts have not been disabled.')
}

View File

@@ -0,0 +1,24 @@
# Script to bulk enable guest accounts in Azure AD, either all or for a specific domain
#
# Are we disabling for a specific guest domain? Leave blank for all guests.
$guestDomain = ''
# Install the AzureAD module, uncomment if required
#Install-Module -Name AzureAD
# Import the AzureAD module and connect to Azure AD
Import-Module -Name AzureAD
Connect-AzureAD
# Get a list of all guests from Azure AD either matching the domain or all guests
if ($guestDomain -ne '') {
$guestAccounts = Get-AzureADUser -All $true | Where-Object { $_.AccountEnabled -eq $false -and $_.UserType -eq 'Guest' -and $_.Mail -like ('*' + $guestDomain) }
}
else {
$guestAccounts = Get-AzureADUser -All $true | Where-Object { $_.AccountEnabled -eq $false -and $_.UserType -eq 'Guest' }
}
# Enable the guest accounts
$guestAccounts | Set-AzureADUser -AccountEnabled $true
Write-Output -InputObject ('The specified guest accounts have been disabled.')

View File

@@ -0,0 +1,21 @@
# Find all guests with unaccepted invites over a specific time period and remove them
#
# How many days are we allowing for invites to be accepted? (This can be 0 for all)
$cutOffDays = 30
# Install the AzureAD module, uncomment if required
#Install-Module -Name AzureAD
# Import the AzureAD module and connect to Azure AD
Import-Module -Name AzureAD
Connect-AzureAD
# Get the cut off date
$cutOffDate = (Get-Date).AddDays(-$cutOffDays)
# Get a list of all guests with unaccepted invites older than the specificed timeframe
$unacceptedGuests = Get-AzureADUser -All $true | Where-Object { $_.UserType -eq 'Guest' -and $_.UserState -eq 'PendingAcceptance' -and $_.RefreshTokensValidFromDateTime -lt $cutOffDate }
# Remove the guest accounts
$unacceptedGuests | Remove-AzureADUser

View File

@@ -0,0 +1,44 @@
# Bulk remove licences to a list of users.
#
# List of available SKUs can be obtained with (Get-AzureADSubscribedSku).SkuPartNumber
#
# Where is the list of user UPN's?
$userListPath = 'C:\Temp\UserList.txt'
# What licences are we removing?
$licencesToRemove = @('ENTERPRISEPACK','ATP_ENTERPRISE','EMSPREMIUM')
# Import AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Import list of users from file
$userList = Get-Content -Path $userListPath | Sort-Object
# Create a licenses object
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
# Get SkuIDs for licence(s) to be removed
$licenses.RemoveLicenses = (Get-AzureADSubscribedSku | Where-Object {$_.SkuPartNumber -in $licencesToRemove}).SkuId
# Remove the licenses
foreach ($username in $userList) {
try {
$user = Get-AzureADUser -ObjectId $username -ErrorAction Stop
if ($user.AccountEnabled -eq $true) {
Set-AzureADUserLicense -ObjectId $user.UserPrincipalName -AssignedLicenses $licenses
Write-Output -InputObject ('Licence(s) removed from user account ' + $user.UserPrincipalName + '.')
}
else {
Write-Output -InputObject ('User account ' + $user.UserPrincipalName + ' is disabled.')
}
}
catch {
Write-Output -InputObject ('There was a problem updating licences for user account ' + $user.UserPrincipalName + '.')
Write-Output -InputObject $Error[0]
}
}
# Disconnect from AzureAD
Disconnect-AzureAD

View File

@@ -0,0 +1,59 @@
# Bulk replace licences to a list of users.
#
# List of available SKUs can be obtained with (Get-AzureADSubscribedSku).SkuPartNumber
#
# Where is the list of user UPN's?
$userListPath = 'C:\Temp\UserList.txt'
# What licences are we adding?
$licencesToAdd = @('EMSPREMIUM')
# What licences are we removing?
$licencesToRemove = @('EMS','AAD_PREMIUM_P2')
# Import AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Import list of users from file
$userList = Get-Content -Path $userListPath | Sort-Object
$userList = @('OAndrewsAdmin@sis.tv')
# Get all available licence skus
$newSkuIDs = (Get-AzureADSubscribedSku | Where-Object {$_.SkuPartNumber -in $licencesToAdd}).SkuId
# Create a licenses object
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
# Add the licences to add to the licences object
foreach ($newSkuID in $newSkuIDs) {
$newLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$newLicense.SkuId = $newSkuID
$licenses.AddLicenses += $newLicense
}
# Add the licences we want to remove to the $licenses object
$licenses.RemoveLicenses = (Get-AzureADSubscribedSku | Where-Object {$_.SkuPartNumber -in $licencesToRemove}).SkuID
# Replace the licenses
foreach ($username in $userList) {
try {
$user = Get-AzureADUser -ObjectId $username -ErrorAction Stop
if ($user.AccountEnabled -eq $true) {
Set-AzureADUserLicense -ObjectId $user.UserPrincipalName -AssignedLicenses $licenses
Write-Output -InputObject ('Licences updated for user account ' + $user.UserPrincipalName + '.')
}
else {
Write-Output -InputObject ('User account ' + $user.UserPrincipalName + ' is disabled.')
}
}
catch {
Write-Output -InputObject ('There was a problem updating licences for user account ' + $user.UserPrincipalName + '.')
Write-Output -InputObject $Error[0]
}
}
# Disconnect from AzureAD
Disconnect-AzureAD

View File

@@ -0,0 +1,14 @@
# Update Online User UPN - when AD sync doesn't do it.
# Import the Azure AD module and connect
Import-Module AzureAD
Connect-AzureAD
# Whose UPN are we changing?
$oldUPN = Read-Host -Prompt 'Enter user''s old UPN in the format username@domain'
# What are we changing it to?
$newUPN = Read-Host -Prompt 'Enter user''s new UPN in the format username@domain'
# Update the UPN in AzureAD
Set-AzureADUser -ObjectId $oldUPN -UserPrincipalName $newUPN

View File

@@ -0,0 +1,10 @@
# Import the Azure AD module and connect
Import-Module AzureAD
Connect-AzureAD
# Whose ID are we getting?
$userUPN = Read-Host -Prompt 'Enter user''s UPN in the format username@domain'
# Convert 365 ImmutableID to AD GUID
$immutableID = (Get-AzureADUser -ObjectId $userUPN).ImmutableID
[GUID][System.Convert]::FromBase64String($immutableID)

View File

@@ -0,0 +1,29 @@
# Retrieve licences for all users and export to CSV.
#
# Import AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Get all available licence details
$allSkus = Get-AzureADSubscribedSku
# Import list of users from file
$users = Get-AzureADUser -All $true
# Get Licences for each user and find out what they are from the list
$allUserLicences = @()
foreach ($user in $users) {
$assignedLicences = @()
foreach ($license in $user.AssignedLicenses.SkuID) {
$assignedLicences += ($allSkus | Where-Object { $_.SkuID -eq $license }).SkuPartNumber
}
$userLicences = [PSCustomObject]@{
'UserName' = $user.UserPrincipalName
'AssignedLicences' = (($assignedLicences | Sort-Object) -join ';')
}
$allUserLicences += $userLicences
}
# Export list to CSV file
$allUserLicences | Export-Csv -Path 'C:\Temp\AllUserLicences.csv' -NoTypeInformation

View File

@@ -0,0 +1,38 @@
# Retrieve licences for a list of users and export them to a CSV file.
#
# Where is the list of user UPN's?
$userListPath = 'C:\Temp\UserList.txt'
# Import AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Get all available licence details
$allSkus = Get-AzureADSubscribedSku
# Import list of users from file
$users = Get-Content -Path $userListPath | Sort-Object
# Get Licences for user and find out what they are from the list
$allUserLicences = @()
foreach ($user in $users) {
$assignedLicences = @()
try {
$userDetails = Get-AzureADUser -ObjectId $user -ErrorAction Stop
foreach ($license in $userDetails.AssignedLicenses.SkuID) {
$assignedLicences += ($allSkus | Where-Object { $_.SkuID -eq $license }).SkuPartNumber
}
}
catch {
$assignedLicences = 'User not found.'
}
$userLicences = [PSCustomObject]@{
'UserName' = $user
'AssignedLicences' = (($assignedLicences | Sort-Object) -join ';')
}
$allUserLicences += $userLicences
}
# Export list to CSV file
$allUserLicences | Export-Csv -Path 'C:\Temp\UserLicences.csv' -NoTypeInformation

View File

@@ -0,0 +1,84 @@
# Some example scripts for finding users with specific plans assigned
#
# A full list of subscription and plan ID's is available from:
# https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/licensing-service-plan-reference
#
# Here I'm using the following plan IDs.
#
# OFFICESUBSCRIPTION (43de0ff5-c92c-492b-9116-175376d08c38)
# PROJECT_CLIENT_SUBSCRIPTION (fafd7243-e5c1-4a3a-9e40-495efcb1d3c3)
# VISIO_CLIENT_SUBSCRIPTION (663a804f-1c30-4ff0-9915-9db84f0d1cea)
#
# Plan ID's to check for
$planID1 = '43de0ff5-c92c-492b-9116-175376d08c38'
$planID2 = 'fafd7243-e5c1-4a3a-9e40-495efcb1d3c3'
$planID3 = '663a804f-1c30-4ff0-9915-9db84f0d1cea'
# Import the AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Get all users from AzureAD
$allUsers = Get-AzureADUser -All $true
# Create variables to put the users in to
$planID1Users = @()
$planID2Users = @()
$planID3Users = @()
# Check through the users and check what plans are assigned and enabled
foreach ($user in $allUsers) {
foreach ($assignedPlan in $user.AssignedPlans) {
# Find all users of plan 1 and add to the related variable
if ($assignedPlan.ServicePlanId -eq $planID1 -and $assignedPlan.CapabilityStatus -eq 'Enabled') {
$planID1Users += $user
}
# Find all users of plan 2 and add to the related variable
if ($assignedPlan.ServicePlanId -eq $planID2 -and $assignedPlan.CapabilityStatus -eq 'Enabled') {
$planID2Users += $user
}
# Find all users of plan 3 and add to the related variable
if ($assignedPlan.ServicePlanId -eq $planID3 -and $assignedPlan.CapabilityStatus -eq 'Enabled') {
$planID3Users += $user
}
}
}
# Create variables to put the users in to
$plan1OnlyUsers = @()
$planID1and2Users = @()
$planID1and3Users = @()
$planID12and3Users = @()
# Using plan 1 as the master list, check which other licences are assigned
foreach ($planID1User in $planID1Users) {
# Find users who are only in the plan 1 list
if ($planID1User -notin $planID2Users -and $planID1User -notin $planID3Users) {
$plan1OnlyUsers += $planID1User
}
# Find users who are the plan 1 and plan 2 list
if ($planID1User -in $planID2Users) {
$planID1and2Users += $planID1User
}
# Find users who are in the plan 1 and plan 3 list
if ($planID1User -in $planID3Users) {
$planID1and3Users += $planID1User
}
# Find users who are in all three lists
if ($planID1User -in $planID2Users -and $planID1User -in $planID3Users) {
$planID12and3Users += $planID1User
}
}
# How many people in each list?
$plan1OnlyUsers.Count
$planID1and2Users.Count
$planID1and3Users.Count
$planID12and3Users.Count
# Who are those people?
$plan1OnlyUsers
$planID1and2Users
$planID1and3Users
$planID12and3Users

View File

@@ -0,0 +1,36 @@
# Script to invite a list of guest users to AzureAD and add them to a group
#
# Useful for bulk adding guests to a SharePoint or Teams group
#
# Uses a CSV file containing columns for FirstName,LastName,DisplayName,ExternalEmailAddress
#
# Where is our list of people?
$contactsFile = 'C:\Temp\ExternalContacts.csv'
# What group are we added them to?
$groupName = 'Test Group'
# Import the AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Import the list of people
$contactsList = Import-Csv -Path $contactsFile
# Find the group ID for our group
$groupID = (Get-AzureADGroup -All $true | Where-Object {$_.DisplayName -eq $groupName}).ObjectId
# Run through the list sending the guest invite and adding them to the group, unless their email address is already used somewhere
foreach ($externalUser in $contactsList) {
$getExisting = Get-AzureADUser -SearchString $externalUser.ExternalEmailAddress
if ($getExisting.Count -eq 0) {
$newUser = New-AzureADMSInvitation -InvitedUserDisplayName $externalUser.DisplayName -InvitedUserEmailAddress $externalUser.ExternalEmailAddress -SendInvitationMessage $true -InviteRedirectUrl "https://myapps.microsoft.com"
Add-AzureADGroupMember -ObjectId $groupID -RefObjectId $newUser.InvitedUser.Id
Write-Output -InputObject ('Guest ' + $externalUser.ExternalEmailAddress + ' created and added to group.')
}
else {
Write-Output -InputObject ('Object already exists with email address ' + $externalUser.ExternalEmailAddress + '.')
}
}

View File

@@ -0,0 +1,42 @@
# Remove all licences from a list of users
#
# Removing licences with this module doesn't work in the way that Microsoft's documentation states.
# I'm assuming this is a bug, so the code below works at time of creation but may stop working in the future.
#
# Where is the list of user UPN's?
$userListPath = 'C:\Temp\UserList.txt'
# Import AzureAD module and connect
Import-Module AzureAD
Connect-AzureAD
# Import list of users from file
$userList = Get-Content -Path $userListPath | Sort-Object
# Remove all the licences
foreach ($username in $userList) {
try {
$user = Get-AzureADUser -ObjectId $username -ErrorAction Stop
if ($user.AccountEnabled -eq $true) {
if(($user.AssignedLicenses.SkuId).Count -ge 1) {
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.RemoveLicenses = (Get-AzureADSubscribedSku | Where-Object {$_.SkuId -in $user.AssignedLicenses.SkuId}).SkuId
Set-AzureADUserLicense -ObjectId $user.UserPrincipalName -AssignedLicenses $licenses
Write-Output -InputObject ('All licences removed from user account ' + $username + '.')
}
else {
Write-Output -InputObject ('User account ' + $username + ' has no licences assigned.')
}
}
else {
Write-Output -InputObject ('User account ' + $username + ' is disabled.')
}
}
catch {
Write-Output -InputObject ('User account ' + $username + ' does not exist.')
}
}
# Disconnect from AzureAD
Disconnect-AzureAD