Script Repository


Import users to be deprovisioned

December 06, 2016
1017

The script can be used to disable or delete user accounts in bulk. To specify the accounts that you need to disable or delete, create a CSV file with information on which operation must be performed on which users.

Parameters:

  • $csvFilePath - specifies the path to the CSV file to import.

CSV File Sample:

Name,Operation
pmason,Disable
John Brown,CN=Users,DC=corp,DC=contoso,DC=com,Delete
S-1-5-21-3165297888-301567370-576410423-1103,Disable
Users can be specified by using the following properties of the user account:
  • Distinguished Name (example: CN=SaraDavis,CN=Users,DC=corp,DC=contoso,DC=com)
  • GUID (example: 599C3D2E-F72D-4D20-8A88-030D99495F20)
  • Security Identifier (example: S-1-5-21-3165297888-301567370-576410423-1103)
  • SAM Account Name (example: saradavis)
Note: The script uses cmdlets from Adaxes PowerShell module for Active Directory. To run the script, you need to install the PowerShell Module for Active Directory component of Adaxes.

Edit Remove
PowerShell
Import-Module Adaxes
$csvFilePath = "\\Server\Share\example.csv" # TODO: modify me
function ExecuteOperation ($username, $operationType)
{
    switch ($operationType)
    {
        "Delete"
        {
            # Try to delete the user
            try
            {
                Remove-AdmUser -Identity $userName -AdaxesService localhost -ErrorAction Stop -Confirm:$False
            }
            catch
            {
                return "Error: User '$userName' was not deleted. Error message: " + $_.Exception.Message
            }
            return "User '$userName' deleted successfully."
        }
        "Disable"
        {
            # Try to disable the User
            try
            {
                Disable-AdmAccount -Identity $userName -AdaxesService localhost -ErrorAction Stop
            }
            catch
            {
                return "Error: User '$userName' was not disabled. Error message: " + $_.Exception.Message
            }
            return "User '$userName' disabled successfully."
        }
        default
        {
            return "Unknown operation: " + $operationType + " for user: " + $user.username
        }
    }
}
# Check file path
if (!(Test-Path -Path $csvFilePath))
{
    Write-Host "File '$csvFilePath' was not found."
    return
}
# Import data
$csvFile = Import-Csv -Path $csvFilePath
foreach ($user in $csvFile)
{
    $result = ExecuteOperation $user.Name $user.Operation
    Write-Host $result
}


Comments ( 0 )
No results found.
Leave a comment