Network Adapter Information (GUID, MAC, IP, Interface Index, Name, Description)

Recently we needed to find a network adapter, provided with nothing but the GUID. Using the code below, we were able to pull the details for our network cards, and find the one in question. Then we went a bit further to poll more information.

1
2
3
4
5
6
$WMIinfo = Get-WmiObject win32_networkadapter -Property guid, Name, MACAddress
$NICinfo = Get-NetAdapter | Where-Object {$_.MacAddress -eq $WMIinfo.MACAddress -replace ':','-' } | Select-Object ifIndex, Status, LinkSpeed, Name
$IPinfo = Get-NetIPAddress | Where-Object {$_.InterfaceIndex -eq $NICinfo.ifIndex}
$WMIinfo | Select-Object MACAddress, GUID, Name | Out-String
$NICinfo | Out-String
$IPinfo | Select-Object IPAddress | Out-String

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
}