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.

Script Repository

Add computers from an OU to user workstations

January 26, 2022 Views: 1196

The script adds computers from a specific Organizational Unit to user workstations. Current computers in user workstations will remain unchanged. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

In the script, the $OUDN variable specifies the distinguished name (DN) of the Organizational Unit to search computers in.

Edit Remove
$OUDN = "OU=Computers,DC=domain,DC=com" #TODO: modify me

# Get current logon workstations
    $logonWorkstations = $Context.TargetObject.Get("userWorkstations")
    $logonWorkstations = ""

# Search parameters
$searcher = $Context.BindToObjectByDN($OUDN)
$searcher.SearchFilter = "(&(objectCategory=computer)(dNSHostName=*))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500

    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    if ($searchResults.Length -eq 0)
        $Context.LogMessage("No computers were found.", "Information")
    foreach ($searchResult in $searchResults)
        # Get computer DNS host name
        $computerDNSHostName = $searchResult.GetPropertyByName("dNSHostName").Values[0]        
        # Update workstations list
        if ($logonWorkstations -notlike "*$computerDNSHostName*")
            $logonWorkstations = $logonWorkstations + "," + $computerDNSHostName
    # Update the user
    $Context.TargetObject.Put("userWorkstations", $logonWorkstations)
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
Comments 0
Leave a comment

Got questions?

Support Questions & Answers