INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Divide Network Total Bytes Received by the Total Bytes PerSecond

Divide Network Total Bytes Received by the Total Bytes PerSecond

Divide Network Total Bytes Received by the Total Bytes PerSecond

(OP)
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

RE: Divide Network Total Bytes Received by the Total Bytes PerSecond

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."

    For this site's posting policies, click here.

RE: Divide Network Total Bytes Received by the Total Bytes PerSecond

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.

RE: Divide Network Total Bytes Received by the Total Bytes PerSecond

(OP)
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

RE: Divide Network Total Bytes Received by the Total Bytes PerSecond

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."

    For this site's posting policies, click here.

RE: Divide Network Total Bytes Received by the Total Bytes PerSecond

I will also try and lend a hand.

Quote (Pyro777)

CODE -->

$cnt = ([int]"\Network Interface(B*)\Bytes Total/sec")
write-host $cnt
$cat = "\Network Interface(B*)\Bytes Received/sec"
write-host $cat
###$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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close