K2000: Script for adding computers to different OU
Answers (3)
You will need to declare an Array with your OUs inside, parse them into a dropwdown menu, and then have the Script run with the selected value.
If this is an urgent matter, you might want to consider sending an email to KACE Professional Services, they are good with Customization and Tailored Scripts, maybe there is a way to query AD and fill an Array with that query results, to avoid having to update the script every-time a new OU appears , please email them at remoteconfig@quest.com . Before any work is done, they will speak to you and build a statement of work; as well as provide you with a quote.
Here is a good example of Powershell with GUI menu.
https://sysadminemporium.wordpress.com/2012/11/27/powershell-gui-for-your-scripts-episode-2/
Here is the Script we use to target an OU manually:
$domain= "DOMAIN.COM"
$password= "PASSWORD" | ConvertTo-SecureString -asPlainText -Force
$user= "$domain\USER"
$cred= New-Object System.Management.Automation.PSCredential($user,$password)
$oupath = "CN=Computers,DC=DOMAIN,DC=COM"
Add-Computer -domainname $domain -oupath $oupath -Credential $cred -ErrorAction silentlycontinue
Add-Computer -DomainName $domain -Credential $cred
I don't do much in the K2000 anymore, but I did the initial setup about a year ago. I believe there are multiple times during the installation where the computer can be added to the domain. We're doing it as a post-installation task.
Below is a modification of the script my coworker wrote for our K2000 to deploy Windows 10. We're deploying to a single OU, so we have a path hard coded, but I changed it to use Out-Gridview so a user can select which OU the computer should go in. You can copy and paste the OU part into the command line to see how the OU selection process works.
A couple of caveats
I haven't tested this modified version
I removed some other hard coded items (domain, username, password, etc.)
I don't think I screwed up the basic function of the script
I haven't test this!
You'll need to upload the script to the K2000, then set the Full Command Line to something like:
powershell -nologo -executionpolicy bypass -noprofile -file JoinDomain.ps1
###########
###########
# Set User account which has permissions to join a computer to the domain
###########
###########
$ComputerDomain = 'test.forest'
$UserDomain = 'test.forest'
$UserName = "$UserDomain\KaceDeploy"
$Password = 'djkl3Hjklds2#' | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $UserName, $PassWord
###########
###########
# Define the OUs here where the computers can be added to
###########
###########
$OUs = $(
New-Object -TypeName PSObject -Property @{Name = 'OU 1'; OU = 'ou=OU 1;dc=Test;dc=forest'}
New-Object -TypeName PSObject -Property @{Name = 'OU 2'; OU = 'ou=OU 2;dc=Test;dc=forest'}
New-Object -TypeName PSObject -Property @{Name = 'OU 3'; OU = 'ou=OU 3;dc=Test;dc=forest'}
New-Object -TypeName PSObject -Property @{Name = 'OU 4'; OU = 'ou=OU 4;dc=Test;dc=forest'}
)
Do {
$OUPath = ($OUs | Sort Name | Select Name, OU | Out-GridView -PassThru -Title 'Please select an OU to put the computer in').OU
} While ($OUPath.Count -ne 1)
###########
###########
# Verify computer is connected to the network
# Over a WAN this sometimes times out if the network is slow
# Wait 2 minutes between checks
###########
###########
$NetworkCount = 1
Write-Host "Testing Network Connection to domain"
if ((Test-Connection $ComputerDomain -Count 1 -Quiet) -eq $false){
###########
# Use the voice synthesizer to warn the admin if there are network problems
###########
Add-Type -AssemblyName System.Speech
$synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
Do{
Write-Host "Testing Network Connection to $ComputerDomain.... $NetworkCount"
$NetworkCount++
Start-Sleep -s 2
if (($NetworkCount%60) -eq 0){ #after a time (2 minutes) of no activity play message
$synthesizer.Speak("Warning, Network connection has failed.")
}
} Until (Test-Connection $ComputerDomain -Count 1 -Quiet)
}
Write-Host "Connected."
###########
###########
# Add computer to domain.
###########
###########
Write-Host "Adding Computer to Domain."
Try{
Add-Computer -DomainName $ComputerDomain -OUPath $OUPath -Credential $Credential -ErrorAction Stop
} catch {
Write-Host "Failed to add to Active Directory" -ForegroundColor Red
Write-Host "Check if Computer Name is already part of Active Directory." -ForegroundColor Yellow
Pause
Throw "Error, stopping KACE install"
}