Last active
February 5, 2021 18:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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