I have been trying to create a Powershell form that uses the input to do other tasks. But the data needs to be validated (can't be blank, etc...) But when I run it and have to correct some of the inputs I get Multiple values from the return with the blank and my final. Also the variable doesn't leave the function. I have tried a number of things like Clear-Variable and Out-Null and I know it has to do with the way Powershell handles return data but I'm stuck. I have a simplified version here:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Microsoft.VisualBasic
$BuildName = ""
function Enter-BuildInfo {
# Create a new form
$CoreForm = New-Object system.Windows.Forms.Form
$CoreForm.ClientSize = ‘220,100’
$CoreForm.text = “Enter Name”
$CoreForm.BackColor = “#03335a”
$Coreform.StartPosition = 'CenterScreen'
$Coreform.Topmost = $true
$OKButton = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Size(160,60)
Size = New-Object System.Drawing.Size(50,25)
BackColor = "#ffffff"
Text = 'OK'
DialogResult = [System.Windows.Forms.DialogResult]::OK
}
$CoreForm.AcceptButton = $OKButton
$CoreForm.Controls.Add($OKButton)
### Inserting the text box that will accept input
$textbox1 = New-Object System.Windows.Forms.TextBox
$textbox1.Location = New-Object System.Drawing.Point(10,25) ### Location of the text box
$textbox1.Size = New-Object System.Drawing.Size(175,25) ### Size of the text box
$textbox1.Multiline = $false ### Allows multiple lines of data
$textbox1.AcceptsReturn = $false ### By hitting enter it creates a new line
#$textbox1.ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
$Coreform.Controls.Add($textbox1)
$BuildName = $textbox1.text
$Coreresult = $CoreForm.ShowDialog()
if ($Coreresult -eq [Windows.Forms.DialogResult]::OK)
{
$BuildName = $textbox1.text
}
if ($BuildName -ne "")
{
Write-Host "Click OK Name: $BuildName "
}
else
{
[Microsoft.VisualBasic.Interaction]::MsgBox("BuildName can not be blank",'OKOnly,SystemModal,Information', 'Retry Name')
Enter-BuildInfo
}
Write-Host "Inside Function Name: $BuildName "
}
Enter-BuildInfo
Write-Host "Out of Function Name: $BuildName "
My results:
Ok
Click OK Name: Test Name
Inside Function Name: Test Name
Inside Function Name:
Out of Function Name: