I have a powershell GUI to create groups in active directory. The GUI looks like this.
Actually I can't manage to define the options in lines 16-21 based on my selection in the GUI to create the groups and then assign the appropriate command to the Create button. I hope someone can help me today.
The code looks like
#### Form settings ################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "AD GROUP CREATER"
$Form.Size = New-Object System.Drawing.Size(750,470)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
$options = @{
Name = $textbox.Text
GroupScope =
GroupCategory =
Path =
}
#### Label settings #################################################################
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,10)
$Label.Size = New-Object System.Drawing.Size(280,20)
$Label.Text = "CREATE IN"
$Label.Font = "8,style=bold"
$Label.Forecolor = "black"
$Form.Controls.Add($Label)
#### Group box ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,30)
$groupBox.size = New-Object System.Drawing.Size(320,50)
$Form.Controls.Add($groupBox)
#### A button ###################################################################
$SPSG = New-Object System.Windows.Forms.RadioButton
$SPSG.Location = New-Object System.Drawing.Size(15,20)
$spsg.Size = New-Object System.Drawing.Size(100,20)
$spsg.Text = "A"
$spsg.Cursor = [System.Windows.Forms.Cursors]::Hand
$spsg.Add_Click({})
$groupBox.Controls.Add($spsg)
#### B button ###################################################################
$FSG = New-Object System.Windows.Forms.RadioButton
$FSG.Location = New-Object System.Drawing.Size(150,20)
$Fsg.Size = New-Object System.Drawing.Size(150,20)
$Fsg.Text = "B"
$Fsg.Cursor = [System.Windows.Forms.Cursors]::Hand
$Fsg.Add_Click({})
$groupBox.Controls.Add($Fsg)
#### Label 2 settings #################################################################
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,100)
$Label2.Size = New-Object System.Drawing.Size(280,20)
$Label2.Text = "GROUP NAME"
$label2.Font = "8, style=bold"
$Label2.Forecolor = "black"
$Form.Controls.Add($Label2)
#### textbox settings #################################################################
$Textbox = New-Object System.Windows.Forms.TextBox
$Textbox.Location = New-Object System.Drawing.Size(10,120)
$Textbox.Size = New-Object System.Drawing.Size(320,20)
$Form.Controls.Add($Textbox)
#### Label 3 settings #################################################################
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,170)
$Label3.Size = New-Object System.Drawing.Size(310,20)
$Label3.Text = "GROUP SECTION"
$Label3.Forecolor = "black"
$label3.Font = "8, style=bold"
$Form.Controls.Add($Label3)
#### Group box 2 ########################################################
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$groupBox2.Location = New-Object System.Drawing.Size(10,190)
$groupBox2.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox2)
#### local button ###################################################################
$lokal = New-Object System.Windows.Forms.RadioButton
$lokal.Location = New-Object System.Drawing.Size(15,20)
$lokal.Size = New-Object System.Drawing.Size(150,20)
$lokal.Text = "Local"
$lokal.Cursor = [System.Windows.Forms.Cursors]::Hand
$lokal.Add_Click({})
$groupBox2.Controls.Add($lokal)
#### global button ###################################################################
$global = New-Object System.Windows.Forms.RadioButton
$global.Location = New-Object System.Drawing.Size(15,70)
$global.Size = New-Object System.Drawing.Size(150,20)
$global.Text = "Global"
$global.Cursor = [System.Windows.Forms.Cursors]::Hand
$global.Add_Click({})
$groupBox2.Controls.Add($global)
#### universal button ###################################################################
$universal = New-Object System.Windows.Forms.RadioButton
$universal.Location = New-Object System.Drawing.Size(15,120)
$universal.Size = New-Object System.Drawing.Size(150,20)
$universal.Text = "Universal"
$universal.Cursor = [System.Windows.Forms.Cursors]::Hand
$universal.Add_Click({})
$groupBox2.Controls.Add($universal)
#### Label 4 settings #################################################################
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Location = New-Object System.Drawing.Size(400,170)
$Label4.Size = New-Object System.Drawing.Size(320,20)
$Label4.Text = "GROUP TYPE"
$Label4.Forecolor = "black"
$label4.Font = "8, style=bold"
$Form.Controls.Add($Label4)
#### Group box 3 ########################################################
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$groupBox3.Location = New-Object System.Drawing.Size(400,190)
$groupBox3.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox3)
#### security button ###################################################################
$security = New-Object System.Windows.Forms.RadioButton
$security.Location = New-Object System.Drawing.Size(15,20)
$security.Size = New-Object System.Drawing.Size(150,20)
$security.Text = "Security"
$security.Cursor = [System.Windows.Forms.Cursors]::Hand
$security.Add_Click({})
$groupBox3.Controls.Add($security)
#### Distribution button ###################################################################
$Distribution = New-Object System.Windows.Forms.RadioButton
$Distribution.Location = New-Object System.Drawing.Size(15,70)
$Distribution.Size = New-Object System.Drawing.Size(150,20)
$Distribution.Text = "Distribution"
$Distribution.Cursor = [System.Windows.Forms.Cursors]::Hand
$Distribution.Add_Click({})
$groupBox3.Controls.Add($Distribution)
#### Create button ###################################################################
$Create = New-Object System.Windows.Forms.Button
$Create.Location = New-Object System.Drawing.Size(10,370)
$Create.Text = "Create"
$Create.Cursor = [System.Windows.Forms.Cursors]::Hand
$Create.Width = 710
$Create.Height = 50
$Create.Font = "12,style=bold"
$Create.Add_Click({pinginfo})
$Form.Controls.Add($Create)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()
CodePudding user response:
@SikorskyS60 please see my new code
#### Form settings ################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "AD GROUP CREATER"
$Form.Size = New-Object System.Drawing.Size(750,470)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
function create {
if ($spsg.Checked) {$pathA = "OU=,OU=,OU=,DC=ad,DC=,DC=de"}
if ($fsg.Checked) {$pathB = "OU=,OU=,OU=,DC=ad,DC=,DC=de"}
if ($lokal.Checked) {$GroupScopeA = local}
if ($global.Checked) {$GroupScopeB = global}
if ($universal.Checked) {$GroupScopeA = universal}
if ($security.Checked) {$GroupCategoryB = security}
if ($distribution.Checked) {$GroupCategoryC = distribution}
New-ADGroup -Name $textbox.Text -groupCategory $GroupScopeA $GroupScopeB $GroupScopeC -groupScope $GroupScopeA $GroupScopeB $GroupScopeC -DisplayName $textbox.text -Path $pathA $pathB
}
#### Label settings #################################################################
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,10)
$Label.Size = New-Object System.Drawing.Size(280,20)
$Label.Text = "CREATE IN"
$Label.Font = "8,style=bold"
$Label.Forecolor = "black"
$Form.Controls.Add($Label)
#### Group box ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,30)
$groupBox.size = New-Object System.Drawing.Size(320,50)
$Form.Controls.Add($groupBox)
#### A button ###################################################################
$SPSG = New-Object System.Windows.Forms.RadioButton
$SPSG.Location = New-Object System.Drawing.Size(15,20)
$spsg.Size = New-Object System.Drawing.Size(100,20)
$spsg.Text = "SPSG"
$spsg.Cursor = [System.Windows.Forms.Cursors]::Hand
$spsg.Checked=$true
$spsg.Add_Click({})
$groupBox.Controls.Add($spsg)
#### B button ###################################################################
$FSG = New-Object System.Windows.Forms.RadioButton
$FSG.Location = New-Object System.Drawing.Size(150,20)
$Fsg.Size = New-Object System.Drawing.Size(150,20)
$Fsg.Text = "FSG"
$Fsg.Cursor = [System.Windows.Forms.Cursors]::Hand
$Fsg.Add_Click({})
$groupBox.Controls.Add($Fsg)
#### Label 2 settings #################################################################
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,100)
$Label2.Size = New-Object System.Drawing.Size(280,20)
$Label2.Text = "GROUP NAME"
$label2.Font = "8, style=bold"
$Label2.Forecolor = "black"
$Form.Controls.Add($Label2)
#### textbox settings #################################################################
$Textbox = New-Object System.Windows.Forms.TextBox
$Textbox.Location = New-Object System.Drawing.Size(10,120)
$Textbox.Size = New-Object System.Drawing.Size(320,20)
$Form.Controls.Add($Textbox)
#### Label 3 settings #################################################################
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Location = New-Object System.Drawing.Size(10,170)
$Label3.Size = New-Object System.Drawing.Size(310,20)
$Label3.Text = "GROUP SECTION"
$Label3.Forecolor = "black"
$label3.Font = "8, style=bold"
$Form.Controls.Add($Label3)
#### Group box 2 ########################################################
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$groupBox2.Location = New-Object System.Drawing.Size(10,190)
$groupBox2.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox2)
#### local button ###################################################################
$lokal = New-Object System.Windows.Forms.RadioButton
$lokal.Location = New-Object System.Drawing.Size(15,20)
$lokal.Size = New-Object System.Drawing.Size(150,20)
$lokal.Text = "Local"
$lokal.Cursor = [System.Windows.Forms.Cursors]::Hand
$lokal.Add_Click({})
$groupBox2.Controls.Add($lokal)
#### global button ###################################################################
$global = New-Object System.Windows.Forms.RadioButton
$global.Location = New-Object System.Drawing.Size(15,70)
$global.Size = New-Object System.Drawing.Size(150,20)
$global.Text = "Global"
$global.Cursor = [System.Windows.Forms.Cursors]::Hand
$global.Add_Click({})
$groupBox2.Controls.Add($global)
#### universal button ###################################################################
$universal = New-Object System.Windows.Forms.RadioButton
$universal.Location = New-Object System.Drawing.Size(15,120)
$universal.Size = New-Object System.Drawing.Size(150,20)
$universal.Text = "Universal"
$universal.Checked=$true
$universal.Cursor = [System.Windows.Forms.Cursors]::Hand
$universal.Add_Click({})
$groupBox2.Controls.Add($universal)
#### Label 4 settings #################################################################
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Location = New-Object System.Drawing.Size(400,170)
$Label4.Size = New-Object System.Drawing.Size(320,20)
$Label4.Text = "GROUP TYPE"
$Label4.Forecolor = "black"
$label4.Font = "8, style=bold"
$Form.Controls.Add($Label4)
#### Group box 3 ########################################################
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$groupBox3.Location = New-Object System.Drawing.Size(400,190)
$groupBox3.size = New-Object System.Drawing.Size(320,160)
$Form.Controls.Add($groupBox3)
#### security button ###################################################################
$security = New-Object System.Windows.Forms.RadioButton
$security.Location = New-Object System.Drawing.Size(15,20)
$security.Size = New-Object System.Drawing.Size(150,20)
$security.Text = "Security"
$security.Checked=$true
$security.Cursor = [System.Windows.Forms.Cursors]::Hand
$security.Add_Click({})
$groupBox3.Controls.Add($security)
#### Distribution button ###################################################################
$Distribution = New-Object System.Windows.Forms.RadioButton
$Distribution.Location = New-Object System.Drawing.Size(15,70)
$Distribution.Size = New-Object System.Drawing.Size(150,20)
$Distribution.Text = "Distribution"
$Distribution.Cursor = [System.Windows.Forms.Cursors]::Hand
$Distribution.Add_Click({})
$groupBox3.Controls.Add($Distribution)
#### Create button ###################################################################
$Create = New-Object System.Windows.Forms.Button
$Create.Location = New-Object System.Drawing.Size(10,370)
$Create.Text = "Create"
$Create.Cursor = [System.Windows.Forms.Cursors]::Hand
$Create.Width = 710
$Create.Height = 50
$Create.Font = "12,style=bold"
$Create.Add_Click({create})
$Form.Controls.Add($Create)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()
CodePudding user response:
Firstly, I would create variables for GroupScope
, GroupCategory
and Path
.
$GroupScope= ""
$GroupCategory= ""
$Path= ""
GroupScope
specifies in what scope the group will be created: DomainLocal(0), Global(1) or Universal(2). GroupCategory
specifies in what category the group is: Distribution(0) or Security(1). Path
specifies the path to the OU in your Domain.
Secondly, you'll need to save the user inputs to these variables. Group Name
as a String and the rest as an int
. You can use your event handler to get which button was pressed.
Finally, for the Create button you'll need to:
- Write all user inputs to the Variables
- Put all this information into a
New-ADGroup
cmdlet (I'd save the whole statement in a variable) - and execute the statement.
Example
$GroupScope= ""
$GroupCategory= ""
$Path= "" //will be overwritten by Button A or B, depending on which was selected
#### A button ###################################################################
$SPSG = New-Object System.Windows.Forms.RadioButton
$SPSG.Location = New-Object System.Drawing.Size(15,20)
$SPSG.Size = New-Object System.Drawing.Size(100,20)
$SPSG.Text = "A"
$SPSG.Cursor = [System.Windows.Forms.Cursors]::Hand
$SPSG.Add_Click({$script:Path = "PathToYourOU"}) //Change path to OU option A
$groupBox.Controls.Add($SPSG)
#### B button ###################################################################
$FSG = New-Object System.Windows.Forms.RadioButton
$FSG.Location = New-Object System.Drawing.Size(150,20)
$FSG.Size = New-Object System.Drawing.Size(150,20)
$FSG.Text = "B"
$FSG.Cursor = [System.Windows.Forms.Cursors]::Hand
$FSG.Add_Click({$script:Path = "PathToYourOU"}) //change path to OU option B
$groupBox.Controls.Add($FSG)