Skip to content

Instantly share code, notes, and snippets.

@zaskem
Created July 28, 2024 23:14
Show Gist options
  • Save zaskem/af2d7f048eb977b8480bff727904f618 to your computer and use it in GitHub Desktop.
Save zaskem/af2d7f048eb977b8480bff727904f618 to your computer and use it in GitHub Desktop.
Basic Windows form to handle manual naming of OSD Task Sequence device, including basic validation.
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$OSDComputerName = $tsenv.Value("_SMSTSMachineName")
function Show-OverrideOptionsPrompt_psf {
#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$formEnterComputerName = New-Object 'System.Windows.Forms.Form'
$ButtonOK = New-Object 'System.Windows.Forms.Button'
$GroupBox = New-Object 'System.Windows.Forms.GroupBox'
$ComputerNameBox = New-Object 'System.Windows.Forms.TextBox'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
#region Generated Events
#----------------------------------------------
$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$formEnterComputerName.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
$OSDComputerName = $ComputerNameBox.Text.ToUpper()
Write-Host "RAW NAME: " $OSDComputerName
if ($OSDComputerName.Length -eq 0)
{
$OSDComputerName = 'MZO-UNKNOWN-BOX'
}
elseif ($OSDComputerName.Length -gt 15)
{
$OSDComputerName = $OSDComputerName[0..14] -join ""
}
elseif ($OSDComputerName -match "^[-_]|[^a-zA-Z0-9-_]")
{
$OSDComputerName = 'MZO-UNKNOWN-BOX'
}
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$tsenv.Value("OSDComputerName") = "$($OSDComputerName)"
Write-Host $OSDComputerName
#Remove all event handlers from the controls
try
{
$formEnterComputerName.remove_Load($Form_StateCorrection_Load)
$formEnterComputerName.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
}
#endregion Generated Events
#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$formEnterComputerName.SuspendLayout()
#
# formEnterComputerName
#
$formEnterComputerName.Controls.Add($ComputerNameBox)
$formEnterComputerName.Controls.Add($ButtonOK)
$formEnterComputerName.Controls.Add($GroupBox)
$formEnterComputerName.AcceptButton = $ButtonOK
$formEnterComputerName.CancelButton = $ButtonOK
$formEnterComputerName.ControlBox = $False
$formEnterComputerName.ClientSize = '285, 140'
$formEnterComputerName.FormBorderStyle = 'FixedDialog'
$formEnterComputerName.MaximizeBox = $False
$formEnterComputerName.MaximumSize = '285, 140'
$formEnterComputerName.MinimizeBox = $False
$formEnterComputerName.MinimumSize = '285, 140'
$formEnterComputerName.Name = 'formEnterComputerName'
$formEnterComputerName.SizeGripStyle = 'Hide'
$formEnterComputerName.StartPosition = 'CenterScreen'
$formEnterComputerName.Text = 'Enter Computer Name'
$formEnterComputerName.TopMost = $True
#
# ButtonOK
#
$ButtonOK.Location = '195, 70'
$ButtonOK.Name = 'ButtonOK'
$ButtonOK.Size = '50, 20'
$ButtonOK.TabIndex = 2
$ButtonOK.Text = 'OK'
$ButtonOK.DialogResult = 'OK'
#
# GroupBox
#
$GroupBox.Location = '20, 10'
$GroupBox.Name = 'GroupBox'
$GroupBox.Size = '255, 50'
$GroupBox.TabIndex = 0
$GroupBox.TabStop = $False
$GroupBox.Text = 'Computer name:'
#
# ComputerNameBox
#
$ComputerNameBox.Location = '25, 30'
$ComputerNameBox.Name = 'ComputerNameBox'
$ComputerNameBox.Size = '195, 70'
$ComputerNameBox.TabIndex = 1
$ComputerNameBox.Text = $OSDComputerName
$formEnterComputerName.ResumeLayout()
#endregion Generated Form Code
#----------------------------------------------
#Save the initial state of the form
$InitialFormWindowState = $formEnterComputerName.WindowState
#Init the OnLoad event to correct the initial state of the form
$formEnterComputerName.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$formEnterComputerName.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $formEnterComputerName.ShowDialog()
} #End Function
#Call the form
Show-OverrideOptionsPrompt_psf | Out-Null
# Wrap-Up / Output for TS log
Write-Host "!--> Computer Name Output: $OSDComputerName"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment