Windows Tip: Determine bandwidth using WMI script

March 16, 2007, 01:17 PM —  ITworld.com — 

Send your Windows question to Mitch today! | See other Windows tips



I've been plugging the Windows Vista Resource Kit for the last couple of newsletters, and by now you're aware that this title will be available in bookstores on April 4th though you can pre-order it from Amazon and other sources. To whet your appetite, here's another tip drawn from this Resource Kit with permission of Microsoft Press. Did you know that you can use WMI to measure your machine's network bandwidth using a script? The Companion DVD included in this Resource Kit includes well over a hundred scripts and other resources for administering, deploying and troubleshooting Windows Vista, and one of these scripts let's you use the Win32_PerfFormattedData_Tcpip_NetworkInterface performance monitoring class to report network bandwidth for each local adapter on the machine. This can be useful when you want to see the actual bandwidth of a network adapter, for example when you want to troubleshoot situations where your adapter autodetects the wrong speed for the network.


To use this script, copy the text below into Notepad (with Word Wrap turned off) and save it as ReportBandwidth.vbs. To run this script on a Windows Vista machine, open an elevated command prompt and type cscript reportbandwidth.vbs /? to view a list of different arguments you can use with this script. Here's the script, minus some comments at the beginning:


Option Explicit 
On Error Resume Next
dim strComputer
dim wmiNS
dim wmiQuery
dim objWMIService
Dim objLocator
dim colItems
dim objItem
Dim strUsr, strPWD, strLocl, strAuth, iFLag 'connect server parameters
Dim colNamedArguments 'WshNamed object

subCheckCscript	 'check to see if running in cscript
	Set colNamedArguments = WScript.Arguments.Named
	strComputer = colNamedArguments("c")
subCheckArguments

wmiNS = "\root\cimv2"
wmiQuery = "Select CurrentBandwidth from Win32_PerfFormattedData_Tcpip_NetworkInterface"
strUsr = colNamedArguments("u") '""'Blank for current security. Domain\Username
strPWD = colNamedArguments("p") '""'Blank for current security.
strLocl = "" '"MS_409" 'US English. Can leave blank for current language
strAuth = ""'if specify domain in strUsr this must be blank
iFlag = "0" 'only two values allowed here: 0 (wait for connection) 128 (wait max two min)

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objLocator.ConnectServer(strComputer, _
	 wmiNS, strUsr, strPWD, strLocl, strAuth, iFLag)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem in colItems
   Wscript.Echo funLine("Name: " & objItem.name)
   Wscript.Echo "CurrentBandwidth: " & funConvert("M",objItem.CurrentBandwidth)
   
Next


' *** subs are below ***
Sub subCheckCscript
If UCase(Right(Wscript.FullName, 11)) = "WSCRIPT.EXE" Then
    Wscript.Echo "This script must be run under CScript"
    WScript.Quit
End If
end Sub

Sub subCheckArguments
If colNamedArguments.Count < 4 Then
	If colNamedArguments.Exists("?") Then 
		WScript.Echo "Uses Win32_PerfFormattedData_Tcpip_NetworkInterface to determine bandwidth" _
	&	VbCrLf & "This script can take arguments. It will analyze bandwidth of network adapter"_
	&	VbCrLf & "This is useful when you want to see the actual bandwidth of a network adapter" _
	&	VbCrLf & "Perhaps to troubleshoot cases when the adapter autodetects the wrong speed" _
	& VbCrLf & "Alternate credentials can ONLY be supplied for remote connections" _
	& VbCrLf & "Try this: cscript " & WScript.ScriptName & " [/c:yourcomputername] [/u:domainName\UserName] [/p:password]" _
	& VbCrLf & "Example: cscript " & WScript.ScriptName & " /c:london /u:nwtraders\londonAdmin /p:P@ssw0rd" _
	& VbCrLf & vbTab & " Connects to a remote machine called london in the nwtraders domain with the londonAdmin user" _
	& VbCrLf & vbTab & " account and the password of P@ssw0rd. It returns the speed of each network adapter" _
	& VbCrLf & "Example: cscript " & WScript.ScriptName _
	& VbCrLf & vbTab & " Returns the speed of each network adapter on local machine"
	WScript.Quit
	End If
WScript.Echo "checking arguments"
	If Not colNamedArguments.Exists("c") Then
		WScript.Echo "Executing on Local Machine only"
		strComputer = "localHost"
	End If 
	If Not colNamedArguments.Exists("u") Then
		WScript.Echo "Executing using current user name"
		strUsr = ""
	End If 
	If Not colNamedArguments.Exists("p") Then
		WScript.Echo "Executing using current user password"
		strPWD = ""
	End If 
End If
If colNamedArguments.Count = 0 Then
	Exit Sub
End If
End Sub

Function funConvert(strC,intIN)
Select Case strC
Case "K"
funConvert = formatNumber(intIN/1000) & " KiloBytes"
Case "M"
funConvert = formatNumber(intIN/1000000) & " MegaBytes"
Case "G"
funConvert = formatNumber(intIN/1000000000) & " GigaBytes"
End Select
End Function

Function funLine(lineOfText)
Dim numEQs, separator, i
numEQs = Len(lineOfText)
For i = 1 To numEQs
	separator= separator & "="
Next
 FunLine = VbCrLf & lineOfText & vbcrlf & separator
End Function

 

ITworld.com

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff

Crimeware: Understanding New Attacks and Defenses
By Markus Jakobsson, Zulfikar Ramzan
Published Apr 6, 2008 by Addison-Wesley Professional. Part of the Symantec Press series.
Enter now! | Official rules | Sample chapter

Securing VoIP Networks: Threats, Vulnerabilities, and Countermeasures
By Peter Thermos, Ari Takanen
Published Aug 1, 2007 by Addison-Wesley Professional.
Enter now! | Official rules | Sample chapter

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources