Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Divide Network Total Bytes Received by the Total Bytes PerSecond

Status
Not open for further replies.

Pyro777

MIS
Jul 14, 2006
47
0
0
US
With the below Powershell Script, I can display both the Network Total Bytes Per Second and Total Bytes Received. When I try to divide on the 3rd line, I receive the following error: (Listed Below my Code)


$cnt = ([int]"\Network Interface(B*)\Bytes Total/sec")
$cat = "\Network Interface(B*)\Bytes Received/sec"
$cant = ($cat,$cnt)
get-counter -ComputerName Server1, Server2 -counter $cant

Cannot convert value "\Network Interface(B*)\Bytes Received/sec" to type "System.Int32". Error: "Input string was not
in a correct format."
At line:1 char:1
+ $cant = ($cat/$cnt)
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: :)) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger


I am just trying to get the Network Utilization of my two servers, then display it in an HTML report
 
Instead of integer, assign the data type as a decimal for each variable that may contain decimals.

So for example:

[decimal]$cnt
[decimal]$cat
[decimal]$cant

-Carl
"The glass is neither half-full nor half-empty: it's twice as big as it needs to be."

[tab][navy]For this site's posting policies, click [/navy]here.
 
Just looked more closely at the error. It's telling you that the data you're assigning to the variables are being stored as strings, not integers (or decimals). You need to make sure you are assigning numerical values in addition to declaring the variable type as I suggested in the last post.

If you need more help, post more of the code so we can see where "Network Interface", "Bytes Total", etc., are coming from.
 
I am very new to Powershell (If you couldnt tell). How would i assign a numerical value ???

All I am wanting to do is take the Network card in my Provisioning server (Get the Total Bytes Per Second and divide it by the Total Received Per Second) so i can get a network load on that/those servers. Once im done, i want to add it to my report which emails an HTML file on my server status.

Can you assist ??

Here is an example of my script that does work for another monitoring piece...
$Process = Get-WmiObject -Computer Server21 -Query "Select * from Win32_Process where name = 'InetMgr6.exe'"| Select @{Label="Server Name";Expression={$_.__SERVER}},@{Label='Owner';Expression={$_.GetOwner().User}},@{Label="Service Name";Expression={$_.Name}},@{name="Time Process Was Started";Expression={$_.ConvertToDateTime($_.creationdate)}} | ConvertTo-Html -Fragment -PreContent "<h2>XenConsoles In Use</h2>" | Out-String
 
I'm fairly new to PowerShell as well, and most of my work has been at an intermediate level. However, I may be able to assist if you post the entire portion of code that shows how Network Interface, Bytes Total, and Bytes Received are being collected.

You have to properly define what these are and store them in a variable as an integer or decimal. Then you can add, subract, divide, or multiply them together. The problem with the way you have it written above, is that Powershell is just treating each as a string of characters. You cannot use math operators on strings of course, which is what the error is telling you.

-Carl
"The glass is neither half-full nor half-empty: it's twice as big as it needs to be."

[tab][navy]For this site's posting policies, click [/navy]here.
 
I will also try and lend a hand.

Pyro777 said:
Code:
$cnt = ([int]"\Network Interface(B*)\Bytes Total/sec")
[COLOR=#EF2929][b]write-host $cnt[/b][/color]
$cat = "\Network Interface(B*)\Bytes Received/sec"
[COLOR=#EF2929][b]write-host $cat[/b][/color]
[COLOR=#EF2929][b]###[/b][/color]$cant = ($cat,$cnt)
get-counter -ComputerName Server1, Server2 -counter $cant

Cannot convert value "\Network Interface(B*)\Bytes Received/sec" to type "System.Int32". Error: "Input string was not
in a correct format."
At line:1 char:1
+ $cant = ($cat/$cnt)
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger

1. Where are the declarations for $cnt and $cat?
2. What output are you expecting from $cnt and $cat? (ie. a number like 4000 or a string like \Network Interface(B*)\Bytes Received/sec)
3. For troubleshooting you can add diagnostic output for $cnt and $cat. And comment out the $cant. That should give you an idea of what is actually being stored in those variables. To me it looks like it's a string. See the red lines in above code.




Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top