locked
Enable Button Event in Powershell Windows Forms RRS feed

  • Question

  • Hi ,

    We are adding New AD user by using below powershell code which includes windows forms as well. Now please help me in enabling the Button click event to add the New AD User in the server.

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $form = New-Object “System.Windows.Forms.Form”;
     $form.Width = 500;
     $form.Height = 250;
     $form.Text = $title;
     $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

    ##############Define text label1
     $textLabel1 = New-Object “System.Windows.Forms.Label”;
     $textLabel1.Left = 25;
     $textLabel1.Top = 15;

    $textLabel1.Text = 'User Name';

    ##############Define text label2

    $textLabel2 = New-Object “System.Windows.Forms.Label”;
     $textLabel2.Left = 25;
     $textLabel2.Top = 50;

    $textLabel2.Text = 'Account Name';

    ##############Define text label3

    $textLabel3 = New-Object “System.Windows.Forms.Label”;
     $textLabel3.Left = 25;
     $textLabel3.Top = 85;

    $textLabel3.Text = 'Display Name';

    ############Define text box1 for input
     $textBox1 = New-Object “System.Windows.Forms.TextBox”;
     $textBox1.Left = 150;
     $textBox1.Top = 10;
     $textBox1.width = 200;

    ############Define text box2 for input

    $textBox2 = New-Object “System.Windows.Forms.TextBox”;
     $textBox2.Left = 150;
     $textBox2.Top = 50;
     $textBox2.width = 200;

    ############Define text box3 for input

    $textBox3 = New-Object “System.Windows.Forms.TextBox”;
     $textBox3.Left = 150;
     $textBox3.Top = 90;
     $textBox3.width = 200;

    #############Define default values for the input boxes
     $defaultValue = “”
    $textBox1.Text = $defaultValue;
     $textBox2.Text = $defaultValue;
     $textBox3.Text = $defaultValue;

    #############define button
     $button = New-Object “System.Windows.Forms.Button”;
     $button.Left = 360;
     $button.Top = 85;
     $button.Width = 100;
     $button.Text = “Enter”;

    ############# This is when you have to close the form after getting values
     $eventHandler = [System.EventHandler]{
     $textBox1.Text;
     $textBox2.Text;
     $textBox3.Text;
     $form.Close();};

    $button.Add_Click($eventHandler) ;

    #############Add controls to all the above objects defined
     $form.Controls.Add($button);
     $form.Controls.Add($textLabel1);
     $form.Controls.Add($textLabel2);
     $form.Controls.Add($textLabel3);
     $form.Controls.Add($textBox1);
     $form.Controls.Add($textBox2);
     $form.Controls.Add($textBox3);
     $ret = $form.ShowDialog();

     #New-ADUser -Name $textBox1 -SamAccountName $textBox2 -DisplayName $textBox3 -Path " + @"""OU=Users,DC=" + domainname + ",DC=SSS,DC=STUDIO,DC=COM" + "-AccountPassword (ConvertTo-SecureString $password -AsPlainText -force) -Enabled $true -ChangePasswordAtLogon $true -PasswordNeverExpires $False
     New-ADUser -Name $textBox1 -DisplayName $textBox1 -GivenName $textBox1 -Path "OU=Users,DC=" + domainname + ",DC=SSS,DC=STUDIO,DC=COM" -UserPrincipalName "$($samaccountname)@sss.studio.com" -SamAccountName $samaccountname -AccountPassword (ConvertTo-SecureString "temp" -AsPlainText -Force) -ChangePasswordAtLogon $true -Enabled $true
     #################return values
    $textBox1.Text, $textBox2.Text, $textBox3.Text


    Sunday, March 11, 2018 6:10 AM

Answers

  • Perhaps try this:
    cls
    
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $form = New-Object “System.Windows.Forms.Form”;
     $form.Width = 500;
     $form.Height = 250;
     $form.Text = $title;
     $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
    
    ##############Define text label1
     $textLabel1 = New-Object “System.Windows.Forms.Label”;
     $textLabel1.Left = 25;
     $textLabel1.Top = 15;
    
    $textLabel1.Text = 'Name:';
    
    ##############Define text label2
    
    $textLabel2 = New-Object “System.Windows.Forms.Label”;
     $textLabel2.Left = 25;
     $textLabel2.Top = 50;
    
    $textLabel2.Text = 'samAccountName:';
    
    ##############Define text label3
    
    $textLabel3 = New-Object “System.Windows.Forms.Label”;
     $textLabel3.Left = 25;
     $textLabel3.Top = 85;
    
    $textLabel3.Text = 'Display Name:';
    
    ############Define text box1 for input
     $textBox1 = New-Object “System.Windows.Forms.TextBox”;
     $textBox1.Left = 150;
     $textBox1.Top = 10;
     $textBox1.width = 200;
    
    ############Define text box2 for input
    
    $textBox2 = New-Object “System.Windows.Forms.TextBox”;
     $textBox2.Left = 150;
     $textBox2.Top = 50;
     $textBox2.width = 200;
    
    ############Define text box3 for input
    
    $textBox3 = New-Object “System.Windows.Forms.TextBox”;
     $textBox3.Left = 150;
     $textBox3.Top = 90;
     $textBox3.width = 200;
    
    #############Define default values for the input boxes
     $defaultValue = “”
    $textBox1.Text = $defaultValue;
     $textBox2.Text = $defaultValue;
     $textBox3.Text = $defaultValue;
    
    #############define button
     $button = New-Object “System.Windows.Forms.Button”;
     $button.Left = 360;
     $button.Top = 85;
     $button.Width = 100;
     $button.Text = “Enter”;
    
    ############# This is when you have to close the form after getting values
     $eventHandler = [System.EventHandler]{
     $textBox1.Text;
     $textBox2.Text;
     $textBox3.Text;
     $form.Close();};
    
    $button.Add_Click($eventHandler) ;
    
    #############Add controls to all the above objects defined
     $form.Controls.Add($button);
     $form.Controls.Add($textLabel1);
     $form.Controls.Add($textLabel2);
     $form.Controls.Add($textLabel3);
     $form.Controls.Add($textBox1);
     $form.Controls.Add($textBox2);
     $form.Controls.Add($textBox3);
     $ret = $form.ShowDialog();
    
     $Name = $textbox1.Text
     $sam = $textbox2.Text
     $DispName = $textbox3.text
     $OU = "OU=Users,DC=SomeCompany,DC=com"
    
     #New-ADUser -Name $textBox1 -SamAccountName $textBox2 -DisplayName $textBox3 -Path " + @"""OU=Users,DC=" + domainname + ",DC=SSS,DC=STUDIO,DC=COM" + "-AccountPassword (ConvertTo-SecureString $password -AsPlainText -force) -Enabled $true -ChangePasswordAtLogon $true -PasswordNeverExpires $False
     New-ADUser -SamAccountName $sam -DisplayName $DispName -Name $Name -Path $OU -UserPrincipalName "$sam@sss.studio.com" -AccountPassword (ConvertTo-SecureString "temp" -AsPlainText -Force) -ChangePasswordAtLogon $true -Enabled $true
     #################return values
    $textBox1.Text, $textBox2.Text, $textBox3.Text
    

    • Marked as answer by Pavan 565 Sunday, March 11, 2018 7:07 AM
    Sunday, March 11, 2018 6:40 AM

All replies

  • Please start by posting your code correctly using the code posting tool provided on the edit bar.  What you have posted in unreadable and cannot be copied without errors.

    Also note that all code that can be run in a form must be in a form event.


    \_(ツ)_/


    • Edited by jrv Sunday, March 11, 2018 6:23 AM
    Sunday, March 11, 2018 6:23 AM
  • Also note that you cannot return values from a form event.

    In your form this is how to get form values

    Here is a correct way to layout a form. It is more understandable. It would also be better if you named your control variables with more meaningful names.

    Add-Type -AssemblyName System.Windows.Forms
    
    $form = New-Object System.Windows.Forms.Form
    $form.Size = '500,250'
    $form.Text = $title
    $form.StartPosition = 'CenterScreen'
    
    ##############Define text label1
    $textLabel1 = New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel1)
    $textLabel1.Location = '25,15'
    $textLabel1.Text = 'User Name'
    
    ##############Define text label2
    $textLabel2 = New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel2)
    $textLabel2.Location = '25,50'
    $textLabel2.Text = 'Account Name'
    
    #Define text label3
    $textLabel3 = New-Object System.Windows.Forms.Label
    $form.Controls.Add($textLabel3)
    $textLabel3.Location = '25,65'
    $textLabel3.Text = 'Display Name'
    
    #Define text box1 for input
    $textBox1 = New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox1)
    $textBox1.Location = '150,10'
    $textBox1.width = 200
    
    #Define text box2 for input
    $textBox2 = New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox2)
    $textBox2.Location = '150,50'
    $textBox2.width = 200
    
    #Define text box3 for input
    $textBox3 = New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textBox3)
    $textBox3.Location = '150,90'
    $textBox3.width = 200
    
    #############define button
    $button = New-Object System.Windows.Forms.Button
    $form.Controls.Add($button)
    $button.Location = '360,85'
    $button.Width = 100
    $button.Text = 'Enter'
    $button.Add_Click({
        $form.Close()
    }) 
    
    $ret = $form.ShowDialog()
    
    # get form values
    $textBox1.Text
    $textBox2.Text
    $textBox3.Text
    Look at the code carefully and think about what it is doing and why.


    \_(ツ)_/

    Sunday, March 11, 2018 6:39 AM
  • Perhaps try this:
    cls
    
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    $form = New-Object “System.Windows.Forms.Form”;
     $form.Width = 500;
     $form.Height = 250;
     $form.Text = $title;
     $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
    
    ##############Define text label1
     $textLabel1 = New-Object “System.Windows.Forms.Label”;
     $textLabel1.Left = 25;
     $textLabel1.Top = 15;
    
    $textLabel1.Text = 'Name:';
    
    ##############Define text label2
    
    $textLabel2 = New-Object “System.Windows.Forms.Label”;
     $textLabel2.Left = 25;
     $textLabel2.Top = 50;
    
    $textLabel2.Text = 'samAccountName:';
    
    ##############Define text label3
    
    $textLabel3 = New-Object “System.Windows.Forms.Label”;
     $textLabel3.Left = 25;
     $textLabel3.Top = 85;
    
    $textLabel3.Text = 'Display Name:';
    
    ############Define text box1 for input
     $textBox1 = New-Object “System.Windows.Forms.TextBox”;
     $textBox1.Left = 150;
     $textBox1.Top = 10;
     $textBox1.width = 200;
    
    ############Define text box2 for input
    
    $textBox2 = New-Object “System.Windows.Forms.TextBox”;
     $textBox2.Left = 150;
     $textBox2.Top = 50;
     $textBox2.width = 200;
    
    ############Define text box3 for input
    
    $textBox3 = New-Object “System.Windows.Forms.TextBox”;
     $textBox3.Left = 150;
     $textBox3.Top = 90;
     $textBox3.width = 200;
    
    #############Define default values for the input boxes
     $defaultValue = “”
    $textBox1.Text = $defaultValue;
     $textBox2.Text = $defaultValue;
     $textBox3.Text = $defaultValue;
    
    #############define button
     $button = New-Object “System.Windows.Forms.Button”;
     $button.Left = 360;
     $button.Top = 85;
     $button.Width = 100;
     $button.Text = “Enter”;
    
    ############# This is when you have to close the form after getting values
     $eventHandler = [System.EventHandler]{
     $textBox1.Text;
     $textBox2.Text;
     $textBox3.Text;
     $form.Close();};
    
    $button.Add_Click($eventHandler) ;
    
    #############Add controls to all the above objects defined
     $form.Controls.Add($button);
     $form.Controls.Add($textLabel1);
     $form.Controls.Add($textLabel2);
     $form.Controls.Add($textLabel3);
     $form.Controls.Add($textBox1);
     $form.Controls.Add($textBox2);
     $form.Controls.Add($textBox3);
     $ret = $form.ShowDialog();
    
     $Name = $textbox1.Text
     $sam = $textbox2.Text
     $DispName = $textbox3.text
     $OU = "OU=Users,DC=SomeCompany,DC=com"
    
     #New-ADUser -Name $textBox1 -SamAccountName $textBox2 -DisplayName $textBox3 -Path " + @"""OU=Users,DC=" + domainname + ",DC=SSS,DC=STUDIO,DC=COM" + "-AccountPassword (ConvertTo-SecureString $password -AsPlainText -force) -Enabled $true -ChangePasswordAtLogon $true -PasswordNeverExpires $False
     New-ADUser -SamAccountName $sam -DisplayName $DispName -Name $Name -Path $OU -UserPrincipalName "$sam@sss.studio.com" -AccountPassword (ConvertTo-SecureString "temp" -AsPlainText -Force) -ChangePasswordAtLogon $true -Enabled $true
     #################return values
    $textBox1.Text, $textBox2.Text, $textBox3.Text
    

    • Marked as answer by Pavan 565 Sunday, March 11, 2018 7:07 AM
    Sunday, March 11, 2018 6:40 AM