Home > other >  define create button in powershell gui
define create button in powershell gui

Time:08-05

i have written a gui in powershell for creating groups in active directory depending on my selections i have made in the gui.

#### 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

#### 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 ###################################################################
$A= New-Object System.Windows.Forms.RadioButton
$A.Location = New-Object System.Drawing.Size(15,20)
$A.Size     = New-Object System.Drawing.Size(100,20)
$A.Text     = "A"
$A.Cursor   = [System.Windows.Forms.Cursors]::Hand
$A.Add_Click({})
$groupBox.Controls.Add($A)


#### B button ###################################################################
$B= New-Object System.Windows.Forms.RadioButton
$B.Location = New-Object System.Drawing.Size(150,20)
$B.Size     = New-Object System.Drawing.Size(150,20)
$B.Text     = "B"
$B.Cursor   = [System.Windows.Forms.Cursors]::Hand
$B.Add_Click({})
$groupBox.Controls.Add($B)


#### 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()

The gui is finished but i have problems completing my code. what do i need to add to my code that the create button creates the groups? furthermore i think that an assignment to the selection is missing

CodePudding user response:

Instead of the now undefined function pinginfo, you will need to check what radiobutton is checked in "Create IN" and from that define the -Path property for New-ADGroup.
Then test if a name is filled in, test if this name does not already exist and use that for -Name.
The checked radiobutton in "Group Section" decides the parameter -GroupCategory and the one in "Group Type" is for -GroupScope.

When you have collected those values and tested if they were filled in, create the group.

Something like this perhaps

$Create.Add_Click({
    # create an empty parameters object for New-ADGroup
    $params = '' | Select-Object Path, Name, GroupCategory, GroupScope
    if ($A.Checked) { $params.Path = 'TheOU for option A' }
    elseif( $B.Checked) { $params.Path = 'TheOU for option B' }
    else {
        # display an error that there MUST be a choice here
        # I'll leave this as exercise for you
        return
    }

    $params.Name = $Textbox.Text.Trim()
    # test if there is a name given and if so, test if that name does not already exist in AD
    if ([string]::IsNullOrWhiteSpace($params.Name) -or (Get-ADGroup -Filter "Name -eq $($params.Name)")) {
        # display an error about the name field
        # I'll leave this as exercise for you
        return
    }

    if ($lokal.Checked) {$params.GroupScope = $lokal.Text}
    elseif ($global.Checked) {$params.GroupScope = $global.Text}
    elseif ($universal.Checked) {$params.GroupScope = $universal.Text}
    else {
        # display an error that there MUST be a choice here
        # I'll leave this as exercise for you
        return
    }

    if ($security.Checked) {$params.GroupCategory = $security.Text}
    elseif ($Distribution.Checked) {$params.GroupCategory = $Distribution.Text}
    else {
        # display an error that there MUST be a choice here
        # I'll leave this as exercise for you
        return
    }

    # next create the group
    New-ADGroup @params
})

CodePudding user response:

@Theo please have a look. I still need to input a GroupScope. After thet, the script says it is a wrong name but my name looks like test-group

#### 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

#### 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 ###################################################################
$A= New-Object System.Windows.Forms.RadioButton
$A.Location = New-Object System.Drawing.Size(15,20)
$A.Size     = New-Object System.Drawing.Size(100,20)
$A.Text     = "A"
$A.Cursor   = [System.Windows.Forms.Cursors]::Hand
$A.Add_Click({})
$groupBox.Controls.Add($A)


#### B button ###################################################################
$B= New-Object System.Windows.Forms.RadioButton
$B.Location = New-Object System.Drawing.Size(150,20)
$B.Size     = New-Object System.Drawing.Size(150,20)
$B.Text     = "B"
$B.Cursor   = [System.Windows.Forms.Cursors]::Hand
$B.Add_Click({})
$groupBox.Controls.Add($B)


#### 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 ###################################################################
$DomainLocal          = New-Object System.Windows.Forms.RadioButton
$DomainLocal.Location = New-Object System.Drawing.Size(15,20)
$DomainLocal.Size     = New-Object System.Drawing.Size(150,20)
$DomainLocal.Text     = "Local"
$DomainLocal.Cursor   = [System.Windows.Forms.Cursors]::Hand
$DomainLocal.Add_Click({})
$groupBox2.Controls.Add($DomainLocal)


#### 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({
    # create an empty parameters object for New-ADGroup
    $params = '' | Select-Object Name, GroupScope, GroupCategory, Path

     $params.Name = $Textbox.Text.Trim()
    # test if there is a name given and if so, test if that name does not already exist in AD
    if ([string]::IsNullOrWhiteSpace($params.Name) -or (Get-ADGroup -Filter "Name -eq $($params.Name)")) {
        # display an error about the name field
        # I'll leave this as exercise for you
        return
    }

    if ($DomainLocal.Checked) {$params.GroupScope = $DomainLocal.Text}
    elseif ($global.Checked) {$params.GroupScope = $global.Text}
    elseif ($universal.Checked) {$params.GroupScope = $universal.Text}
    else {
        # display an error that there MUST be a choice here
        # I'll leave this as exercise for you
        return
    }

    if ($security.Checked) {$params.GroupCategory = $security.Text}
    elseif ($Distribution.Checked) {$params.GroupCategory = $Distribution.Text}
    else {
        # display an error that there MUST be a choice here
        # I'll leave this as exercise for you
        return
    }

    if ($A.Checked) { $params.Path = 'TheOU for option A' }
    elseif( $B.Checked) { $params.Path = 'TheOU for option B' }
    else {
        # display an error that there MUST be a choice here
        # I'll leave this as exercise for you
        return
    }    

    

    # next create the group
    New-ADGroup @params
})
$Form.Controls.Add($Create)


$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

# destroy the form from memory
$Form.Dispose()
  • Related