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!

Need help with cmdlet output

Status
Not open for further replies.

jbarelds

MIS
Aug 5, 2008
30
NL
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.
 
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
faq732-7259
My Blog
 
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.



 
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
faq732-7259
My Blog
 
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()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top