How to get only STDOUT: value from command output in powershell

Welcome Forums General PowerShell Q&A How to get only STDOUT: value from command output in powershell

Viewing 4 reply threads
  • Author
    Posts
    • #215976
      Participant
      Topics: 1
      Replies: 0
      Points: -8
      Rank: Member

      In my powershell script, I have bolt command as below

      bolt command run hostname --targets winrm://158.28.0.546 --no-ssl -user testuser123 -password test@84p

      Above command executes successfully and returns  output as below

      Starting on..
      Finished at..
      STDOUT:
      VALUE123
      Success at..
      Run on ..

      From above output, I want to eliminate messages like Starting on,Finished at, Success at , Run on .. etc.

      I want to return only STDOUT: values from command.

      I want to have command similar to below, for getting only STDOUT: value

      bolt command run hostname --targets winrm://158.28.0.546 --no-ssl -user testuser123 -password test@84p | Select STDOUT:

      How to get only only STDOUT: value from commad output.

      Kindly suggest.

      • This topic was modified 1 month, 2 weeks ago by Chandra68.
      • This topic was modified 1 month, 2 weeks ago by Chandra68.
    • #215991
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      bolt is not a powershell command.  The PowerShell interpreter is just calling the associated executable using your syntax.  You could just run that command in a cmd.exe shell.  The results of the command are returned as a string object to PS.  Recommend reviewing documentation on that command.

      • This reply was modified 1 month, 2 weeks ago by Mike R..
    • #215994
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      Assuming Bolt running on a windows machine (not Linux), and that the text output is a single [String] not a string array [String[]],
      You’ll just have to parse it manually like you do in Bash as in:

      $SampleBotOutput = @'
      Starting on..
      Finished at..
      STDOUT:
      VALUE123
      other lines
      bla bla bla
      Success at..
      Run on ..
      '@
      
      $StartMarker = 'STDOUT:'
      $EndMarker   = 'Success at'
      
      $myStdOut = ((($SampleBotOutput -split $StartMarker)[1] -split $EndMarker)[0]).Trim()
      
      "STDOUT = ($myStdOut)"
      
    • #216003
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      All of the output is from STDOUT. If you assign a variable you may need to add the call operator. I would test success and fail scenarios to see if the parse would work, but if it’s always the same line you can do something simple like this:

      $output = @"
      Starting on..
      Finished at..
      STDOUT:
      VALUE123
      Success at..
      Run on ..
      "@
      
      #Always the same number of lines
      #($output -split [environment]::NewLine)[3]
      
      #Select String to find the line with STDOUT and get next line
      ($output -split [environment]::NewLine) | Select-String 'STDOUT:' -Context 0,1 | ForEach-Object {
          $_.Context.PostContext
      }
      
    • #216153
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Check out Adam’s post where he offers a small script to take care of this for you. I think it will help you achieve your goal.

      https://adamtheautomator.com/powershell-start-process/

      I hope this is helpful!

Viewing 4 reply threads
  • You must be logged in to reply to this topic.