Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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 from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

jbarelds (MIS) (OP)
2 Dec 11 10:57
Hi all,

Been fiddling with PowerShell last couple of days, but there's this thing I can't get my head around.

Let's say I'd like some message if HTTP-GET requests are happening on SomeSite. I'd think I'd need a syntax like:

if((Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec") -ne 0) {Write-Host "We received" $_ "GET requests last sec"}

The above does not work, because Get-Counter, like all cmdlets, outputs (a table showing) objects of various types, inlcuding a header. In this example an object of type 'System.Object.PerformanceCounterSampleSet' is returned.

I'm used to cmd scripting, and never had to deal with anything else then strings and ints. I'm confused how I can get Get-Counter to return just an int value.
jmeckley (Programmer)
2 Dec 11 12:43
something like

CODE

get-counter | where {$_.Name == "\Web Service(SomeSite)\Get Requests/sec"} | select {$_.Count} | foreach-object {write-host "We received" $_ "GET requests last sec"}
get all counters
where name is ...
select the count property of that object
write to host

Jason Meckley
Senior Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive
My Blog

jbarelds (MIS) (OP)
2 Dec 11 14:21
Hi jmeckley,

I can't get your code to work. Using just the...

CODE

Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec"
...part from my example, I was at least able to get data from the right counter. Considering your code, I have the following questions...

CODE

get-counter | where {$_.Name == "\Web Service(SomeSite)\Get Requests/sec"} ..etc..
Are you sure the Get-Counter cmdlet is returning objects that have a .Name property? How to determine this? Also, the '==' condition comparisson is invalid in powershell, you're probably used to C# or Java, right? In powershell, use '-eq'.

CODE

select {$_.Count}
Don't understand what you're trying to do here. Assuming you agree on $_ to represent an array (with possible various object types), I can't see why you'd call the .Count property, which will return the number of objects in the array.



 
jmeckley (Programmer)
2 Dec 11 14:45
i'm sure there are bugs in the script as I wrote this from memory without testing it.

if the property isn't Name it's something else.
when piping from one command to the next ps will iterate over the collection allowing you to process each object individually. $_ references the current item in the collection. so $_.Count would give you the value of the Count property on the current object, assuming the Count property exists.

powershell pipes objects from one cmdlet to the next, not strings or integers. if you just want a count of the webserver counts it might look like this

CODE

$counters = Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec"
write-host $counters.length

Jason Meckley
Senior Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive
My Blog

jbarelds (MIS) (OP)
5 Dec 11 7:34
Hi jmeckley,

Thanks for the info above. Based on your comment I started googling again, and stumbled upon the online PowerShell SDK library, and the Get-Method Cmdlet, which will display all properties and methods available for any given cmdlet.

The code I used in this thread was just an example; what I really wanted was to understand how to parse cmdlet output. Knowing how to show available methods and properties for cmdlets is crucial for doing this.

Working line:

CODE

$gets = [float]((Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec").CounterSamples | select CookedValue).CookedValue.ToString()

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!

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