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] $Credentials_Username = $null,
[System.String] $Credentials_Password = $null,
[System.String] $DomainController = $null,
[System.String] $TargetOU = $null
)
$WarningMessages = New-Object System.Collections.ArrayList
try
{
#Migrator Pro for Active Directory PowerShell Output Object
#This object is used to report status to the Migrator Pro for Active Directory Console
$output = New-Object BinaryTree.ADMigrator.Agent.PSHelpers.PSOutput
#Verify input parameters are not $null.
if($Credentials_Username -eq $null -OR $Credentials_Password -eq $null -OR $DomainController -eq $null -OR $TargetOU -eq $null)
{
#Write-Error will show in Migrator Pro for Active Directory Log
Write-Error 'Credentials_Username, Credentials_Password, DomainController, and TargetOU parameters are required.'
$output.ResultCode = 1
return ($output)
}
#Output object's AgentFilePath is the download directory for the local agent installation
[System.String]$downloadsDirectory = $output.DownloadsPath
#Migrator Pro for Active Directory 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 Credentials_Password input parameter and convert to a secure string to be used by a PSCredential object
$securePassword = $Credentials_Password | ConvertTo-SecureString -AsPlainText -Force
#Build the credential using $Credentials_Username and $securePassword
$credential = New-Object System.Management.Automation.PSCredential($Credentials_Username, $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"
#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
Write-Error "ERROR: $errorMessage"
#Return $output object to Migrator Pro for Active Directory Agent for reporting to Migrator Pro for Active Directory Console
return ($output)
}
© ALL RIGHTS RESERVED. Feedback 이용 약관 개인정보 보호정책 Cookie Preference Center