RegEx validate NetBIOS computer name for Domain Controllers

1
2
3
4
5
6
7
8
9
# DomainController NetBIOS naming convention validation - using RegEx.
# This RegEx requires 1-11 characters in length, allows A-Z and 0-9 PLUS Requiring an additional 4 characters, to be 'DC' followed by 2 digits.
$regString = '^[A-Z0-9-]{1,11}DC[0-9]{2}$'
$netBIOSname = 'ADFREWGEHDEDC01'
If ($netBIOSname -cmatch $regString){
    Write-Host "Name passes"
}else{
    Write-Host "Name fails"
}

PowerShell Tail – Watch log file as data is appended by script

Add this snippet to any script which writes to a log file, and it will open a second PowerShell console window with which you can watch the logfile as it is updated in real-time.

1
2
$Logfile = "$env:systemroot\Temp\$Logname.log"
Start-Process -FilePath $pshome\powershell.exe -ArgumentList @("-NoExit","&`{Get-Content -LiteralPath $Logfile -Tail 50 -Wait`}")

Start-Transcript – Execute only if in console and not ISE

Due to functionality differences between the PowerShell console and PowerShell ISE, there are times you only want to run certain functions and/or commands if the script is running in a console window. Below is an example of how to do so.

1
2
3
4
5
6
# Log script output to file - only works in console, not ISE.
If ($host.Name -notlike "*ISE*"){
    $scriptLog = "dcpromo_$(Get-Date -f yyyy-MM-dd_HH_mm_ss).log"
    $logging = Start-Transcript -Path $scriptLog
    Write-Host "`tTranscript started, logging to: $($logging.Path)" -ForegroundColor Cyan
}