locked
Powershell : Issues with user input collection from Multiple InPutBox Form RRS feed

  • Question

  • I am having issues with getting user input to pass from a form that a user fills out into variables that I can then use in other methods and commands. (ex; SQL Query, SQL Data Add, ... )

    I have attached the Powershell script I am using in it's designed form but I am having issues getting the DataCollection function to grab the content of the InputBox and send it to a variable for later use.

    Note: I'm running this at this time from the ISE so I can actually see what is going on.

    Any help would be appreciated.

    DAS

    [System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
    [System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
    
    $FormDBA = New-Object System.Windows.Forms.Form
    $FormDBA.Size = New-Object System.Drawing.Size(300,500)
    $FormDBA.Text = "MIS Data"
    $FormDBA.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
    
    
    function DataCapture
        {
        $SubSID = $InBoxSID.text
        ECHO $SubSID
        }
    
    $InBoxTxtSID = New-Object System.Windows.Forms.Label
    $InBoxTxtSID.Location = New-Object System.Drawing.Size(25,15)
    $InBoxTxtSID.Text = "Sticker ID : "
    $InBoxSID = New-Object System.Windows.Forms.TextBox 
    $InBoxSID.Location = New-Object System.Drawing.Size(130,10)
    $InBoxSID.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtUSR = New-Object System.Windows.Forms.Label
    $InBoxTxtUSR.Location = New-Object System.Drawing.Size(25,55)
    $InBoxTxtUSR.Text = "User Name ; "
    $InBoxUSR = New-Object System.Windows.Forms.TextBox 
    $InBoxUSR.Location = New-Object System.Drawing.Size(130,50)
    $InBoxUSR.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtPCN = New-Object System.Windows.Forms.Label
    $InBoxTxtPCN.Location = New-Object System.Drawing.Size(25,95)
    $InBoxTxtPCN.Text = "PC Name : "
    $InBoxPCN = New-Object System.Windows.Forms.TextBox 
    $InBoxPCN.Location = New-Object System.Drawing.Size(130,90)
    $InBoxPCN.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtDPT = New-Object System.Windows.Forms.Label
    $InBoxTxtDPT.Location = New-Object System.Drawing.Size(25,135)
    $InBoxTxtDPT.Text = "Department : "
    $InBoxDPT = New-Object System.Windows.Forms.TextBox 
    $InBoxDPT.Location = New-Object System.Drawing.Size(130,130)
    $InBoxDPT.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtPCT = New-Object System.Windows.Forms.Label
    $InBoxTxtPCT.Location = New-Object System.Drawing.Size(25,175)
    $InBoxTxtPCT.Text = "PC Type : "
    $InBoxPCT = New-Object System.Windows.Forms.TextBox 
    $InBoxPCT.Location = New-Object System.Drawing.Size(130,170)
    $InBoxPCT.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtMAK = New-Object System.Windows.Forms.Label
    $InBoxTxtMAK.Location = New-Object System.Drawing.Size(25,215)
    $InBoxTxtMAK.Text = "Make : "
    $InBoxMAK = New-Object System.Windows.Forms.TextBox 
    $InBoxMAK.Location = New-Object System.Drawing.Size(130,210)
    $InBoxMAK.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtMOD = New-Object System.Windows.Forms.Label
    $InBoxTxtMOD.Location = New-Object System.Drawing.Size(25,255)
    $InBoxTxtMOD.Text = "Model : "
    $InBoxMOD = New-Object System.Windows.Forms.TextBox 
    $InBoxMOD.Location = New-Object System.Drawing.Size(130,250)
    $InBoxMOD.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtOPS = New-Object System.Windows.Forms.Label
    $InBoxTxtOPS.Location = New-Object System.Drawing.Size(25,295)
    $InBoxTxtOPS.Text = "O.S. : "
    $InBoxOPS = New-Object System.Windows.Forms.TextBox 
    $InBoxOPS.Location = New-Object System.Drawing.Size(130,290)
    $InBoxOPS.Size = New-Object System.Drawing.Size(120,20)
    
    $InBoxTxtDIS = New-Object System.Windows.Forms.Label
    $InBoxTxtDIS.Location = New-Object System.Drawing.Size(25,335)
    $InBoxTxtDIS.Text = "Disposed : "
    $InBoxDIS = New-Object System.Windows.Forms.TextBox 
    $InBoxDIS.Location = New-Object System.Drawing.Size(130,330)
    $InBoxDIS.Size = New-Object System.Drawing.Size(120,20)
    
    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Size(150,400)
    $button.Width = 100
    $button.Text = “Ok”
    $button.Add_Click({DataCapture})
    
    $FormDBA.Controls.Add($button)
    $FormDBA.Controls.Add($InBoxTxtSID)
    $FormDBA.Controls.Add($InBoxTxtUSR)
    $FormDBA.Controls.Add($InBoxTxtPCN)
    $FormDBA.Controls.Add($InBoxTxtDPT)
    $FormDBA.Controls.Add($InBoxTxtPCT)
    $FormDBA.Controls.Add($InBoxTxtMAK)
    $FormDBA.Controls.Add($InBoxTxtMOD)
    $FormDBA.Controls.Add($InBoxTxtOPS)
    $FormDBA.Controls.Add($InBoxTxtDIS)
    $FormDBA.Controls.Add($InBoxSID)
    $FormDBA.Controls.Add($InBoxUSR)
    $FormDBA.Controls.Add($InBoxPCN)
    $FormDBA.Controls.Add($InBoxDPT)
    $FormDBA.Controls.Add($InBoxPCT)
    $FormDBA.Controls.Add($InBoxMAK)
    $FormDBA.Controls.Add($InBoxMOD)
    $FormDBA.Controls.Add($InBoxOPS)
    $FormDBA.Controls.Add($InBoxDIS)
    $FormDBA.ShowDialog()

    Wednesday, October 1, 2014 12:27 PM

Answers

  • Change this:

    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Size(150,400)
    $button.Width = 100
    $button.Text = “Ok”
    $button.DialogResult='Ok'  #<<<<<-------
    #$button.Add_Click({DataCapture})

    Remove function andrun like this:

    if('Ok' -eq $FormDBA.ShowDialog()){
        $FormDBA.Controls|%{$_.Text}
    }

    With names you can get values by control name.


    ¯\_(ツ)_/¯

    Wednesday, October 1, 2014 1:43 PM

All replies

  • Give each textbox a name then you can retrieve the contents after the form closes like this:

    $txtSID=$FormDBA.Controls['txtSID'].Text


    ¯\_(ツ)_/¯

    Wednesday, October 1, 2014 1:18 PM
  • Change this:

    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Size(150,400)
    $button.Width = 100
    $button.Text = “Ok”
    $button.DialogResult='Ok'  #<<<<<-------
    #$button.Add_Click({DataCapture})

    Remove function andrun like this:

    if('Ok' -eq $FormDBA.ShowDialog()){
        $FormDBA.Controls|%{$_.Text}
    }

    With names you can get values by control name.


    ¯\_(ツ)_/¯

    Wednesday, October 1, 2014 1:43 PM
  • Also if you are running this as a script file then you need to do this.

    if('Ok' -eq $FormDBA.ShowDialog()){
        $FormDBA # return the form object
    }else{
         Write-Host 'Form Cancelled!' -fore red
    }

    Call the script like this:

    $form=.\MyscriptForm.ps1

    $form.Controls | %{$_.Text

    }


    ¯\_(ツ)_/¯

    Wednesday, October 1, 2014 1:47 PM
  • Thanks for the quick responses.

    The purpose of the function is that when the 'Accept' or (in my case/purpose)'Write Record' button is clicked, it collects all of the information from all of the input boxes and placed them into individual variables that I can use in SQL commands.

    If it can stay as a function, that would be preferred.

    Also, I forgot to mention that this will be going into another 'Larger' script file that will be called via a button click.

    If you would like to look at that I can post it here as well.

    I'll give these first two a try, but I'm not yet sure of the last one.

    Wednesday, October 1, 2014 2:21 PM
  • Thanks for the quick responses.

    The purpose of the function is that when the 'Accept' or (in my case/purpose)'Write Record' button is clicked, it collects all of the information from all of the input boxes and placed them into individual variables that I can use in SQL commands.

    If it can stay as a function, that would be preferred.

    Also, I forgot to mention that this will be going into another 'Larger' script file that will be called via a button click.

    If you would like to look at that I can post it here as well.

    I'll give these first two a try, but I'm not yet sure of the last one.


    I don't think you understand.  There is no need to collect the data.  It is in the form.  Just use it when the form closes.  You can extract it to variables or just leave it in the form.  The key is to give the objects names which you can use to extract the text of the text boxes.  That is hoe we use forms.  You are trying to re-invent something that is built into the forms system.

    ¯\_(ツ)_/¯

    Wednesday, October 1, 2014 2:28 PM
  • Change this:

    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Size(150,400)
    $button.Width = 100
    $button.Text = “Ok”
    $button.DialogResult='Ok'  #<<<<<-------
    #$button.Add_Click({DataCapture})

    Remove function andrun like this:

    if('Ok' -eq $FormDBA.ShowDialog()){
        $FormDBA.Controls|%{$_.Text}
    }

    With names you can get values by control name.


    ¯\_(ツ)_/¯

    This suggestion works for the purpose I needed. 

    If I could, I would attach the file instead of pasting the script so you can see what all I am using this to do.

    But in short, we have a main form we use to pull records from a database and call up an application at the click of a button for remote assistance.  However, we seem to now need the ability to have this application to edit and add new records into said database.  That's where this second form came in and also when I hit my issue with the information capture.

    At this time I'm getting this to format the information collected so that I can start using using it with SQL commands.

    Wednesday, October 1, 2014 3:45 PM
  • If it is iterative then you should place the database update code inside of the form.

    This tool here can build data aware forms very easily and with much less effort than doing it manually.  It is highly automated and specific to Windows Administration and maintenance.


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, October 1, 2014 4:13 PM
    Wednesday, October 1, 2014 4:13 PM
  • I'll look into that.  It's a shame that Notepad++ doesn't have a debug environment or a native Powershell snapin.  But, I deal with the ISE because we are limited on our funds here due to higher management, and it would help if the ISE would stop hanging up where I have to 'End Process Tree' it from Task Manager every time I turnaround. Took us almost two years to get management to realize we could do more with less if we get 2012 Datacenter and reduce costs in the server budget, but that's aside the point.

    Thanks for the help on this issue.

    I hope you have a great day.

    Wednesday, October 1, 2014 4:49 PM