Workflow output,need help

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

  • Author
    Posts
  • #13525

    turbo mcp
    Participant

    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

    what am I missing here?
    Thanks in advance

  • #13537

    Dave Wyatt
    Moderator

    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 '.*:' } } |
    Format-Table
    
  • #13551

    turbo mcp
    Participant

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

  • #13552

    turbo mcp
    Participant

    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

  • #13555

    Dave Wyatt
    Moderator

    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
      Participant

      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
    Participant

    Maybe something like this?


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

  • #13569

    Dave Wyatt
    Moderator

    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
    Participant

    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)

    Thanks in advance

  • #13584

    Dave Wyatt
    Moderator

    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
    Participant

    understood
    Thanks

You must be logged in to reply to this topic.