#Name: FreeCompmaggus v. 1.0

#Description: Utility for creating an arbitrary number of domain joinable Active Directory computer objects in a designated container.

#Update History:

#1.0- Original release. 06/17/15

#Copyright 2015, Luther Clark Jr. (lutherinbmore@gmail.com)

# This program is free software: you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation, either version 3 of the License, or

# (at your option) any later version.

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License

# along with this program. If not, see <http://www.gnu.org/licenses/>.

#region Script Settings

#<ScriptSettings xmlns="http://tempuri.org/ScriptSettings.xsd">

# <ScriptPackager>

# <process>powershell.exe</process>

# <arguments />

# <extractdir>%TEMP%</extractdir>

# <files />

# <usedefaulticon>true</usedefaulticon>

# <icon>Z:\RHEL 7\LAN-128.ico</icon>

# <showinsystray>false</showinsystray>

# <altcreds>false</altcreds>

# <efs>true</efs>

# <ntfs>true</ntfs>

# <local>false</local>

# <abortonfail>true</abortonfail>

# <product />

# <version>1.0.0.1</version>

# <versionstring />

# <comments />

# <company />

# <includeinterpreter>false</includeinterpreter>

# <forcecomregistration>false</forcecomregistration>

# <consolemode>false</consolemode>

# <EnableChangelog>false</EnableChangelog>

# <AutoBackup>false</AutoBackup>

# <snapinforce>false</snapinforce>

# <snapinshowprogress>false</snapinshowprogress>

# <snapinautoadd>2</snapinautoadd>

# <snapinpermanentpath />

# <cpumode>1</cpumode>

# <hidepsconsole>false</hidepsconsole>

# </ScriptPackager>

#</ScriptSettings>

#endregion

#region ScriptForm Designer

#region Constructor

[ void ] [ System.Reflection.Assembly ] :: LoadWithPartialName ( "System.Windows.Forms" )

[ void ] [ System.Reflection.Assembly ] :: LoadWithPartialName ( "System.Drawing" )

#endregion

#region Post-Constructor Custom Code

#endregion

#region Form Creation

#Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.

#When working with the ScriptForm designer this region and any changes within may be overwritten.

#~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$Form1 = New-Object System.Windows.Forms.Form

$Form1 .ClientSize = New-Object System.Drawing.Size ( 532 , 163 )

$Form1 .Text = "Compmaggus v. 1.0"

#~~< GroupBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$GroupBox2 = New-Object System.Windows.Forms.GroupBox

$GroupBox2 .Location = New-Object System.Drawing.Point ( 12 , 68 )

$GroupBox2 .Size = New-Object System.Drawing.Size ( 248 , 47 )

$GroupBox2 .TabIndex = 13

$GroupBox2 .TabStop = $false

$GroupBox2 .Text = "Distinguished Name of Domain Join Group"

#~~< TextBox3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$TextBox3 = New-Object System.Windows.Forms.TextBox

$TextBox3 .Location = New-Object System.Drawing.Point ( 7 , 18 )

$TextBox3 .Size = New-Object System.Drawing.Size ( 235 , 20 )

$TextBox3 .TabIndex = 0

$TextBox3 .Text = ""

$GroupBox2 .Controls.Add ( $TextBox3 )

#~~< GroupBox3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$GroupBox3 = New-Object System.Windows.Forms.GroupBox

$GroupBox3 .Location = New-Object System.Drawing.Point ( 270 , 12 )

$GroupBox3 .Size = New-Object System.Drawing.Size ( 257 , 47 )

$GroupBox3 .TabIndex = 12

$GroupBox3 .TabStop = $false

$GroupBox3 .Text = "Distinguished Name Excluding CN"

#~~< TextBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$TextBox2 = New-Object System.Windows.Forms.TextBox

$TextBox2 .Location = New-Object System.Drawing.Point ( 7 , 18 )

$TextBox2 .Size = New-Object System.Drawing.Size ( 244 , 20 )

$TextBox2 .TabIndex = 0

$TextBox2 .Text = ""

$GroupBox3 .Controls.Add ( $TextBox2 )

#~~< CheckBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$CheckBox1 = New-Object System.Windows.Forms.CheckBox

$CheckBox1 .Location = New-Object System.Drawing.Point ( 18 , 127 )

$CheckBox1 .Size = New-Object System.Drawing.Size ( 213 , 24 )

$CheckBox1 .TabIndex = 11

$CheckBox1 .Text = "Add Leading Zeros to Computer Name"

$CheckBox1 .UseVisualStyleBackColor = $true

#~~< GroupBox6 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$GroupBox6 = New-Object System.Windows.Forms.GroupBox

$GroupBox6 .Location = New-Object System.Drawing.Point ( 270 , 66 )

$GroupBox6 .Size = New-Object System.Drawing.Size ( 153 , 49 )

$GroupBox6 .TabIndex = 10

$GroupBox6 .TabStop = $false

$GroupBox6 .Text = "Progress"

#~~< ProgressBar1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$ProgressBar1 = New-Object System.Windows.Forms.ProgressBar

$ProgressBar1 .Location = New-Object System.Drawing.Point ( 6 , 20 )

$ProgressBar1 .Size = New-Object System.Drawing.Size ( 133 , 23 )

$ProgressBar1 .TabIndex = 9

$ProgressBar1 .Text = ""

$ProgressBar1 .add_Click ( { ProgressBar1Click ( $ProgressBar1 ) } )

$GroupBox6 .Controls.Add ( $ProgressBar1 )

$GroupBox6 .add_Enter ( { GroupBox6Enter ( $GroupBox6 ) } )

#~~< Button1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$Button1 = New-Object System.Windows.Forms.Button

$Button1 .Location = New-Object System.Drawing.Point ( 429 , 86 )

$Button1 .Size = New-Object System.Drawing.Size ( 98 , 27 )

$Button1 .TabIndex = 8

$Button1 .Text = "Submit"

$Button1 .UseVisualStyleBackColor = $true

$Button1 .add_Click ( { makeADComputers ( $Button1 ) } )

#~~< GroupBox5 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$GroupBox5 = New-Object System.Windows.Forms.GroupBox

$GroupBox5 .Location = New-Object System.Drawing.Point ( 183 , 12 )

$GroupBox5 .Size = New-Object System.Drawing.Size ( 77 , 48 )

$GroupBox5 .TabIndex = 7

$GroupBox5 .TabStop = $false

$GroupBox5 .Text = "Number"

#~~< NumericUpDown1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$NumericUpDown1 = New-Object System.Windows.Forms.NumericUpDown

$NumericUpDown1 .Location = New-Object System.Drawing.Point ( 6 , 19 )

$NumericUpDown1 .Size = New-Object System.Drawing.Size ( 42 , 20 )

$NumericUpDown1 .TabIndex = 6

$GroupBox5 .Controls.Add ( $NumericUpDown1 )

#~~< GroupBox4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$GroupBox4 = New-Object System.Windows.Forms.GroupBox

$GroupBox4 .Location = New-Object System.Drawing.Point ( 12 , 12 )

$GroupBox4 .Size = New-Object System.Drawing.Size ( 165 , 47 )

$GroupBox4 .TabIndex = 5

$GroupBox4 .TabStop = $false

$GroupBox4 .Text = "Base Computer Name"

#~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$TextBox1 = New-Object System.Windows.Forms.TextBox

$TextBox1 .Location = New-Object System.Drawing.Point ( 6 , 19 )

$TextBox1 .Size = New-Object System.Drawing.Size ( 152 , 20 )

$TextBox1 .TabIndex = 0

$TextBox1 .Text = ""

$TextBox1 .add_TextChanged ( { TextBox1TextChanged ( $TextBox1 ) } )

$GroupBox4 .Controls.Add ( $TextBox1 )

$GroupBox4 .add_Enter ( { GroupBox4Enter ( $GroupBox4 ) } )

$Form1 .Controls.Add ( $GroupBox2 )

$Form1 .Controls.Add ( $GroupBox3 )

$Form1 .Controls.Add ( $CheckBox1 )

$Form1 .Controls.Add ( $GroupBox6 )

$Form1 .Controls.Add ( $Button1 )

$Form1 .Controls.Add ( $GroupBox5 )

$Form1 .Controls.Add ( $GroupBox4 )

#endregion

#region Custom Code

#endregion

#region Event Loop

function Main {

[ System.Windows.Forms.Application ] ::EnableVisualStyles ( )

[ System.Windows.Forms.Application ] ::Run ( $Form1 )

}

#endregion

#endregion

#region Event Handlers

function ProgressBar1Click ( $object ) {

}

function GroupBox6Enter ( $object ) {

}

function makeADComputers ( $object ) {

#Pull the total number of computers to create.

$total = $NumericUpDown1 .Value

#Setup the progress bar

$ProgressBar1 .Maximum = $total * 2 + 2

$ProgressBar1 .Value = 0

#Get the Distinguished Name for the Path switch and the DN for the Domain Join Group

$PATHDN = $TextBox2 .Text

$JOINGROUPDN = $TextBox3 .Text

#Set the alphabetical portion of the object name and create a query based on it.

$UNNUMB = $TextBox1 .Text

$QUERY = "$UNNUMB*"

#Increment the progress bar

$ProgressBar1 .Value ++

#Use the query to obtain the highest numbered computer object name.

$COMPY = get - adcomputer - filter { Name -like $QUERY } | ForEach-Object { echo $_ .Name } | Select-Object -last 1

$TEST = $COMPY -match "[0-9]*[0-9]*[0-9]"

$lastNUM = [ int ] $matches [ 0 ]

$count = 0

#Increment the progress bar

$ProgressBar1 .Value ++

#Start a loop to create the new accounts and change the permissions so that members of the Domain Join group can join them.

while ( $count -lt $total )

{

#Gotta make sure that you aren't trying to create an already existing object.

$lastNUM ++

#If required, add the leading zeros back in (they were stripped out when the $lastNUM became an integer).

if ( $CheckBox1 .Checked )

{

if ( $lastNUM -match "^[0-9]$" )

{

$lastSTRING = "00$lastNUM"

}

elseif ( $lastNUM -match "^[0-9][0-9]$" )

{

$lastSTRING = "0$lastNUM"

}

else

{

$lastSTRING = [ string ] $lastNUM

}

}

#Make the new object.

new - adcomputer -name $UNNUMB $lastSTRING - SAMAccountName "$UNNUMB$lastSTRING `$ " -Path $PATHDN

#Increment the progress bar

$ProgressBar1 .Value ++

#Update the Access Control List

dsacls "CN=$UNNUMB$lastSTRING,$PATHDN" / G "$JOINGROUPDN:CA;Reset password" "$JOINGROUPDN:CA;Send as" "$JOINGROUPDN:CA;Receive as" "$JOINGROUPDN:CA;Change password" "$JOINGROUPDN:CA;Allowed to authenticate" "$JOINGROUPDN:WP;Account restrictions" "$JOINGROUPDN:WS;Validated write to service principal name" "$JOINGROUPDN:WS;Validated write to DNS host name" "$JOINGROUPDN:GR"

#Increment the progress bar again

$ProgressBar1 .Value ++

$count ++

}

#[System.Windows.Forms.MessageBox]::Show("$UNNUMB")

}

function TextBox1TextChanged ( $object ) {

}

function GroupBox4Enter ( $object ) {

}

Main # This call must remain below all other event functions