Try this code. I rearranged the sequence in which you executed the form open/close. I also added some error/sanity checks on the data from the form.
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[System.Windows.Forms.Application]::EnableVisualStyles()
#
#---- Form Information ----
#
function Load-Build {
$Build.Controls.Add($TextBox1)
$Build.Controls.Add($RadioButton1) #Standard
$Build.Controls.Add($RadioButton2) #Admin
$Build.Controls.Add($Button1)
$Build.Controls.Add($Groupbox1)
$Build.Add_Shown( { $Build.Activate() })
[void] $Build.ShowDialog()
}
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
$Build = New-Object system.Windows.Forms.Form
$Build.StartPosition = "CenterScreen"
$Build.SizeGripStyle = "Hide"
$Build.ControlBox = $false
$Build.Size = New-Object System.Drawing.Size(180, 200)
$Build.MinimumSize = New-Object System.Drawing.Size(180, 200)
$Build.MaximumSize = New-Object System.Drawing.Size(180, 200)
$Build.text = "Build"
$Build.TopMost = $true
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 135
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(6, 15)
$TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$TextBox1.Text = ""
$RadioButton1 = New-Object system.Windows.Forms.RadioButton
$RadioButton1.text = "Standard Build"
$RadioButton1.AutoSize = $true
$RadioButton1.width = 104
$RadioButton1.height = 20
$RadioButton1.location = New-Object System.Drawing.Point(8, 46)
$RadioButton1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$RadioButton2 = New-Object system.Windows.Forms.RadioButton
$RadioButton2.text = "Admin Build"
$RadioButton2.AutoSize = $true
$RadioButton2.width = 104
$RadioButton2.height = 20
$RadioButton2.location = New-Object System.Drawing.Point(8, 72)
$RadioButton2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "OK"
$Button1.width = 130
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(7, 100)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height = 140
$Groupbox1.width = 150
$Groupbox1.text = "Enter Computer Name"
$Groupbox1.location = New-Object System.Drawing.Point(5, 10)
$Groupbox1.controls.AddRange(@($TextBox1, $RadioButton1, $RadioButton2, $Button1))
$Build.controls.AddRange(@($Groupbox1))
$Button1.Add_Click( {
#Close Build
$Build.Close() | Out-Null
}
)
#
#---- Main Code ----
#
#---- Show Form ----
$Build.ShowDialog() | Out-Null
# Check for empty/All-Spaces computer name
If ($TextBox1.Text.Trim().Length -lt 1){
Throw "Computer name wasn't entered!!"
}
# Determine first part of Build Selected
if ($Radiobutton1.Checked) {
$Build_Type = "S" # S = Standard
}
elseif ($Radiobutton2.Checked) {
$Build_Type = "A" # A = Admin
}
else{
Throw "This should never happen! No RadioButton checked!!!"
}
# Determine the Full Computer Name
$OSDFullName = "{0}{1}" -f $Build_Type, $TextBox1.Text.Trim().ToUpper()
#### If you want a hyphen between the computer name and the build type, use the line below
#$OSDFullName = "{0}-{1}" -f $Build_Type, $TextBox1.Text.Trim().ToUpper()
#
#################################
# Omitting SMS code because I don't have any SMS!
#$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
#$TSEnv.Value("OSDComputerName") = "$OSDFullName"
#$TSEnv.Value($TSBuild) = $Build_Type