Powershell Forms: Meter maquinas a dominio

Cuantas veces habeis metido una maquina a dominio y luego se os ha olvidado arrastrarla a la OU correspondiente?
He hecho el siguiente script para sustituir la interfaz de Windows en esta tarea.

Para ejecutar el script powershell uso el siguiente bat

powershell -noprofile -executionpolicy "Unrestricted" .\join-domain.ps1
Este es el script powershell:
$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 "El equipo ya es miembro de un dominio!"
$choice=read-host "`nQuieres aprovechar para almacenar las credenciales en un fichero para poder meter maquinas a dominio de una manera desatendida?(y/n)"
 if ($choice -eq "y")
 {
 read-host "dominio\usuario"|out-file $credfile
 read-host "contraseña" -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
He añadido la opción de meter la maquina de una manera “mas desatendida” ahorrando el paso de meter las credenciales.


Si se ejecuta el script en una maquina que ya pertenezca a un dominio, ofrece la opción de crear un fichero con las credenciales encriptadas.
Esto genera un fichero (domain.int.cred) en la misma carpeta que los scripts, y así os ahorrais meter las credenciales cada vez.

Comentarios