Binary Tree Documentation
Appendix F. Sample Admin Agent PowerShell Script
This is a sample PowerShell starter script that moves users from one OU to another. You may want to use this as a baseline on which to build your own custom scripts. Be sure to test your scripts in a non -production environment.
Param (
[System.String] $AdminUsername = $null,
[System.String] $AdminPassword = $null,
[System.String] $DomainController = $null,
[System.String] $TargetOU = $null
)
$WarningMessages = New-Object System.Collections.ArrayList try
{
#Active Directory Pro PowerShell Output Object
#This object is used to report status to the Active Directory Pro Console
$output = New-Object BinaryTree.ADMigrator.Agent.PSHelpers.PSOutput
#Verify input parameters are not $null.
if($AdminUsername -eq $null -OR $AdminPassword -eq $null -OR
$DomainController -eq $null -OR $TargetOU -eq $null)
{
#Write-Error will show in Active Directory Pro Log
Write-Error 'AdminUsername, AdminPassword, DomainController, and
TargetOU parameters are required.'
$output.ResultCode = 1
return ($output)
}
Active Directory Pro 10.2 User Guide
105
Binary Tree Documentation
#Output object's AgentFilePath is the download directory for the local agent installation
[System.String]$downloadsDirectory = $output.DownloadsPath
#Active Directory Pro Agent will download AdminAgent.csv file to the downloads directory before each job
#This CSV contains a list of Users to perform actions on
[System.String]$adminAgentCSVPath = Join-Path -Path $downloadsDirectory -
ChildPath 'AdminAgent.csv'
#Read AdminPassword input parameter and convert to a secure string to be used by a PSCredential object
$securePassword = $AdminPassword | ConvertTo-SecureString -AsPlainText -
Force
#Build the credential using $AdminUsername and $securePassword
$credential = New-Object
System.Management.Automation.PSCredential($AdminUsername, $securePassword)
#Create a Remote PowerShell Session to the server $DomainController and provide PSCredential object
$session = New-PSSession -ComputerName $DomainController -Credential
$credential
#Invoke-Command to ensure the ActiveDirectory modules are imported.
Invoke-Command -Session $session -ScriptBlock { Import-Module
ActiveDirectory }
#Merge the remote PowerShell Session's ActiveDirectory module to the local session
Import-PSSession -Session $session -Module ActiveDirectory -AllowClobber
#Verify $adminAgentCSVPath exists
if((Test-Path $adminAgentCSVPath) -eq $false)
{
Write-Error "Admin Agent CSV does not exist at path:
$adminAgentCSVPath"
Active Directory Pro 10.2 User Guide
106
Binary Tree Documentation
#Different ResultCodes can be used to troubleshoot script errors
$output.ResultCode = 2
return ($output)
}
#Read Admin Agent CSV into $users variable
$users = Import-CSV $adminAgentCSVPath
######## Admin Agent User Script Here ########
#For each user in the list
foreach($user in $users)
{
#Move the object to the $TargetOU
Move-ADObject -Identity $user.TargetDN -TargetPath $TargetOU -
Confirm:$false -Verbose
}
######## Admin Agent User Script Here ########
#ResultCode of 0 is a success, set and return
$output.ResultCode = 0
return ($output)
} catch
{
#Generic unexpected ResultCode
$output.ResultCode = 99
#Construct generic error message and include Exception message text.
$errorMessage = $_.Exception.Message
Active Directory Pro 10.2 User Guide
107
Write-Error "ERROR: $errorMessage"
Binary Tree Documentation
#Return $output object to Active Directory Pro Agent for reporting to
Active Directory Pro Console
return ($output)
}
Active Directory Pro 10.2 User Guide
108