Script Repository


Export and import user and computer properties

May 13, 2016
1238

The scripts allow exporting properties of user and computer accounts in all domains managed by Adaxes to CSV files, editing them, and then importing back into Active Directory. The scripts uses 2 files, one file for users and one more for computers. For computers, only workstation computer accounts are included.

Script for exporting user and computer properties

This script can be used to export properties of all users and computers managed by Adaxes.

To be able to export properties, create a Custom Command configured for the Domain-DNS object type and run it on any of your AD domains.

Note: The selected domain does not limit the range of accounts included in the reports. It is used only to trigger the script.

Parameters:

  • $usersSCVFilePath - specifies a path to a CSV file that will contain properties of user accounts;
  • $computersCSVFilePath - specifies a path to a CSV file that will contain properties of computer accounts.
Edit Remove
PowerShell
$usersCSVFilePath = "\\Server\share\users.csv" # TODO: modify me
$computerCSVFilePath = "\\Server\share\computers.csv" # TODO: modify me

function BuildReport($filter, $properties)
{
    # Search objects in all domains
    $searcher = $Context.BindToObject("Adaxes://rootDSE")
    $searcher.SearchFilter = $filter
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 500
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SetPropertiesToLoad($properties)
    $searcher.VirtualRoot = $True
    
    try
    {
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        foreach ($searchResult in $searchResults)
        {
            # Add object to report
            $record = New-Object PSObject
            foreach ($propertyName in $properties)
            {
                $record | Add-Member -MemberType NoteProperty -Name $propertyName -Value $searchResult.Properties[$propertyName].Value
            }
            $record
        }
    }
    finally
    {
        $searchResultIterator.Dispose()
    }
}

# Create CSV file for users
$userReport = BuildReport "(sAMAccountType=805306368)" @("telephoneNumber", "mobile", "ipPhone", "mail", "manager", "distinguishedName")
$userReport | Export-Csv -NoTypeInformation -Path $usersCSVFilePath

# Create CSV file for computers
$computerReport = BuildReport "(&(objectCategory=computer)(!(userAccountControl:1.2.840.113556.1.4.803:=8192)))" @("managedBy", "distinguishedName")
$computerReport | Export-Csv -NoTypeInformation -Path $computerCSVFilePath

Script for importing user and computer properties

This script can be used to import properties of users and computers from CSV files to Active Directory.

To be able to import properties, create a Custom Command configured for the Domain-DNS object type and run it on any of your AD domains.

Note: The selected domain does not limit the range of accounts included in the reports. It is used only to trigger the script.

Parameters:

  • $usersSCVFilePath - specifies a path to a CSV file that will contain properties of user accounts;
  • $computersCSVFilePath - specifies a path to a CSV file that will contain properties of computer accounts.
Edit Remove
PowerShell
$usersCSVFilePath = "\\Server\share\users.csv" # TODO: modify me
$computerCSVFilePath = "\\Server\share\computers.csv" # TODO: modify me

function UpdateObjects($objects)
{
    $propertiesToCheck = New-Object System.Collections.ArrayList
    $objects[0].PSObject.Properties | %%{[void]$propertiesToCheck.Add($_.Name)}
    $propertiesToCheck.Remove("distinguishedName")
    
    foreach ($objectInfo in $objects)
    {
        # Bind to object
        $objectDN = $objectInfo.distinguishedName
        $object = $Context.BindToObjectByDN($objectDN)
        
        # Update properties
        foreach ($propertyName in $propertiesToCheck)
        {
            $value = $objectInfo."$propertyName"
            if ([System.String]::IsNullOrEmpty($value))
            {
                $value = $NULL
            }
            $object.Put($propertyName, $value)
        }
        
        try
        {
            $object.SetInfo()
        }
        catch
        {
            $Context.LogMessage("An error occurred while updating the object '$objectDN'. Error: " + $_.Exception.Message, "Warning")
        }
    }
}

# Import users
if (Test-Path -Path $usersCSVFilePath)
{
    [Object[]]$users = Import-Csv -Path $usersCSVFilePath
    UpdateObjects $users
}
else
{
    $Context.LogMessage("File '$usersCSVFilePath' was not found.", "Warning")
}

# Import computers
if (Test-Path -Path $computerCSVFilePath)
{
    [Object[]]$computers = Import-Csv -Path $computerCSVFilePath
    UpdateObjects $computers
}
else
{
    $Context.LogMessage("File '$computerCSVFilePath' was not found.", "Warning")
}

Comments ( 0 )
No results found.
Leave a comment