Script Repository

Execute custom command on users listed in CSV file

July 22, 2021

The script executes a certain custom command on users listed in a CSV file. The users must be contained in a certain column of the CSV file, one user account per a line. To identify them, you can use any unique property of their accounts, for example, User Logon Name or Distinguished Name.

To schedule execution of the custom command using the script, create a scheduled task configured for the Domain-DNS object type, and include any of your AD domains in the Activity Scope.


  • $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. They will be sent to the e-mails identified in $recipient.
  • $commandID - Specifies the ID of the custom command to execute. How to get the ID.
  • $csvFilePath - Specifies a path to the CSV file to import.
  • $userIdentityProperty - Specifies the LDAP display name of the property to identify users by.
  • $userIdentityColumn - Specifies the name of the CSV file column that contains the user list.
  • $recepient - Specifies a semicolon-separated list of recipients of error reports.
  • $subject - Specifies the subject of emails with error reports.
  • $from - Specifies the email address from which the notification will be sent.
  • $mailServer - Specifies the SMTP server to be used when sending the report.
Edit Remove
$waitTimeMilliseconds = 9 * 60 # TODO: modify me. Time in seconds

$scriptBlock = {
    $commandID = "{9db88ec3-1241-4ab1-9612-c7c982baa49f}" # TODO: modify me
    $csvFilePath = "\\Server\Share\example.csv" # TODO: modify me
    $userIdentityProperty = "sAMAccountName" # TODO: modify me
    $userIdentityColumn = "Identity" # TODO: modify me
    # E-mail settings
    $recepient = "" # TODO: Modify me
    $subject = "Error report $date"
    $from = "" # TODO: Modify me
    $mailServer = "" # TODO: Modify me
    Function SearchUser ($userIdentity, $propertyName, $errorReport)
        # Find user in AD
        $searcher = $admService.OpenObject("Adaxes://RootDse", $NULL, $NULL, 0)
        $searcher.SearchFilter = "(&(sAMAccountType=805306368)($propertyName=$userIdentity))"
        $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
        $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
        $searcher.VirtualRoot = $True
            $searchResultIterator = $searcher.ExecuteSearch()
            $searchResults = $searchResultIterator.FetchAll()
            if ($searchResults.Length -eq 0)
                [void]$errorReport.Append("User '$userIdentity' not found")
                return $NULL
            elseif ($searchResults.Length -gt 1)
                [void]$errorReport.Append("Found more then one user with identity '$userIdentity'")
                return $NULL
                return $searchResults[0].AdsPath
            # Release resources
    # Check whether file exists
    if (!(Test-Path -Path $csvFilePath))
        Write-Error "File '$csvFilePath' was not found."
    $admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
    $admService = $admNS.GetServiceDirectly("localhost")
    $records = Import-Csv -Path $csvFilePath
    $errorReport = New-Object "System.Text.StringBuilder"

    foreach ($record in $records)
        $userPath = SearchUser $record.$userIdentityColumn $userIdentityProperty $errorReport
        if ([System.String]::IsNullOrEmpty($userPath))
        # Run Custom Command for the user
        $user = $admService.OpenObject($userPath, $NULL, $NULL, 0)
    if ($errorReport.Length -ne 0)
        Send-MailMessage -to $recepient -Body $errorReport.ToString() -Subject $subject -SmtpServer $mailServer -From $from

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

if ($job.State -ne "Completed")

# Get output from external process
Receive-Job -Job $job

Comments ( 0 )
No results found.
Leave a comment