PowerShell Script to Query Wireless Modem and pull Asset Information.

By | February 3, 2014
Share

Pulling a Wireless WAN Modem’s information while in the field is a daunting task. Here is a script to pull this information using AT commands with in a PowerShell script. The script was tested on a CF-53J Toughbook laptop with an embedded Sierra 7750 (Verizon LTE) modem. The script displays methods of capturing the information to a text file or writing the information to the registry.

QueryWANModem.ps1 – GitHub Link:

###################################################################
# ChangeLog -
# 2/2/14 BG - Added code to write information into the registry
# 4/9/13 BG - Added CGMR command to pull Firmware date.
###################################################################

$oInvocation = (Get-Variable MyInvocation).Value
$sCurrentDirectory = Split-Path $oInvocation.MyCommand.Path

Function fQueryModem($sQueryString, $sRegExp) {
    $oComPort = New-Object System.IO.Ports.SerialPort $sComPortNumber,$sComPortSpeed,None,8,1
    $oComPort.Open()
    $oComPort.Write("AT")
    $oComPort.Write($sQueryString + "`r")
    Start-Sleep -m 50
    $tVar = $oComPort.ReadExisting()
    $tVar = ($tVar -replace "OK","").trim()
    $oComPort.Close()

    If (!($sRegExp -eq "")) {$tVar -Match $sRegExp|Out-Null; $tVar = $Matches[0]}
    return $tVar
}

#AT Commands to pull information from Modems
#"MEID", "+CGSN"			#i.e. "990000780252708"
#"Modem Model", "+CGMM"		#i.e. "MC7750"
#"Phone Number", "+CNUM"	#i.e. "+CNUM: "Line 1","+15514972305",145"
#"SIM", "+ICCID"			#i.e. "ICCID: 89148000000148583496"
#"Firmware Date", "+GMR		#i.e. "33, SWI9600M_03.05.10.09ap r5700 carmd-en-10527 2013/03/12 10:37:48"
#*Commands pulled using AT+CLAC command...

#Grab COMPort number and max ComPort speed
$sComPortNumber = Get-WmiObject Win32_PotsModem | `
	Where-Object {$_.DeviceID -like "USB\VID*" -and $_.Status -like "OK"} | `
	foreach {$_.AttachedTo}
$sModemInfPath = Get-WmiObject Win32_PotsModem | `
	Where-Object {$_.DeviceID -like "USB\VID*" -and $_.Status -like "OK"} | `
	foreach {$_.ModemInfPath}
$sModemDriverVer = Get-WmiObject Win32_PnPSignedDriver | `
	Where-Object {$_.InfName -like $sModemInfPath} | `
	foreach {$_.DriverVersion}
$sComPortSpeed = Get-WMIObject Win32_PotsModem | `
	Where-Object {$_.DeviceID -like "USB\VID*" -and $_.Status -like "OK"} | `
	foreach {$_.MaxBaudRateToSerialPort}

#Populate Variables using fQueryModem Function Call
$sMEID = fQueryModem "+CGSN" "\d{15}"
$sModemModel = fQueryModem "+CGMM" "" #Match Everything
$sPhoneNumber = fQueryModem "+CNUM" "\d{11}"
$sSIM = fQueryModem "+ICCID" "\d{20}"
#$sFirmwareDate = fQueryModem "+GMR" "\d{4}/\d{2}/\d{2}"
$sFirmwareDate = fQueryModem "+GMR" ""

#Populate TXT file with captured variables
#$sDate = Get-Date -Format "MM/dd/yyyy"
#$sOutString = "Date: $sDate
#Username: $env:username
#MEID: $sMEID
#Modem Model: $sModemModel
#Phone Number: $sPhoneNumber
#SIM Number: $sSIM
#Firmware Date: $sFirmwareDate
#Modem Driver: $sModemDriverVer"
#$sOutString | Out-File -FilePath "$sCurrentDirectory\" + $sFirmwareDate + ".TXT" -Force

#Create Registry Key with Firmware Version
New-Item -Path HKLM:\Software -Name _BHN -Value "Default Value" -Force
New-ItemProperty -Path HKLM:\Software\_BHN -Name "WWANFirmware" -PropertyType "String" -Value $sFirmwareDate
New-ItemProperty -Path HKLM:\Software\_BHN -Name "WWANSIM" -PropertyType "String" -Value $sSIM
New-ItemProperty -Path HKLM:\Software\_BHN -Name "WWANPhoneNumber" -PropertyType "String" -Value $sPhoneNumber
New-ItemProperty -Path HKLM:\Software\_BHN -Name "WWANModel" -PropertyType "String" -Value $sModemModel
New-ItemProperty -Path HKLM:\Software\_BHN -Name "WWANMEID" -PropertyType "String" -Value $sMEID
New-ItemProperty -Path HKLM:\Software\_BHN -Name "WWANComPort" -PropertyType "String" -Value $sComPortNumber

 

 

/BG

6 thoughts on “PowerShell Script to Query Wireless Modem and pull Asset Information.

  1. James

    Hi,
    Forgive me but my powershell script knowledge is terribly lacking. I’m trying to use the scrip that you have posted above, however when I try to run it, I’m returned with an error at this line:
    If (!($sRegExp -eq “”)) {$tVar -Match $sRegExp|Out-Null; $tVar = $Matches[0]}
    return $tVar

    The error is advising that it “Cannot Index into a Null Array”
    Just wondering if you came across this problem at all when you were testing, and if so, have you been able to resolve it.

    Reply
    1. Brian Gonzalez Post author

      You might be better off these days using the following command line tool too grab information on the connection:

      netsh mbn show interfaces

      netsh mbn commandline.

      /Brian

      Reply
    2. Darren

      The ‘cannot index into a null array’ error refers to the regular expression not matching anything in the reply. A possible reason is due to the local phone number not having 11 digits for example. To resolve: either change the regular expression to something that will match or just pass “”, e.g.

      replace
      $sMEID = fQueryModem “+CGSN” “\d{15}”
      with
      $sMEID = fQueryModem “+CGSN” “”

      Reply
  2. William Brinker

    I started running this script and it worked well on a few computers, but then I ran into this on a few. I’m not sure where to go from here. When I ran the line by itself, it got the same error, but when I removed all or all but one of the arguments, the line runs but the script is broken after that.

    New-Object : Exception calling “.ctor” with “5” argument(s): “The PortName cannot be empty.
    Parameter name: PortName”
    At \\plainfield.int\th\Users\wbrinker\ComputerInfo.ps1:11 char:17
    + … $oComPort = New-Object System.IO.Ports.SerialPort $sComPortNumber,$sC …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    Reply
    1. Brian Gonzalez Post author

      William,

      This article is a bit dated. Can you check if this command gives you the information you need?

      netsh mbn show int

      netsh mbn show int

      /BG

      Reply

Leave a Reply to Brian Gonzalez Cancel reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.