Need Help with Powershell script

Greeting Experts,
      May be someone can give me a hand with a script I have been working on for the past few hours.  I have a script that is designed to measure the average IO as a percentage of available bandwidth from the network card(s) of a machine. But for some reason I keep getting the following message below. Can somebody take a look at this and see what may be wrong….

Error Message
Get-WmiObject : Invalid query "select * from Win32_PerfFormattedData_Tcpip_NetworkInterface"
At line:14 char:21
+    $colInterfaces = Get-WmiObject -class Win32_PerfFormattedData_Tcpip_NetworkIn ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

$startTime = get-date
$endTime = $startTime.addMinutes(0.5)
$timeSpan = new-timespan $startTime $endTime

$count = 0
$totalBandwidth = 0

while ($timeSpan -gt 0)
   # Get an object for the network interfaces, excluding any that are currently disabled.
   $colInterfaces = Get-WmiObject -class Win32_PerfFormattedData_Tcpip_NetworkInterface |select BytesTotalPersec, CurrentBandwidth,PacketsPersec|where {$_.PacketsPersec -gt 0}

   foreach ($interface in $colInterfaces) {
      $bitsPerSec = $interface.BytesTotalPersec * 8
      $totalBits = $interface.CurrentBandwidth

      # Exclude Nulls (any WMI failures)
      if ($totalBits -gt 0) {
         $result = (( $bitsPerSec / $totalBits) * 100)
         Write-Host "Bandwidth utilized:`t $result %"
         $totalBandwidth = $totalBandwidth + $result
   Start-Sleep -milliseconds 100

   # recalculate the remaining time
   $timeSpan = new-timespan $(Get-Date) $endTime

"Measurements:`t`t $count"

$averageBandwidth = $totalBandwidth / $count
$value = "{0:N2}" -f $averageBandwidth
Write-Host "Average Bandwidth utilized:`t $value %"

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Your code works in the ise (run as administrator)
Bandwidth utilized:      0.010664 %
Bandwidth utilized:      0.004072 %
Bandwidth utilized:      0.003824 %
Bandwidth utilized:      0.006136 %
Bandwidth utilized:      0.002032 %
Bandwidth utilized:      0.006584 %
Bandwidth utilized:      0.010208 %
Bandwidth utilized:      0.005272 %
Measurements:            32
Average Bandwidth utilized:      0.01 %
PS D:\Documents\WindowsPowerShell\Scripts> $psversiontable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
CLRVersion                     4.0.30319.0
BuildVersion                   6.3.9600.17400
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Tested against W7, XP, W2003, and no errors (but no data output either except in W7).
MikeSecurityAuthor Commented:
Thanks David,....... good point....  :)
Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I object. David's comments just confirmed that the script works. Whether you run it in ISE or in the shell directly does not matter.
David Johnson, CD, MVPOwnerCommented:
There are scripts that will not run in the ise.. and there are scripts that depend upon a specific version of powershell. that is why I specified that it ran in the ise and worked using PSVersion 4.0

This script obviously requires version 3 or higher so adding
#Requires -Version 3.0

if you want to restrict a script from running in the ise then put this test at the beginning of your script.
if ($psISE -eq $null) { write-output "This script will not run in the ISE"
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
David, nothing is obvious here. The error message is not reproducible - as said, I tried with older PS on older OS and there is just no output.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I suppose the relevant part is run as administrator, not the ISE. I'm 100% positive that ISE makes no change here.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.