Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Last active February 5, 2021 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techthoughts2/b8edff545afc31702172f3f16d0f1754 to your computer and use it in GitHub Desktop.
Save techthoughts2/b8edff545afc31702172f3f16d0f1754 to your computer and use it in GitHub Desktop.
This retrieves the computer name and then regex validates that name against common windows machine name requirements. If the computer name matches Windows name requirements it will return true, otherwise it will return false.
<#
.Synopsis
Verifies if computername matches Windows machine naming requirements
.DESCRIPTION
This function accepts a computername string and regex verifies if that name meets Windows machine naming requirements
.PARAMETER ComputerName
String of computername to be tested
.EXAMPLE
Test-ComputerName -ComputerName PC-Num-43123
This will verify if the computer name meets windows requirements
.EXAMPLE
Test-ComputerName PC-Num-43123
This will verify if the computer name meets windows requirements - the -ComputerName does not have to be explicitly used
.OUTPUTS
Boolean value
.NOTES
Author: Jake Morrison
http://techthoughts.info
#>
function Test-ComputerName {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
Position = 0
)]
[String]
$ComputerName
)
#assume the best
[bool]$compEval = $true
if ($computerName -notmatch '(?i)(?=.{5,15}$)^(([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])\.)*([a-z\d]|[a-z\d][a-z\d\-]*[a-z\d])$') {
$compEval = $false
}
return $compEval
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment