Workflow output,need help

This topic contains 10 replies, has 3 voices, and was last updated by  turbo mcp 4 years, 4 months ago.

  • Author
  • #13525

    turbo mcp

    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



    # 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:
    I see the data although not so usable for reading
    I want to see:
    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 :
    server1 0
    server2 0
    server3 0

    what am I missing here?
    Thanks in advance

  • #13537

    Dave Wyatt

    I suspect that all you need to do is change $data to $_ inside your Select-Object arguments:

    $data |
    Select-Object -Property PSComputerName, @{ Name = 'Readings'; Expression = { $_.Readings -replace '.*:' } } |
  • #13551

    turbo mcp

    I tried that
    but then I get:
    pscomputername readings
    server1 ...
    server2 ...
    server3 ...

  • #13552

    turbo mcp

    I think maybe there are spaces or something that's why
    because when I try it with |FL
    I get:
    readings: blank line

  • #13555

    Dave Wyatt

    Could be. Try this as your expression, and see if it works. This would clear out any leading or trailing whitespace (including newlines):

    $_.Readings -replace '.*:' -replace '^\s*|\s*$'
    • #13567

      turbo mcp

      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

  • #13559

    Tore Groneng

    Maybe something like this?

    $data.CounterSamples | foreach {
    $dataout = "" | select Computer, Value;
    $dataout.value = $_.cookedvalue;
    $dataout.Computer = $_.path.split("\\")[2];

  • #13569

    Dave Wyatt

    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.

  • #13582

    turbo mcp

    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:
    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)

    Thanks in advance

  • #13584

    Dave Wyatt

    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.

  • #13586

    turbo mcp


You must be logged in to reply to this topic.