Search code examples
powershelljoindns

Automatic Domain Join


i want to automate the domain join.

The admin should define in which OU the Computer will be assigned.

Therefore i used a Listbox.

The result should be compared with the predifined variables for the spezific distinguished Name of the OUs and write the string in a variable to use it in the following command:

Add-Computer -ComputerName $hostname -DomainName "domain.de" -OUPath $OUpathVariable

I dont know how to compare the result with the predefined variables of the spezific OUs and how i get them in the command above.

I could use many if-loops like that - but there has to be a much smarter way i guess.

if ($OU_result -eq $OU_1)
{
Add-Computer -ComputerName $hostname -DomainName "domain.de" -OUPath $OU_1
}

if ($OU_result -eq $OU_2)
{
Add-Computer -ComputerName $hostname -DomainName "domain.de" -OUPath $OU_2
}

if ($OU_result -eq $OU_3)
{
Add-Computer -ComputerName $hostname -DomainName "domain.de" -OUPath $OU_3
}

**the hostname will be predifed in a seperatly

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'AD-OU?'
$form.Size = New-Object System.Drawing.Size(300,360)
$form.StartPosition = 'CenterScreen'

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,290)
$OKButton.Size = New-Object System.Drawing.Size(188,23)
$OKButton.Text = 'Start DomainJoin'
$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(205,290)
$CancelButton.Size = New-Object System.Drawing.Size(65,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(280,20)
$label.Text = 'Select Computer-OU:'
$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(260,20)

$listBox.SelectionMode = 'MultiExtended'

[void] $listBox.Items.Add('OU1')
[void] $listBox.Items.Add('OU2')
[void] $listBox.Items.Add('OU3')
[void] $listBox.Items.Add('OU4')
[void] $listBox.Items.Add('OU5')
[void] $listBox.Items.Add('OU6')
[void] $listBox.Items.Add('OU7')
[void] $listBox.Items.Add('OU8')
[void] $listBox.Items.Add('OU9')
[void] $listBox.Items.Add('OU10')
[void] $listBox.Items.Add('OU11')
[void] $listBox.Items.Add('OU12')
[void] $listBox.Items.Add('OU13')
[void] $listBox.Items.Add('OU14')
[void] $listBox.Items.Add('OU15')
[void] $listBox.Items.Add('OU16')
[void] $listBox.Items.Add('OU17')
[void] $listBox.Items.Add('OU18')

$listBox.Height = 250
$form.Controls.Add($listBox)
$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $OU_result = $listBox.SelectedItems
}

$OU_1 =                   "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_2 =               "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_3 =               "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_4 =                   "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_5 =                   "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_6 =                   "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_7 =                       "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_8 =                   "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_9 =                   "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_10 =                  "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_11 =              "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_12 =              "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_13 =              "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_14 =              "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_15 =              "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_16 =                      "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_17 =                      "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"
$OU_18 =              "OU=Computer,OU=XYZ,OU=XYZ,DC=domain,DC=de"

if ($OU_result -eq #one of the variables above)
{
#then take the spezific string and define it as $OUpathVariable
}
Add-Computer -ComputerName $hostname -DomainName "domain.de" -OUPath $OUpathVariable


Solution

  • I could use many if-loops like that - but there has to be a much smarter way i guess.

    There is a smarter way!

    Simply test that the input falls into the accepted range of values and then pass it directly:

    if (@($OU_1,$OU_2,$OU_3,...,$OU_N) -contains $OU_result) {
        # valid response, proceed with the join
        Add-Computer -ComputerName $hostname -DomainName "domain.de" -OUPath $OU_result
    }
    else {
        # invalid OU result, fail or prompt the user again
    }
    

    If you need to translate the user response (eg. allow the user to pick "The IT Department OU") to a valid OU, organize the response->OU pairs into a dictionary and use that to "look up" the correct target OU:

    $OUMapping = @{
        "IT Department" = "OU=Endpoints,OU=Computers,OU=IT,DC=domain,DC=tld"
        "Marketing"     = "OU=Computers,OU=Marketing,DC=domain,DC=tld"
        "Accounting"    = "OU=Laptops,OU=Accounting,DC=domain,DC=tld"
    }
    

    Then for processing the response you'd test that the mapping table contains the chosen department and grab the corresponding value:

    if ($OUMapping.ContainsKey($OU_result)) {
        # valid response, proceed with the join with corresponding OU from mapping table
        Add-Computer -ComputerName $hostname -DomainName "domain.de" -OUPath $OUMapping[$OU_result]
    }
    else {
        # invalid OU result, fail or prompt the user again
    }