So I am not always able to make changes to users or add users in Active directory. I am trying to create a fallible form that can be used to input the pertinent information for new users in active directory. I am not sure what I am doing wrong or if this is even possible. My script is below .<\/p>\n
<# Author: Alex Graves\n This script creates a GUI interface to add new users information into specific OUs in AD.\n\nPrerequisites:\nActiveDirectory powershell module\n\n*****CHANGE LOG*****\n6/27/19 - Initial script creation \n\n#>\n\n<#Begin Script #>\n\n###################Load Assembly for creating form & button######\n\n[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)\n[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)\n\nTry\n{\n##Variables\n$first = \"\"\n$full = \"\"\n$user = \"\"\n$email = \"\"\n$Camp = \"\"\n$phone = \"\"\n\n#####Define the form size & placement\n\n$form = New-Object “System.Windows.Forms.Form”;\n$form.Width = 500;\n$form.Height = 325;\n$form.Text = \"Add New Users\";\n$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;\n\n##############Define text label1\n$textLabel1 = New-Object “System.Windows.Forms.Label”;\n$textLabel1.Left = 25;\n$textLabel1.Top = 10;\n$textLabel1.Text = \"Enter First Name\";\n\n##############Define text label2\n\n$textLabel2 = New-Object “System.Windows.Forms.Label”;\n$textLabel2.Left = 25;\n$textLabel2.Top = 50;\n$textLabel2.Text = \"Enter Full Name\";\n\n##############Define text label3\n\n$textLabel3 = New-Object “System.Windows.Forms.Label”;\n$textLabel3.Left = 25;\n$textLabel3.Top = 90;\n$textLabel3.Text = \"Enter Username\";\n\n##############Define text label4\n\n$textLabel4 = New-Object “System.Windows.Forms.Label”;\n$textLabel4.Left = 25;\n$textLabel4.Top = 130;\n$textLabel4.Text = \"Enter the User's Email Address\";\n\n##############Define text label5\n\n$textLabel5 = New-Object “System.Windows.Forms.Label”;\n$textLabel5.Left = 25;\n$textLabel5.Top = 170;\n$textLabel5.Text = \"Enter Campus\";\n\n##############Define text label6\n$textLabel6 = New-Object “System.Windows.Forms.Label”;\n$textLabel6.Left = 25;\n$textLabel6.Top = 210;\n$textLabel6.Text = \"Enter Phone Number\";\n\n############Define text box1 for input\n$textBox1 = New-Object “System.Windows.Forms.TextBox”;\n$textBox1.Left = 150;\n$textBox1.Top = 10;\n$textBox1.width = 200;\n\n############Define text box2 for input\n\n$textBox2 = New-Object “System.Windows.Forms.TextBox”;\n$textBox2.Left = 150;\n$textBox2.Top = 50;\n$textBox2.width = 200;\n\n############Define text box3 for input\n\n$textBox3 = New-Object “System.Windows.Forms.TextBox”;\n$textBox3.Left = 150;\n$textBox3.Top = 90;\n$textBox3.width = 200;\n\n############Define text box4 for input\n\n$textBox4 = New-Object “System.Windows.Forms.TextBox”;\n$textBox4.Left = 150;\n$textBox4.Top = 130;\n$textBox4.width = 200;\n\n############Define text box5 for input\n\n$textBox5 = New-Object “System.Windows.Forms.TextBox”;\n$textBox5.Left = 150;\n$textBox5.Top = 170;\n$textBox5.width = 200;\n\n############Define text box6 for input\n\n$textBox6 = New-Object “System.Windows.Forms.TextBox”;\n$textBox6.Left = 150;\n$textBox6.Top = 210;\n$textBox6.width = 200;\n\n#############Define default values for the input boxes\n$defaultValue = “”\n$textBox1.Text = $defaultValue;\n$textBox2.Text = $defaultValue;\n$textBox3.Text = $defaultValue;\n$textBox4.Text = $defaultValue;\n$textBox5.Text = $defaultValue;\n$textBox6.Text = $defaultValue;\n\n#############define OK button\n$button = New-Object “System.Windows.Forms.Button”;\n$button.Left = 360;\n$button.Top = 250;\n$button.Width = 100;\n$button.Text = “Ok”;\n\n############# This is when you have to close the form after getting values\n$eventHandler = [System.EventHandler]{\n$textBox1.Text;\n$textBox2.Text;\n$textBox3.Text;\n$textBox4.Text;\n$textBox5.Text;\n$textBox6.Text;\n$form.Close();};\n\n$button.Add_Click($eventHandler) ;\n\n#############Add controls to all the above objects defined\n$form.Controls.Add($button);\n$form.Controls.Add($textLabel1);\n$form.Controls.Add($textLabel2);\n$form.Controls.Add($textLabel3);\n$form.Controls.Add($textLabel4);\n$form.Controls.Add($textLabel5);\n$form.Controls.Add($textLabel6);\n\n$form.Controls.Add($textBox1);\n$form.Controls.Add($textBox2);\n$form.Controls.Add($textBox3);\n$form.Controls.Add($textBox4);\n$form.Controls.Add($textBox5);\n$form.Controls.Add($textBox6);\n\n$ret = $form.ShowDialog();\n\n#################return values\n\nreturn $first, $full, $user, $email, $Camp, $phone,\n\n$return= Button “Enter First Name” \"Enter Full Name\" \"Enter UserName\" \"Enter the Users Email Address\" “Enter Campus” \"Enter Phone Number\" \n\n#Below variables will get the values that had been entered by the user\n\n$first = $return[0]\n$full = $return[1]\n$user = $return[2]\n$email = $return[3]\n$camp = $return[4]\n$phone = $return[5] \n }\n\n#####Script to add the users to heir specific OUs within the AD environment#####\n \nFinally\n\n{\nImport-Module ActiveDirectory\n ForEach-Object{\n $full = $_.full \n $user = $_.user\n $email = $_.email\n $first = $_.first \n $camp = $_.camp \n $phone = $_.phone \n\nNew-ADUser -Name \"$full\" -AccountPassword (ConvertTo-SecureString “P@55W0rd!” -AsPlainText -force) -ChangePasswordAtLogon \"$true\" -City \"Hollywood\" -Company \"Example Inc.\" -Country \"United States\" -DisplayName \"$user\" -EmailAddress \"$email\" -Enabled \"$true\" -GivenName \"$first\" -Manager \"Joe Somebody\" -Office \"$camp\" -OfficePhone \"$phone\" -Path \"OU=example1A,OU=example,DC=camp3,DC=camp2,DC=local\" -SamAccountName \"$user\" -Surname \"$first\" -UserPrincipalName \"$email\"\n}\n}\n\n<\/code><\/pre>\n
Advertisement
When I run the script, the form comes up and I can put in the information. After I hit “OK” it looks like it is going to run and it spits out the following error<\/p>\n
Advertisement
New-ADUser : A positional parameter cannot be found that accepts argument ‘Hollywood Institute of Beauty Careers’.
\nAt line:199 char:1<\/p>\n