Author Posts

July 7, 2016 at 12:11 pm

Hi All,

I got a task to create Powershell script to validate computer name entered by user while Operating System deployment using SCCM tool. Requirement is,
1. Computer Name should not be greater than 9 characters.
2. Computer Name should not contain special character.
3. last four characters should be numeric, ex: 'L-IN-1111'

I'm done with 1 and 2 (script code copied below for reference), can someone help me to code for last validation rule? Thanks..

—————————————–
if ($TBComputerName.Text.Length -gt 9)
{
$ErrorProvider.SetError($GBComputerName, "Computer Name cannot be more than 9 characters.")
}

elseif ($TBComputerName.Text -match "^[-_]|[^a-zA-Z0-9-_]")
{
$ErrorProvider.SetError($GBComputerName, "Invalid Computer Name, Please correct the computer name.")

——————————————
}

July 7, 2016 at 1:54 pm

The RegEx to match on four digits in the last four positions is:

'\d{4}$'

\d is a meta character representing [0-9]

{n} Using the curly braces, matches the previous element exactly n times.

$ specifies that the match must come at the end of line or string

July 7, 2016 at 2:27 pm

You can be very specific with regular expressions. Although I use regular expressions, they still look like an alien language to me. However, assuming that the 9 character requirement is 5 alpha characters and 4 numeric characters, you could do something like this:

$names = @(
    "abCDE5242",
    "abcdef4242"
    "cmpfl1230d"
    "cmptn4241"
    "cmptn-1234"
)

$pattern = "^[a-zA-Z]{5}\d{4}$"

foreach ($name in $names) {
    New-Object -TypeName PSObject -Property @{ComputerName=$name;Results=($name -match $pattern)}
}

Output:

ComputerName Results
------------ -------
abCDE5242       True
abcdef4242     False
cmpfl1230d     False
cmptn4241       True
cmptn-1234     False

July 7, 2016 at 11:07 pm

Your last computername: 'L-IN-1111'
Probably figured that one out yourself:
$pattern = "^[a-zA-Z-]{5}\d{4}$"

July 8, 2016 at 12:06 am

Just my two cents with a slight adjustment to Rob's suggestion. Since the requirement it not more than 9 characters, but no requirement for less than 9 characters, the repeat needs to be an at least with an upper bound. I added a few more samples, and changed the repeat to {0,5} which is at least 0 times and at most 5 times. I also changed the pattern to \w which is equivalent to [a-zA-Z_0-9], but you could change it to [a-zA-Z0-9]if you consider _ a special character.

$names = @(
    "abCDE5242",
    "abcdef4242"
    "cmpfl1230d"
    "cmptn4241"
    "cmptn-1234"
    "cmpt-1234"
    "cm1234"
    "cm_1234"
    "4567"
)

$pattern = "^\w{0,5}\d{4}$"

foreach ($name in $names) {
    New-Object -TypeName PSObject -Property @{ComputerName=$name;Results=($name -match $pattern)}
}

Results:

ComputerName Results
------------ -------
abCDE5242       True
abcdef4242     False
cmpfl1230d     False
cmptn4241       True
cmptn-1234     False
cmpt-1234      False
cm1234          True
cm_1234         True
4567            True

July 8, 2016 at 12:04 pm

$textbox.maxlength = 9 The masked textbox is probably more appropriate in this scenario.

When you post questions about forms make sure you mention it. There isn't a ton of forms questions in this forum.

July 8, 2016 at 12:15 pm

ex. $maskedtextbox1.Mask = 'LLLL-0000'

July 8, 2016 at 12:26 pm

You don't need to validate or use the error provider if the mask is set as you can only enter the allowed characters. edit: You might use the errorprovider to indicate a computername already in use.



#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-forums_psf {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$form1 = New-Object 'System.Windows.Forms.Form'
	$maskedtextbox1 = New-Object 'System.Windows.Forms.MaskedTextBox'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	$form1_Load={
		
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$form1.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$form1.remove_Load($form1_Load)
			$form1.remove_Load($Form_StateCorrection_Load)
			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$form1.SuspendLayout()
	#
	# form1
	#
	$form1.Controls.Add($maskedtextbox1)
	$form1.ClientSize = '213, 117'
	$form1.Name = 'form1'
	$form1.Text = 'Form'
	$form1.add_Load($form1_Load)
	#
	# maskedtextbox1
	#
	$maskedtextbox1.Font = 'Microsoft Sans Serif, 12pt'
	$maskedtextbox1.Location = '24, 34'
	$maskedtextbox1.Mask = 'L-LL-0000'
	$maskedtextbox1.Name = 'maskedtextbox1'
	$maskedtextbox1.Size = '128, 26'
	$maskedtextbox1.TabIndex = 2
	$form1.ResumeLayout()
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $form1.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$form1.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$form1.add_FormClosed($Form_Cleanup_FormClosed)
	#Show the Form
	return $form1.ShowDialog()

} #End Function

#Call the form
Call-forums_psf | Out-Null

  • This reply was modified 2 years, 2 months ago by  Dan Potter.