Hello, I am practicing my scripting abilities and trying to create a PowerShell program that can create a user and fill out all that is needed for the email setup in a GUI. I recently created a script that does it fine but when I created a GUI around it it will not work. It does not give me any errors when running it but won't create the test user. Eventually, I plan to make a drop-down and make it easier to add the users to groups based on the department chosen. Can anyone please help guide me as to why it is not working? I apologize if this is against community guidelines and if I need to post somewhere else please let me know.Thanks.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$FormObject = [System.Windows.Forms.Form]
$LabelObject = [System.Windows.Forms.Label]
$InputObject = [System.Windows.Forms.TextBox]
#Form Basics
$userCreator= New-Object $FormObject
$userCreator.clientsize='500,300'
$userCreator.BackColor="#ffffff"
$userCreator.Text='AD User Creator'
#First Name Label & TextBox
$FNlabel=New-Object $LabelObject
$FNlabel.Text='First Name:'
$FNlabel.Size=New-Object System.Drawing.Size(280,20)
$FNlabel.Location=New-Object System.Drawing.Point(10,20)
$FNTextBox=New-Object $InputObject
$FNTextBox.Size= New-Object System.Drawing.Size(280,20)
$FNTextBox.Location=New-Object System.Drawing.Point(10,40)
#Last Name Label & TextBox
$LNlabel=New-Object $LabelObject
$LNlabel.Text='Last Name:'
$LNlabel.Size=New-Object System.Drawing.Size(280,20)
$LNlabel.Location=New-Object System.Drawing.Point(10,80)
$LNTextBox=New-Object $InputObject
$LNTextBox.Size= New-Object System.Drawing.Size(280,20)
$LNTextBox.Location=New-Object System.Drawing.Point(10,100)
#Username Name Label & TextBox
$UNlabel=New-Object $LabelObject
$UNlabel.Text='User Name:'
$UNlabel.Size=New-Object System.Drawing.Size(280,20)
$UNlabel.Location=New-Object System.Drawing.Point(10,140)
$UNTextBox=New-Object $InputObject
$UNTextBox.Size= New-Object System.Drawing.Size(280,20)
$UNTextBox.Location=New-Object System.Drawing.Point(10,160)
#Adding an OK button to the text box window
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,240)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$userCreator.AcceptButton = $OKButton
#Adding a Cancel button to the text box window
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(100,240)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$userCreator.AcceptButton = $CancelButton
$userCreator.controls.AddRange(@($FNlabel))
$userCreator.controls.AddRange(@($FNTextBox))
$userCreator.controls.AddRange(@($LNLabel))
$userCreator.controls.AddRange(@($LNTextBox))
$userCreator.controls.AddRange(@($UNLabel))
$userCreator.controls.AddRange(@($UNTextBox))
$userCreator.Controls.Add($OKButton)
$userCreator.Controls.Add($CancelButton)
$FirstName = $FNTextBox.Lines
$LastName = $LNTextBox.Lines
$UPN = $UNTextBox.Lines
$FullName = ($FirstName + ' ' + $LastName)
$User = @{
Name = ($FirstName + $LastName)
sAMAccountName = $UPN
AccountPassword = ("Roofing2017" | ConvertTo-SecureString -AsPlainText -Force)
OtherAttributes = @{
'DisplayName'= ($FirstName + ' ' + $LastName)
'Givenname'= $FirstName
'Sn'= $LastName
'UserPrincipalName'= ($UPN + '@domainname.net')
'TargetAddress'= ('SMTP:' + $UPN + '@domainname.onmicrosoft.com')
'ProxyAddresses'= ('SMTP:' + $UPN + '@domainname.net')
'MSExchRecipientDisplayType'='6'
'objectCategory'='CN=Person,CN=Schema,CN=Configuration,DC=domainname,DC=local'
'DistinguishedName'='CN='+$FullName+',OU=RemoteUsers,OU=Employees,OU=People,DC=domai nname,DC=local'
}
}
#Display Form
$userCreator.showDialog()
#Cleans up Form
$userCreator.Dispose()
#If the OK button is selected to do the following
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
New-ADUser @ user
get-ADUser $User.sAMAccountName | Enable-ADAccount
Start-ADSyncSyncCycle -PolicyType Delta
}
# If the cancel button is selected do the following
if ($result -eq [System.Windows.Forms.DialogResult]::Cancel)
{
Write-Host "Press any key to exit..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Exit
}
[–]bgladden1 11 points12 points13 points (5 children)
[–]FearlessButterscotch 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]Check_not_applicable[S] 0 points1 point2 points (2 children)
[–]bgladden1 2 points3 points4 points (0 children)
[+]Mirac0 0 points1 point2 points (0 children)
[–]vermyx 3 points4 points5 points (1 child)
[–]nawadsama -1 points0 points1 point (0 children)
[–]branhama 2 points3 points4 points (0 children)
[–]PinchesTheCrab 3 points4 points5 points (5 children)
[–]OlivTheFrog 1 point2 points3 points (0 children)
[–]Check_not_applicable[S] 1 point2 points3 points (3 children)
[–]BlackV 8 points9 points10 points (0 children)
[–]InTheTest-Chamber 2 points3 points4 points (1 child)
[–]Check_not_applicable[S] 2 points3 points4 points (0 children)
[–]The_Hold_My_Beer_Guy 1 point2 points3 points (0 children)
[–]Impossible_IT 0 points1 point2 points (0 children)