Script repository

Execute custom command on users listed in CSV file

Updated on: Jan 18, 2026, Views: 4014

Miscellaneous

The script executes a custom command on users listed in a CSV file. To execute the script, create a scheduled task configured for the Domain object type and add a managed domain to the Activity Scope of the task. The domain will only be used to trigger execution of the scheduled task.

Parameters

  • $waitTimeMilliseconds - the time (in seconds) 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 specified in the $recipient variable.
  • $commandID - the identifier of the custom command to execute. For informaiotn on how to get the identifier, see Get custom command identifier.
  • $csvFilePath - a path to the CSV file to import.
  • $userIdentityProperty - the name of the property to identify users.
  • $userIdentityColumn - the header of the CSF file column that contains user identities.
  • $recipient - the email address to send error reports to.
  • $subject - the subject of emails with error reports.
  • $from - the email address from which the notification will be sent.
  • $mailServer - the SMTP server to be used when sending emails.
$waitTimeMilliseconds = 9 * 60 # TODO: modify me.

$scriptBlock = {
    Import-Module Adaxes
    
    $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
    
    # Email settings
    $recipient = "recipient@domain.com" # TODO: Modify me
    $subject = "Error report $date"
    $from = "noreply@domain.com" # TODO: Modify me
    $mailServer = "mail.domain.com" # TODO: Modify me
    
    Function SearchUser ($userIdentity, $propertyName, $errorReport)
    {
        # Find user in AD.
        $searcher = $admService.OpenObject("Adaxes://RootDse", $NULL, $NULL, 0)
        $criteria = New-AdmCriteria "user" {$propertyName -eq $userIdentity}
        $searcher.Criteria = $criteria
        $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
        $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
        $searcher.VirtualRoot = $True
        
        try
        {
            $searchResultIterator = $searcher.ExecuteSearch()
            $searchResults = $searchResultIterator.FetchAll()
            
            if ($searchResults.Length -eq 0)
            {
                [void]$errorReport.Append("User '$userIdentity' not found")
                [void]$errorReport.AppendLine()
                return $NULL
            }
            elseif ($searchResults.Length -gt 1)
            {
                [void]$errorReport.Append("Found more then one user with identity '$userIdentity'")
                [void]$errorReport.AppendLine()
                return $NULL
            }
            else
            {
                return $searchResults[0].AdsPath
            }
        }
        finally
        {
            # Release resources
            $searchResultIterator.Dispose()
        }
    }
    
    
    # Check whether file exists.
    if (!(Test-Path -Path $csvFilePath))
    {
        Write-Error "File '$csvFilePath' was not found."
        return
    }
    
    $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))
        {
            continue
        }
        
        # Run custom command for the user.
        $user = $admService.OpenObject($userPath, $NULL, $NULL, 0)
        $user.ExecuteCustomCommand($commandID, $null)
    }
    
    if ($errorReport.Length -ne 0)
    {
        Send-MailMessage -to $recipient -Body $errorReport.ToString() -Subject $subject -SmtpServer $mailServer -From $from
    }
}

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

if ($job.State -ne "Completed")
{
    return
}

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

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.