Last active
July 12, 2021 17:45
-
-
Save janegilring/c243f0085f2f4bb1212be2da296e48c9 to your computer and use it in GitHub Desktop.
Sample user offboarding runbook for Azure Automation for initiating device factory reset for all of a users devices as well as deleting the user
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
param( | |
$UserPrincipalName | |
) | |
try { | |
Import-Module -Name AzureAD -ErrorAction Stop | |
Import-Module -Name MSGraphIntuneManagement -ErrorAction Stop | |
} | |
catch { | |
throw 'Prerequisites not installed (AzureAD or MSGraphIntuneManagement PowerShell module not installed' | |
} | |
$IntuneCredential = Get-AutomationPSCredential -Name Intune | |
$IntuneClientId = Get-AutomationVariable -Name Intune-Client-Id | |
$Token = Get-MSGraphAuthenticationToken -Credential $IntuneCredential -ClientId $IntuneClientId | |
Write-Output "Authenticating to Microsoft Graph API" | |
# Get an Azure Active Directory User we want to retrieve Intune devices for | |
$AzureADUser = Get-MSGraphAzureADUser -UserPrincipalName $UserPrincipalName -AuthenticationToken $Token | |
Write-Output "Found user $($AzureADUser.displayName) in Azure AD" | |
# User`s devices for targeting a remote action | |
$Devices = Get-MSGraphIntuneUserDevice -UserID $AzureADUser.Id -AuthenticationToken $Token | |
# Invoke a reboot action (other available actions include RemoteLock,ResetPasscode, and FactoryReset) | |
$Devices | Foreach-Object { | |
switch ($PSItem.chassisType) { | |
'mobile' { | |
Write-Output "Invoking action RemoveCompanyData on device $($PSItem.deviceName)" | |
Invoke-MSGraphIntuneDeviceAction -RemoveCompanyData -DeviceID $PSItem.id -AuthenticationToken $Token | |
} | |
default { | |
Write-Output "Invoking action FactoryReset on device $($PSItem.deviceName)" | |
Invoke-MSGraphIntuneDeviceAction -FactoryReset -DeviceID $PSItem.id -AuthenticationToken $Token | |
} | |
} | |
} | |
# Remove user | |
Write-Output "Removing user from Azure Active Directory" | |
Connect-AzureAd -Credential $IntuneCredential | |
Start-Sleep 1800 # Allow some time to remote wipe to be initiated - won`t succeed if user is deleted | |
Remove-AzureADUser -ObjectId $AzureADUser.Id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment