Script Repository


Delete user from Office 365

October 18, 2018
1526

The scripts can be used in Business Rules, Custom Commands and Scheduled Tasks to delete the Office 365 account of the user on which the script is executed.

For the script to work, you need to install Microsoft Azure Active Directory Module for Windows PowerShell on each computer where Adaxes service is running.

Note: If Adaxes is installed in an environment where Active Directory is synchronized with Office 365, you do not need to delete Office 365 user accounts. If you delete a user from your Active Directory, the user's Office 365 account will be deleted automatically during the nearest directory synchronization.

Locate Account by Office 365 Object ID

This version of the script locates a user account in Office 365 using a unique ID assigned to a user account in Office 365.

Parameter:

  • $waitTimeMilliseconds - specifies the time during which Adaxes will wait for the script to complete. It is recommended not to set a time exceeding the 10 minutes' limit applied by Adaxes to scripts executed by Business Rules, Custom Commands and Scheduled Tasks. If a script runs for more time than you specify, it will be completed, but the errors, warnings and other messages will not be added to the Execution Log.
Edit Remove
PowerShell
$waitTimeMilliSeconds = 8 * 60 # TODO: modify me

# Get Office 365 Object ID for the user
try
{
    $objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
    $Context.LogMessage("The user doesn't have an Office 365 Account", "Warning")
    return
}

# Get Office 365 Tenant credentials
$office365Cred = $Context.GetOffice365Credential()

# Script block to delete the user in Office 365
$scriptBlock = {
    param($credential, $objectId)
    Import-Module MsOnline
    
    Connect-MsolService -Credential $credential
    
    try
    {
        Remove-MsolUser -ObjectId $objectId -Force -ErrorAction Stop    
    }
    catch
    {
        Write-Error $_.Exception.Message
    }
}

# Start Windows PowerShell as a separate process and run the script block in that process
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $office365Cred, $objectId.ToString()
$job | Wait-Job -Timeout $waitTimeMilliSeconds

# Check job state
if ($job.State -ne "Completed")
{
    return # Could not get response from Office 365 within the time frame set
}

# Get response from Office 365
Receive-Job -Job $job

Locate Account by User Principal Name

This version of the script locates a user account in Office 365 via the User Principal Name (UPN).

Parameters:

  • $office365Domain - specifies your Office 365 domain name;
  • $waitTimeMilliseconds - specifies the time during which Adaxes will wait for the script to complete. It is recommended not to set a time exceeding the 10 minutes' limit applied by Adaxes to scripts executed by Business Rules, Custom Commands and Scheduled Tasks. If a script runs for more time than you specify, it will be completed, but the errors, warnings and other messages will not be added to the Execution Log.
Edit Remove
PowerShell
$waitTimeMilliseconds = 8 * 60 # TODO: modify me
$office365Domain = "MyCompany.onmicrosoft.com" # TODO: modify me

# Get Office 365 Tenant credentials
$office365Cred = $Context.GetOffice365Credential()

# Script block to delete the user in Office 365
$scriptBlock = {
    param($credential, $office365Domain)
    Import-Module MsOnline
    
    Connect-MsolService -Credential $credential
    
    try
    {
        Remove-MsolUser -UserPrincipalName "%username%@$office365Domain" -Force -ErrorAction Stop    
    }
    catch
    {
        Write-Error $_.Exception.Message
    }
}

# Start Windows PowerShell as a separate process and run the script block in that process
$job = Start-Job -ScriptBlock $scriptBlock -ArgumentList $office365Cred, $office365Domain
$job | Wait-Job -Timeout $waitTimeMilliseconds

# Check job state
if ($job.State -ne "Completed")
{
    return # Could not get response from Office 365 within the time frame set
}

# Get response from Office 365
Receive-Job -Job $job

Comments ( 0 )
No results found.
Leave a comment