I am building a dashboard (WinForm) for internal usage, written in PS, and have a section where users can query sites using various methods/tools for inspection.
All the other methods/tools (IWR, nslookup via CMD call, OpenSSL via CMD call, CURL via CMD call, etc.) have no issues displaying their query results within a Form TextBox... and results for CURL commands seem to execute, return and save results properly in a PS string variable fine when run just within PS (see demo), but when running from the Form, PS seems to want console input for some reason and never displays the stored string in the Form (I presume because it isn't populating the same when called from the Form).
To minimally reproduce the issue, here is the code...
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,880)
$form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "Query Dashboard"
$Font = New-Object System.Drawing.Font("Arial",9,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
#####RESULTSBOX and OUTPUT BUTTONS####
$Results_txtbox = New-Object System.Windows.Forms.RichTextBox
$Results_txtbox.Multiline = $true
$Results_txtbox.ScrollBars = "Vertical"
$Results_txtbox.Location = New-Object System.Drawing.Size(10,490)
$Results_txtbox.Size = New-Object System.Drawing.Size(485,270)
$Form.Controls.Add($Results_txtbox)
##########CURL#############
#divider
$CURLLinediv = New-Object System.Windows.Forms.Label
$CURLLinediv.Text = ""
$CURLLinediv.BorderStyle = "Fixed3D"
$CURLLinediv.AutoSize = $false
$CURLLinediv.height = 2
$CURLLinediv.width = 550
$CURLLinediv.Location = New-Object System.Drawing.Size(20,340)
$Form.Controls.Add($CURLLinediv)
$LabelCURL = New-Object System.Windows.Forms.Label
$LabelCURL.Text = "CURL"
$LabelCURL.AutoSize = $true
$LabelCURL.Location = New-Object System.Drawing.Size(30,342)
$Form.Controls.Add($LabelCURL)
$CURL_query_txtbox = New-Object System.Windows.Forms.TextBox
$CURL_query_txtbox.Location = New-Object System.Drawing.Size(20,360)
$CURL_query_txtbox.Size = New-Object System.Drawing.Size(300,20)
$CURL_query_txtbox.Text = "-s www.google.ca"
$CURL_query_txtbox.add_MouseHover($CURLquery_Help)
$Form.Controls.Add($CURL_query_txtbox)
$CURL_query_tip = New-Object System.Windows.Forms.ToolTip
$CURLquery_Help ={ $tip = "Enter entire CURL CMD here starting after curl"
$CURL_query_tip.SetToolTip($this,$tip)
}
$CURL_button = New-Object System.Windows.Forms.Button
$CURL_button.Location = New-Object System.Drawing.Size(325,355)
$CURL_button.Size = New-Object System.Drawing.Size(40,22)
$CURL_button.Text = "GET"
$CURL_button.Add_Click({CURL})
$Form.Controls.Add($CURL_button)
function CURL
{
#MOCK
#$CURL_query_txtbox.Text = "-s www.google.ca"
###$Results_txtbox.Text = (curl.exe $($CURL_query_txtbox.Text) 2>&1 | % ToString) -join "`n"
###$Results_txtbox.Text = (curl.exe -s www.google.ca 2>&1 | % ToString) -join "`n"
$Results_txtbox.Text = Invoke-Expression "curl.exe $($CURL_query_txtbox.Text) 2>&1"
}
$result = $Form.ShowDialog()
If you hit the GET button, the results should show in the Results textbox, but instead of returning the result to the form, PS wants an IWR parameter (if you enter www.google.ca or another Uri, it will return back to the application but does nothing with the input, and doesn't update the textfield... but you can now close the Form.
Now if in your PS window you run
$Results_txtbox.Text = (curl.exe -s www.google.ca 2>&1 | % ToString) -join "`n"
you can see that it populates $Results_txtbox.Text correctly when running from PS, and if you mock (ie - $CURL_query_txtbox.Text = "-s www.google.ca" ) first, you will see that
$Results_txtbox.Text = Invoke-Expression "curl.exe $($CURL_query_txtbox.Text) 2>&1"
works perfectly in PS alone, but when called from the Form it doesn't return any results, and the same cmdlet Invoke-WebRequest... message shows up in the PS console looking for a parameter to be entered.