Mike
asked on
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_Tc pip_Networ kInterface "
At line:14 char:21
+ $colInterfaces = Get-WmiObject -class Win32_PerfFormattedData_Tc pip_Networ kIn ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException, Microsoft. PowerShell .Commands. GetWmiObje ctCommand
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_Tc
At line:14 char:21
+ $colInterfaces = Get-WmiObject -class Win32_PerfFormattedData_Tc
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,
$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 %"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Tested against W7, XP, W2003, and no errors (but no data output either except in W7).
ASKER
Thanks David,....... good point.... :)
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.
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
}
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
}
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.
I suppose the relevant part is run as administrator, not the ISE. I'm 100% positive that ISE makes no change here.