Script Repository


Execute Custom Command on user with username identical to the target user

September 09, 2015
863

The following script executes a Custom Command on a user with the same username as the target user, but located in another AD domain. You can use the script in environments where a user has 2 separate user accounts in 2 different domains. In such a case, when a certain event occurs with one of accounts of a user, you can automatically perform certain operations on the other account associated with the same user. For example, when one of the accounts gets deprovisioned, you can also deprovision the other account.

To execute the script as a part of a Business Rule, Custom Command, or Scheduled Task, use the Run a program or PowerShell script action.

Parameters:

  • $commandID - Specifies the ID of the Custom Command that you want the script to execute. For information on how to get the ID of a Custom Command, see Get the ID of a Custom Command.
  • $resourceDomainDN - Specifies the Distinguished Name (DN) of the domain where the 2nd accounts of users are located.
Edit Remove
PowerShell
$commandID = "{9db88ec3-1241-4ab1-9612-c7c982baa49f}" # TODO: modify me
$resourceDomainDN = "DC=domain,DC=com" # TODO: modify me

# Find a user with the same username in the resource domain
$searcher = $Context.BindToObjectByDN($resourceDomainDN)
$searcher.SearchFilter = "(&(sAMAccountType=805306368)(sAMAccountName=%username%))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SizeLimit = 1

try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Count -eq 0)
    {
        $Context.LogMessage("Cannot find a user with username '%username%' in the resource domain", "Warning")
        return
    }
    
    # Run the Custom Command on the user
    $user = $Context.BindToObjectEx($searchResults[0].AdsPath, $True)
    $user.ExecuteCustomCommand($commandID)
}
finally
{
    # Release resources used by the search
	$searchResultIterator.Dispose()
}


Comments ( 0 )
No results found.
Leave a comment