Powershell Forms: Join computers to a domain

How many times have you joined a computer to a domain and forgot to move it to the appropriate OU?
I've written this script to substitute the Windows interface in this job.

To execute the powershell script I use the following batch:

powershell -noprofile -executionpolicy "Unrestricted" .\join-domain.ps1
This is the powershell script:
$domain="domain.int"
$OUs="OU=Workstations,OU=USA,DC=domain,DC=int",
 "OU=Workstations,OU=Europe,DC=domain,DC=int",
 "OU=Workstations,OU=Asia,DC=domain,DC=int"

$credfile="$domain.cred"
$partofdomain=(gwmi "Win32_ComputerSystem").partofdomain
if($partofdomain -eq $true)
{
write-host "Computer is already member of a domain!"
$choice=read-host "`nDo you want to store credentials in a file to join computers in the domain unattended?(y/n)"
 if ($choice -eq "y")
 {
 read-host "domain\user"|out-file $credfile
 read-host "password" -assecurestring|ConvertFrom-SecureString -key(1..16)|out-file $credfile -append
 }
}
else
{
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
$MyForm = New-Object System.Windows.Forms.Form
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$MyForm.Icon = $Icon
$MyForm.Text = "SistemasWin | Join-Domain: $domain"
$MyForm.Size = New-Object System.Drawing.Size(455,70) 
$MyForm.StartPosition = "CenterScreen"
$MyForm.BackColor = [System.Drawing.Color]::SeaShell
$Myform.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$Myform.topMost=$true
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Point(5,5) 
$objLabel1.Size = New-Object System.Drawing.Size(30,20)
$objLabel1.Text = "OU:"
$MyForm.Controls.Add($objLabel1)
$objComboBox1 = New-Object System.Windows.Forms.ComboBox 
$objComboBox1.Location = New-Object System.Drawing.Point(30,5) 
$objComboBox1.Size = New-Object System.Drawing.Size(350,20) 
$objComboBox1.Name = "OUs"
$objComboBox1.items.addrange($OUs)
$objComboBox1.text=$OUs[0]
$objComboBox1.FlatStyle="Flat"
$objComboBox1.Font = New-Object System.Drawing.Font("Arial",7,0,3,0)
$MyForm.Controls.Add($objComboBox1)
$buttonselect = New-Object Windows.Forms.Button
$buttonselect.Location = New-Object System.Drawing.Point(385,5) 
$buttonselect.Size = New-Object System.Drawing.Size(50,20) 
$buttonselect.BackColor = [System.Drawing.Color]::MistyRose
$buttonselect.text="Join!"
$MyForm.Controls.Add($buttonselect) 
$buttonselect.Add_Click({
 switch ($buttonselect.text)
 {
 "Join!"{
  if (!(test-path -path $credfile)){$cred=get-credential}
  else{
   $storedcreds = get-content ($credfile)
   $username=$storedcreds[0]
   $password = ConvertTo-SecureString $storedcreds[1] -key (1..16)
   $cred = New-Object System.Management.Automation.PSCredential ($username, $password)
   }
  add-computer -domainname $domain -credential $cred -OUPath $objComboBox1.text -passthru
  if ($? -eq $true){$buttonselect.text="Restart!"}
  }
 "Restart!"{
  $MyForm.hide()
  $MyForm.dispose()
  restart-computer  
  }
 }#end switch
})
# Activates/draws the form.
$myForm.Add_Shown({$myForm.Activate()})
[void] $MyForm.ShowDialog()
}#end partofdomain
I've added the option to join computers to domain unattended jumping the step of entering credentials everytime.


If you execute the script in a computer that is already member of a domain it will offer the choice of saving encrypted credentials in a file.
It generates a file (domain.int.cred) in the script folder, and will use it next time you want to join a computer to the domain.

Comments