Hi there
I finish "converting my performance monitor script to a workflow
defiantly fast as lightning.
BUT I cant figure out how to output the data in a more readable way:(
here is the workflow:
workflow Test-performance
{
$Servers="server1","server2","server3"
# The servers are processed in parallel.
ForEach -Parallel ($server in $servers)
{
# The commands run sequentially on each server.
(Get-Counter -pscomputername $server -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 10 -MaxSamples 1)
}
now if I run it like this:
$data=test-performance
$data
I see the data although not so usable for reading
I want to see:
servername/pscomputername,cookedvalue
if I do:
$data.readings I see the performance values(again with a lot of garbage like \server\counter…:value
if I do:
$newdata=$data.readings -replace “.:"
I see the value/numbers clear
so I thought why not do something like this:
$data |Select pscomputername,@{n=“Readings”;Expression={$data.readings -replace ".:” } }| FT
but all I see is :
PSCOMPUTERNAME READINGS
server1 0
server2 0
server3 0
I think maybe there are spaces or something that’s why
because when I try it with |FL
I get:
pscomputername:server1
readings: blank line
number/reading
on my way to work I was thinking only regular expressions can do it regex I hate it:)
it worked like a champ
thanks a lot for a quick reply to put me in the right direction
now i’m going to do research and understand what exactly it does.
so now I can say I love workflows:) it gets the job done in 1/100 of the time(although it has its own tweaks and limitations)
Thanks again
The regex ‘^\s*|\s*$’ matches any amount of whitespace at the beginning or end of a string. Using that pattern with -replace (if a string variable is on the left of the -replace operator) is exactly like calling the Trim() method on the string.
Thanks again
I just finished reading the "PowerTips Monthly Volume 6: Regular Expressions " and understood it.
although I was playing around with it and saw that even ‘^\s*’ seem to do the job(I guess I have spaces only at the beginning)
one more question if you don’t mind regarding workflows:
is there away to combine that output into the workflow itself?
I tried to create an empty array before the foreach like:
$data=@()
and then before the actual command inside foreach I put:
$workflow:data=(Get-Counter -pscomputername $server -Counter “\Processor(_Total)% Processor Time” -SampleInterval 10 -MaxSamples 1)
and at the end(after the foreach completes but before I close the workflow } I put:
return $data
but then I get “pscomputername is not a recognized parameter…” error
if I remove the -pscomputername inside get-counter… it runs forever(where usually it runs for 10seconds max and gets me back the data)
Your original approach is better. It’s poor practice to create an empty array, append to it, and return the whole array in one shot in a PowerShell function (though many people do it, for some reason.) You’re forcing the entire result set to be held in memory at once, taking away the caller’s ability to stream objects via the pipeline if they desire.