Skip to content

Instantly share code, notes, and snippets.

@aleksandr-kosobokov
Forked from jmcker/README.md
Created June 16, 2020 13:59
Show Gist options
  • Save aleksandr-kosobokov/82d0feae0db009b71de4e4315492207c to your computer and use it in GitHub Desktop.
Save aleksandr-kosobokov/82d0feae0db009b71de4e4315492207c to your computer and use it in GitHub Desktop.
Visual Basic Scripts for displaying extended network adapter and OS build info via WMI. Compatible with SysInternal's BGInfo.

BGInfo Scripts

Local Script Usage

You can also modify and test run the scripts locally using:

cscript .\network-info.vbs

Usage in BGInfo

  1. Go to Custom -> New
  2. Select VB Script File and pick the desired .vbs file
  3. OK OK OK
  4. Insert the new entry from the Fields section

Note: BGInfo seems to cache the content of the script files while it is open. If you are modifying them, you'll have to Custom -> Double Click -> OK -> OK to pull in fresh changes for each file.

Persistent Updates

You can schedule BGInfo to run occasionally (default every hour) to keep the display up to date.

  1. Download BGInfo or all of SysInternals link
  2. Unzip the files to somewhere permanent
  3. Clone or download this gist
  4. Store it somewhere permanent as well
  5. Run the following to test things out
# You can cancel the timer in the upper right corner to stop the apply
${YOUR_PATH}\BGInfo64.exe ${OTHER_PATH}\bginfo-example.bgi
  1. Modify scheduled-task.xml to use your paths (or wait to use the GUI)
  2. Import the task into Task Scheduler
  3. Set the user account General -> Change User or Group
  4. Update the paths (if you didn't already) Actions -> Edit -> Program/script and Actions -> Edit -> Start in
ON ERROR RESUME NEXT
includeInterfaces = Array()
excludeInterfaces = Array("Hyper-V Virtual Ethernet Adapter", "Npcap Loopback Adapter")
strComputer = "."
descriptionIncMatch = ""
descriptionExcMatch = ""
descriptionIncFiller = "Description = Description" ' Always true
descriptionExcFiller = "Description = Description" ' Always true
For Each interface in includeInterfaces
descriptionIncMatch = descriptionIncMatch & " OR Description = '" & interface & "'"
descriptionIncFiller = "Description != Description" ' Always false
Next
For Each interface in excludeInterfaces
descriptionExcMatch = descriptionExcMatch & " AND Description != '" & interface & "'"
Next
query = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'" _
& " AND (" & descriptionIncFiller & descriptionIncMatch & ")" _
& " AND (" & descriptionExcFiller & descriptionExcMatch & ")"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set adapterInfo = objWMIService.ExecQuery(query)
If Err.Number <> 0 Then
ShowError("Query Error:")
Else
DisplayResults(adapterInfo)
End If
Sub DisplayResults(results)
' See https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapterconfiguration for fields
For Each objIPv4 in results
SafeEcho objIPv4.Description & ":"
SafeEcho " MAC: " & objIPv4.MACAddress
If objIPv4.DHCPEnabled Then
SafeEcho " DHCP: " & objIPv4.DHCPServer
Else
SafeEcho " DHCP: Disabled"
End If
' Show all IPv4 addresses
For i=LBound(objIPv4.IPAddress) to UBound(objIPv4.IPAddress)
If IsIpv4(objIPv4.IPAddress(i)) Then
SafeEcho " IP: " & objIPv4.IPAddress(i)
SafeEcho " Subnet: " & objIPv4.IPSubnet(i)
If IsArray(objIPv4.DefaultIpGateway) Then
SafeEcho " Gateway: " & objIPv4.DefaultIpGateway(i)
End If
End If
Next
SafeEcho " DNS Domain: " & objIPv4.DNSDomain
If IsArray(objIPv4.DNSServerSearchOrder) Then
SafeEcho " DNS Servers: " & objIPv4.DNSServerSearchOrder(LBound(objIPv4.DNSServerSearchOrder))
For i=LBound(objIPv4.DNSServerSearchOrder) + 1 to UBound(objIPv4.DNSServerSearchOrder)
SafeEcho " " & objIPv4.DNSServerSearchOrder(i)
Next
End If
Next
If Err.Number <> 0 Then
ShowError("Error:")
Exit Sub
End If
End Sub
Function IsIpv4(address)
' Assume all IPv6 addresses have a :
IsIpv4 = (InStr(address,":") = 0)
End Function
' Support both wscript/cscript and BGInfo
Sub SafeEcho(message)
ON ERROR RESUME NEXT
WScript.Echo message
Echo message
Err.Clear
End Sub
Sub ShowError(strMessage)
num = Err.Number
src = Err.Source
desc = Err.Description
SafeEcho strMessage
SafeEcho num & " Source: " & src & " Desc: " & desc
Err.Clear
End Sub
ON ERROR RESUME NEXT
Const HKEY_LOCAL_MACHINE = &H80000002
Const releaseIdPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
strComputer = "."
releaseId = "N/A"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set osInfoRes = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
If Err.Number <> 0 Then ShowError("Query Error:")
' https://docs.microsoft.com/en-us/previous-versions/windows/desktop/regprov/stdregprov
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
oReg.GetStringValue HKEY_LOCAL_MACHINE, releaseIdPath, "ReleaseId", releaseId
If Err.Number <> 0 Then ShowError("Registry Error:")
DisplayResults osInfoRes.ItemIndex(0), releaseId
Sub DisplayResults(osInfo, releaseId)
' https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem
caption = Replace(osInfo.Caption, "Microsoft ", "")
SafeEcho caption & " v" & releaseId & " (" & osInfo.Version & " " & osInfo.OSArchitecture & ")"
End Sub
' Support both wscript/cscript and BGInfo
Sub SafeEcho(message)
ON ERROR RESUME NEXT
WScript.Echo message
Echo message
Err.Clear
End Sub
Sub ShowError(strMessage)
num = Err.Number
src = Err.Source
desc = Err.Description
SafeEcho strMessage
SafeEcho num & " Source: " & src & " Desc: " & desc
Err.Clear
End Sub
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2020-04-14T00:44:36.4739713</Date>
<Author>jmcker</Author>
<Description>Run BGInfo64 to update the desktop wallpaper information</Description>
<URI>\BGInfo Updatesssss</URI>
</RegistrationInfo>
<Triggers>
<RegistrationTrigger>
<Repetition>
<Interval>PT1H</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<Enabled>true</Enabled>
</RegistrationTrigger>
<BootTrigger>
<Repetition>
<Interval>PT1H</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>REPLACE THIS</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<WorkingDirectory>D:\dev\repos\bginfo-wmi-scripts</WorkingDirectory>
<Command>C:\Program Files\SysInternals\Bginfo.exe</Command>
<Arguments>bginfo-example.bgi /Timer:0 /Silent /NoLicPrompt</Arguments>
</Exec>
</Actions>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment