mzmaili / AzureADDeviceCleanup

AzureADDeviceCleanup PowerShell script helps to manage the stale devices in Azure AD in an efficient way by giving different options to deal with stale devices in Azure AD.
MIT License
26 stars 12 forks source link

AzureAD Device Cleanup Script

AzureADDeviceCleanup PowerShell script helps to manage the stale devices in Azure AD in an efficient way by giving different options to deal with stale devices in Azure AD.

Why is this script useful?

What does this script do?

[!NOTE] It is recommended to disable the stale devices for a grace period of time before deleting them from AAD safely, as you can not recover the deleted devices.

Using AzureADDeviceCleanup PowerShell script, you can automate Azure AD devices cleanup using schedule task as the following (ThresholdDays value can be changed as per the company's policy):

Extremely Important Notes:

User experience:

PS output

 .SYNOPSIS 
    AzureADDeviceCleanup PowerShell script. 

.DESCRIPTION 
    AzureADDeviceCleanup.ps1 is a PowerShell script helps to manage the stale devices in Azure AD in an efficient way by giving different options to deal with stale devices in Azure AD tenants. 

.AUTHOR: 
    Mohammad Zmaili 

.PARAMETER 
    ThresholdDays 
    Specifies the period of the last login. 
    Note: The default value is 90 days if this parameter is not configured. 

.PARAMETER 
    Verify 
    Verifies the affected devices that will be deleted when running the PowerShell with 'CleanDevices' parameter. 

.PARAMETER 
    VerifyDisabledDevices 
    Verifies disabled devices that will be deleted when running the PowerShell with 'CleanDisabledDevices' parameter. 

.PARAMETER 
    DisableDevices 
    Disables the stale devices as per the configured threshold. 

.PARAMETER 
    CleanDisabledDevices 
    Removes the stale disabled devices as per the configured threshold. 

.PARAMETER 
    CleanDevices 
    Removed the stale devices as per the configured threshold. 

.PARAMETER 
    OnScreenReport 
    Displays The health check result on PowerShell screen. 

.PARAMETER 
    SavedCreds 
    Uses the saved credentials option to connect to MSOnline. 
    You can use any normal CLOUD only user who is having read permission to verify the devices.  
    But you have to use a global admin when using clean parameters. 
    Notes: - This parameter is very helpful when automating/running the script in task scheduler. 
           - Update the saved credentials under the section "Update Saved credentials". 

.PARAMETER 
    ExcelReport 
    Generates Excel report and saves the result into it, if this switch not selected script will generate a CSV report. 

.EXAMPLE 
    .\AzureADDeviceCleanup.ps1 -Verify 
    Verifies the stale devices since 90 says that will be deleted when running the PowerShell with 'CleanDevices' parameter. 

.EXAMPLE 
    .\AzureADDeviceCleanup.ps1 -Verify -ThresholdDays <Number of Days> 
    Verifies the stale devices as per the entered threshold days that will be deleted when running the PowerShell with 'CleanDevices' parameter. 

.EXAMPLE 
    .\AzureADDeviceCleanup.ps1 -VerifyDisabledDevices -ThresholdDays <Number of Days> 
    Verifies the DISABLED stale devices as per the entered threshold days that will be deleted when running the PowerShell with 'CleanDisabledDevices' parameter. 

.EXAMPLE 
    .\AzureADDeviceCleanup.ps1 -VerifyDisabledDevices -ThresholdDays <Number of Days> -DisableDevices 
    Disables the stale devices as per the entered threshold days. 

.EXAMPLE 
    .\AzureADDeviceCleanup.ps1 -ThresholdDays <Number of Day> -CleanDevices -SavedCreds 
    Removes the stale devices as per the entered threshold days, uses the saved credentials to access MSOnline. 
    Note: You can automate running this script using task scheduler. 

.EXAMPLE 
    .\AzureADDeviceCleanup.ps1 -ThresholdDays <Number of Day> -CleanDisabledDevices -SavedCreds 
    Removes the stale disabled devices as per the entered threshold days, uses the saved credentials to access MSOnline. 
    Note: You can automate running this script using task scheduler. 

Script Output: 
----------- 

=================================== 
|Azure AD Devices Cleanup Summary:| 
=================================== 
Number of affected devices: 16 
Last Login verified: 5/31/2019 2:32:37 PM