techthoughts2
10/7/2016 - 10:33 PM

Test-ComputerName

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
}