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
         $count++
      }
   }
   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

MikeSecurityAsked:
Who is Participating?
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
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.0
BuildVersion                   6.3.9600.17400
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2
0

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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Tested against W7, XP, W2003, and no errors (but no data output either except in W7).
0
MikeSecurityAuthor Commented:
Thanks David,....... good point....  :)
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

QlemoBatchelor, 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.
0
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"
break
}
0
QlemoBatchelor, 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.
0
QlemoBatchelor, 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.
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.