0

Hi have a script to create a input box using powershell. It looks like this

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
{$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please enter @from date:"
$objForm.Controls.Add($objLabel) 


$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(10,40) 
$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
$objForm.Controls.Add($objTextBox) 

 $objForm.Topmost = $True

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

$from

Now I want to pass the value $from I input into stored procedure parameter @from, I tried below but not working. Any suggestion?

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=localhost;Database=AMSDataWarehouse      Test;Integrated Security=SSPI"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand


 $SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$SqlCmd.CommandText = "YQBreport1"
$cmd.Parameters.Add("@from", $from)| Out-Null
$Command.ExecuteNonQuery()
$from = $Command.Parameters["@from"].value


$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$SQLResult =$DataSet.Tables[0]
$commands = $SQLResult | foreach-object -process { $_.output }> output.ps1
.\output.ps1
2

3 Answers 3

1

The line:

$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})

Only works within the Add_click functionality. To make it accessible elsewhere you need to alter it slightly to:

$OKButton.Add_Click({$Global:x=$objTextBox.Text;$objForm.Close()})

$x is then a global parameter that you can pass to your procedure.

Sign up to request clarification or add additional context in comments.

Comments

0

Yeah you can make a GUI in PowerShell using Windows Presentation Foundation (WPF). I've tried and failed.

Read in values like this:

$from = Read-Host '@From'
$from

Out-GridView is something new I just learned. You should look into it.

ls | Out-GridView -PassThru -Title 'GUI?'

2 Comments

You just made me realize Out-Gridview allows the selection of one or more entries, that are returned to the caller script... Thanks! :D Though I don't think that will help @Apriljuly, given that it only provides a list to select from, not an input box.
0

Nice example how to create an input box in PowerShell (using Windows Forms)

http://technet.microsoft.com/en-us/library/ff730941.aspx

3 Comments

That's a very good input box, thank you, but how do I associate what I input in it with the parameter in my stored procedure, do you know?
This should do the trick: $SqlCmd.Parameters.Add("@from", $x) | Out-Null. Assuming that you will use the example mentioned which assigns text entered in the dialog to $x variable.
What do you mean by not working? Does it throw any exception or error message?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.