none
PowerShell - Run Command with Windows Form RRS feed

  • Question

  • Hello,

    What is wrong with my code?

    In the first textbox, the command work

    but in the second textbox, the command not work.

    but if I put the group name instead of a variable, the command works

    Below is the complete code:

    function Show-mainform2_psf {

        Add-Type -AssemblyName System.Windows.Forms
        import-module activedirectory


        #region Generated Form Objects
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $form1 = New-Object 'System.Windows.Forms.Form'
        $textboxResultado = New-Object 'System.Windows.Forms.TextBox'
        $textboxCheckPath = New-Object 'System.Windows.Forms.TextBox'
        $textboxCheckGroup = New-Object 'System.Windows.Forms.TextBox'
        $buttonCheckOwnerGroup = New-Object 'System.Windows.Forms.Button'
        $buttonClickMe = New-Object 'System.Windows.Forms.Button'
        $buttonOK = New-Object 'System.Windows.Forms.Button'
        $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
        #endregion Generated Form Objects

        
        $form1_Load={
            
        }
        
        
        $buttonClickMe_Click={
            $textboxResultado.Lines = (Get-Acl $textboxCheckPath.Lines).Access | ft IdentityReference,FileSystemRights -auto | Out-String
        }



        $buttonCheckOwnerGroup_Click={
            $textboxResultado.Lines = Get-ADGroup -Identity $textboxCheckGroup.Lines -Properties Name,Description,info | select Name,Description,Info | Out-String
        }
        
        #region Generated Events
        $Form_StateCorrection_Load=
        {
            #Correct the initial state of the form to prevent the .Net maximized form issue
            $form1.WindowState = $InitialFormWindowState
        }
        
        $Form_Cleanup_FormClosed=
        {
            #Remove all event handlers from the controls
            try
            {
                $buttonClickMe.remove_Click($buttonClickMe_Click)
                $form1.remove_Load($form1_Load)
                $form1.remove_Load($Form_StateCorrection_Load)
                $form1.remove_FormClosed($Form_Cleanup_FormClosed)
            }
            catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
        }
        #endregion Generated Events

        #region Generated Form Code
        # form1
        #
        $form1.Controls.Add($textboxResultado)
        $form1.Controls.Add($textboxCheckPath)
        $form1.Controls.Add($textboxCheckGroup)
        $form1.Controls.Add($buttonClickMe)
        $form1.Controls.Add($buttonCheckOwnerGroup)
        $form1.Controls.Add($buttonOK)
        $form1.AcceptButton = $buttonOK
        $form1.AutoScaleDimensions = '6, 13'
        $form1.AutoScaleMode = 'Font'
        $form1.ClientSize = '711, 483'
        $form1.FormBorderStyle = 'FixedDialog'
        $form1.MaximizeBox = $False
        $form1.MinimizeBox = $False
        $form1.Name = 'Check Folder'
        $form1.StartPosition = 'CenterScreen'
        $form1.Text = 'Check Folder'
        $form1.add_Load($form1_Load)
        #
        # textboxCheckGroup
        #
          $textboxCheckGroup.Location = '23, 45'
        $textboxCheckGroup.Multiline = $True
        $textboxCheckGroup.Name = 'textboxCheckGroup'
        $textboxCheckGroup.Size = '300, 20'
        $textboxCheckGroup.TabIndex = 4
        #
        # textboxCheckPath
        #
          $textboxCheckPath.Location = '23, 15'
        $textboxCheckPath.Multiline = $True
        $textboxCheckPath.Name = 'textboxCheckPath'
        $textboxCheckPath.Size = '300, 20'
        $textboxCheckPath.TabIndex = 3
        #
        # textboxResultado
        #
        $textboxResultado.Location = '23, 150'
        $textboxResultado.Multiline = $True
        $textboxResultado.Name = 'textboxResultado'
        $textboxResultado.Size = '476, 178'
        $textboxResultado.TabIndex = 2
        #
        # buttonCheckOwnerGroup
        #
        $buttonCheckOwnerGroup.Location = '330, 44'
        $buttonCheckOwnerGroup.Name = 'CheckOwnerGroup'
        $buttonCheckOwnerGroup.Size = '120, 23'
        $buttonCheckOwnerGroup.TabIndex = 5
        $buttonCheckOwnerGroup.Text = 'Check Owner Group'
        $buttonCheckOwnerGroup.UseCompatibleTextRendering = $True
        $buttonCheckOwnerGroup.UseVisualStyleBackColor = $True
        $buttonCheckOwnerGroup.add_Click($buttonCheckOwnerGroup_Click)
        #
        # buttonClickMe
        #
        $buttonClickMe.Location = '330, 13'
        $buttonClickMe.Name = 'buttonClickMe'
        $buttonClickMe.Size = '75, 23'
        $buttonClickMe.TabIndex = 1
        $buttonClickMe.Text = 'Check Path'
        $buttonClickMe.UseCompatibleTextRendering = $True
        $buttonClickMe.UseVisualStyleBackColor = $True
        $buttonClickMe.add_Click($buttonClickMe_Click)
        #
        # buttonOK
        #
        $buttonOK.Anchor = 'Bottom, Right'
        $buttonOK.DialogResult = 'OK'
        $buttonOK.Location = '624, 448'
        $buttonOK.Name = 'buttonOK'
        $buttonOK.Size = '75, 23'
        $buttonOK.TabIndex = 0
        $buttonOK.Text = '&OK'
        $buttonOK.UseCompatibleTextRendering = $True
        $buttonOK.UseVisualStyleBackColor = $True
        $form1.ResumeLayout()
        #endregion Generated Form Code

        #----------------------------------------------

        #Save the initial state of the form
        $InitialFormWindowState = $form1.WindowState
        #Init the OnLoad event to correct the initial state of the form
        $form1.add_Load($Form_StateCorrection_Load)
        #Clean up the control events
        $form1.add_FormClosed($Form_Cleanup_FormClosed)
        #Show the Form
        return $form1.ShowDialog()

    }
    Show-mainform2_psf

    Tuesday, October 8, 2019 9:16 PM

All replies

  • Hello,

    I am creating an interface in the power shell with forms, and I need to click on the button it runs the command I set and return the result in a text field that I put in forms too.

    Example:

    Path field:

    Command: Get-acl <value dictated in path field>

    Button

    Result Field: Returns Get-acl Path Command Value

    how do i make it work?
    Tuesday, October 8, 2019 12:08 AM
  • function Show-mainform2_psf {
    
    	Add-Type -AssemblyName System.Windows.Forms
    
    	#region Generated Form Objects
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$textbox1 = New-Object 'System.Windows.Forms.TextBox'
    	$buttonClickMe = New-Object 'System.Windows.Forms.Button'
    	$buttonOK = New-Object 'System.Windows.Forms.Button'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	
    	$form1_Load={
    		
    	}
    	
    	$buttonClickMe_Click={
    		$textbox1.Lines = Get-Process | Out-String
    	}
    	
    	#region Generated Events
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$buttonClickMe.remove_Click($buttonClickMe_Click)
    			$form1.remove_Load($form1_Load)
    			$form1.remove_Load($Form_StateCorrection_Load)
    			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
    	}
    	#endregion Generated Events
    
    	#region Generated Form Code
    	# form1
    	#
    	$form1.Controls.Add($textbox1)
    	$form1.Controls.Add($buttonClickMe)
    	$form1.Controls.Add($buttonOK)
    	$form1.AcceptButton = $buttonOK
    	$form1.AutoScaleDimensions = '6, 13'
    	$form1.AutoScaleMode = 'Font'
    	$form1.ClientSize = '711, 483'
    	$form1.FormBorderStyle = 'FixedDialog'
    	$form1.MaximizeBox = $False
    	$form1.MinimizeBox = $False
    	$form1.Name = 'form1'
    	$form1.StartPosition = 'CenterScreen'
    	$form1.Text = 'Form'
    	$form1.add_Load($form1_Load)
    	#
    	# textbox1
    	#
    	$textbox1.Location = '23, 64'
    	$textbox1.Multiline = $True
    	$textbox1.Name = 'textbox1'
    	$textbox1.Size = '676, 378'
    	$textbox1.TabIndex = 2
    	#
    	# buttonClickMe
    	#
    	$buttonClickMe.Location = '104, 26'
    	$buttonClickMe.Name = 'buttonClickMe'
    	$buttonClickMe.Size = '75, 23'
    	$buttonClickMe.TabIndex = 1
    	$buttonClickMe.Text = 'Click Me'
    	$buttonClickMe.UseCompatibleTextRendering = $True
    	$buttonClickMe.UseVisualStyleBackColor = $True
    	$buttonClickMe.add_Click($buttonClickMe_Click)
    	#
    	# buttonOK
    	#
    	$buttonOK.Anchor = 'Bottom, Right'
    	$buttonOK.DialogResult = 'OK'
    	$buttonOK.Location = '624, 448'
    	$buttonOK.Name = 'buttonOK'
    	$buttonOK.Size = '75, 23'
    	$buttonOK.TabIndex = 0
    	$buttonOK.Text = '&OK'
    	$buttonOK.UseCompatibleTextRendering = $True
    	$buttonOK.UseVisualStyleBackColor = $True
    	$form1.ResumeLayout()
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $form1.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$form1.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $form1.ShowDialog()
    
    }
    Show-mainform2_psf
    


    \_(ツ)_/

    Tuesday, October 8, 2019 12:25 AM
  • but how do I add a textbox to when I put information in the textbox it will run along with the command?


    Example: TextBox: \\share\subfolder

    $path = textBox

    $textbox1.Lines = Get-Acl $path | Out-String

    #Run: Get-Acl \\share\subfolder


    Thanks


    Tuesday, October 8, 2019 1:17 AM
  • It is all there in the example. It is up to you to learn enough PowerShell and programming to understand it. We cannot give you personal instruction.


    \_(ツ)_/

    Tuesday, October 8, 2019 1:23 AM
  • ok.

    thank you very much

    Tuesday, October 8, 2019 1:56 AM




  • Tuesday, October 8, 2019 7:25 PM
  • Here is a much simpler example of how to use a textbox to display output.

    Add-Type -AssemblyName System.Windows.Forms
    
    # control creation
    $form = New-Object System.Windows.Forms.Form
    $buttonGetAcl = New-Object System.Windows.Forms.Button
    $form.Controls.Add($buttonGetAcl)
    
    $textbox = New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textbox)
    
    # form settings
    $form.Size = '400,300'
    $form.StartPosition = 'CenterScreen'
    $form.Text = 'Basic Form Demo'
    
    # buttonGetAcl settings
    $buttonGetAcl.Text = 'Get Acl'
    $buttonGetAcl.add_Click({
    	$textbox.Lines = (Get-Acl).Access | Out-String
    })
    
    # textbox settings
    $textbox.Multiline = $true
    $textbox.Size = '365,200'
    $textbox.Anchor = 'Top,Bottom,Left,Right'
    $textbox.Location = '5,30'
    $textbox.ScrollBars = 'Both'
    
    $form.ShowDialog()


    \_(ツ)_/

    Tuesday, October 8, 2019 8:47 PM
  • This is why it seems pointless to try and help users who just want someone to write a free script. Too bad we don't have more forum controls on how questions are posted. Some form of announcement when a user j0ins or asks a question might keep users from getting frustrated. I also think a forum for non-technical users questions about scripting would be useful.


    \_(ツ)_/

    Tuesday, October 8, 2019 9:43 PM
  • That happens to be my code and this forum cannot solve your issue any more than the previous forum.

    You need to learn how to use PowerShell to a fairly high level of proficiency in order to use PS in a form. 

    Results from PS commands are in the form of objects which have to be converted to text.  You also need to select the properties you are interested in.  This is fundamental PowerShell at a command line.  Once you have learned how this works then apply it to the output in the form.

    You will need some time to learn how forms work.  There is no easy way to do this.  Look for books and tutorials on forms and how they work.

    Try8ing to delete threads that don't satisfy you in this and other technical forums is not helpful to anyone.

    To learn how to program with PowerShell takes patience and learning.

    Try asking a single basic question without lots of pictures.  What line of code a problem and what is the problem.


    \_(ツ)_/

    Tuesday, October 8, 2019 9:51 PM
  • Is this forum not for doubt? because I have a doubt
    Tuesday, October 8, 2019 10:07 PM
  • Is this forum not for doubt? because I have a doubt

    What is a doubt? It is not a computer term. Do you mean you have a problem, issue? Please try to be clear.


    \_(ツ)_/

    Tuesday, October 8, 2019 10:26 PM
  • This command is not working.
    I don't want you to write the code for me, I just want some help solving the problem with this command, saying what's wrong.

    $buttonCheckOwnerGroup_Click={
            $textboxResultado.Lines = Get-ADGroup -Identity $textboxCheckGroup.Lines -Properties Name,Description,info | select Name,Description,Info | Out-String

    Wednesday, October 9, 2019 12:23 AM
  • How is it not working? Do you get an error? j Note that you cannot use "lines" to get a single value from a textbox you must use "Text" for a single value.  "Lines" is an array of text lines.  AD cannot us an array for a string value.


    \_(ツ)_/

    Wednesday, October 9, 2019 12:34 AM
  • Worked out with the command below:

    $buttonCheckOwnerGroup_Click={
            $textboxResultado.Lines = Get-ADGroup -Identity $textboxCheckGroup.text -Properties Name,Description,info | select Name,Description,Info | Out-String

    just put the .text in the $textboxCheckGroup variable

    Thank you all

    Saturday, October 12, 2019 2:06 PM