/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


K2000: Script for adding computers to different OU

08/14/2018 1688 views
I want to create a script where I can choose Organizational Unit (OU) from a drop down menu during the K2000 deployment and then add the computer to the correct OU (We have more than 10 different OU and I don't want to create a different scripted installation for everyone of them) . Is there anyway I could do it in k2000?

The best would be if I could add a custom description to the computer.  
0 Comments   [ + ] Show comments

Comments



Community Chosen Answer

1
I have solved it (sorry can't find where to format it as code)

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$DropdownArray=@("OU=test,DC=test,DC=com",
"OU=compapany,DC=test,DC=com",
"OU=company1,DC=test,DC=com",)


$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select an OU'
$form.Size = New-Object System.Drawing.Size(500,600)
$form.StartPosition = 'CenterScreen'

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,500)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,500)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(380,20)
$label.Text = 'Please select an Organizational Unit:'
$form.Controls.Add($label)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(400,500)
$listBox.Height = 450

ForEach ($Item in $DropDownArray) {
     [void] $listBox.Items.Add($Item)
    }

$form.Controls.Add($listBox)

$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $oupath = $listBox.SelectedItem
    $oupath
    $domain= "test.com"
    $cred = New-Object System.Management.Automation.PsCredential("XXXXX", (ConvertTo-SecureString XXXXX -AsPlainText -Force))
    Add-Computer -domainname $domain -oupath $oupath -Credential $cred -ErrorAction silentlycontinue 
}


Answered 08/22/2018 by: ismazs
White Belt

All Answers

0
This one is a Powershell Script, you can manually type the Path, 

but for a drop-down menu Script type.... a Powershell GUI is needed.... to capture your selection and then join the domain.

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
Answered 08/14/2018 by: Channeler
Red Belt

0

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"
}

Answered 08/16/2018 by: PaulGibson
Senior Yellow Belt

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ