powershell script to capture part of data from command line output

powershell capture output from exe
powershell parse command output
powershell capture output to variable
powershell command output to variable
powershell output to file
powershell start-process output
powershell out-string
capture console output powershell

sorry if the title is a bit confusing, honestly i did not know how to put it in plain words or what exactly to search for. I have an output from command line showing as per below.

Remote Copy System Information
Status: Started, Normal

Target Information

Name        ID Type Status Options Policy
3PARSYSTEM1  2 IP   ready  -       mirror_config

Link Information

Target      Node  Address      Status Options
3PARSYSTEM1 0:3:1 xxx.xxx.xxx.xxx Up     -
3PARSYSTEM1 1:3:1 xxx.xxx.xxx.xxx Up     -
receive     0:3:1 receive      Up     -
receive     1:3:1 receive      Up     -

Group Information

Name         Target      Status   Role       Mode     Options
GRP001Temp   3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 00:08:09 MYT, Period 3h,over_per_alert
  LocalVV        ID   RemoteVV         ID   SyncStatus    LastSyncTime
  LUN001-Temp 13304 LUN001-TempDR 16914 Synced        2018-11-04 00:08:10 MYT

Name              Target      Status   Role       Mode     Options
GRP002-PHY01 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:17:54 MYT, Period 2h,auto_recover,over_per_alert
  LocalVV          ID   RemoteVV         ID   SyncStatus    LastSyncTime
  LUN001-VVT2.12  120 LUN001-VVT2.12  210 Syncing (33%) 2018-11-03 23:51:04 MYT

Name              Target      Status   Role       Mode     Options
GRP003-PHY02 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:27:12 MYT, Period 1h45m,auto_recover,over_per_alert
  LocalVV          ID   RemoteVV         ID   SyncStatus    LastSyncTime
  LUN002-VVT2.14  130 LUN002-VVT2.14  207 Syncing (49%) 2018-11-03 23:59:27 MYT

Name              Target      Status   Role       Mode     Options
GRP001-PRD-ORA 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 00:45:09 MYT, Period 2h,auto_recover,over_per_alert
  LocalVV                      ID   RemoteVV                     ID   SyncStatus    LastSyncTime
  ORA-PROD-VG01.35   97 ORA-PROD-VG01.35 2451 Synced        2018-11-04 00:45:54 MYT
  ORA-PROD-VG02.36   98 ORA-PROD-VG02.36 2452 Synced        2018-11-04 00:46:10 MYT
  ORA-PROD-VG03.37   99 ORA-PROD-VG03.37 2453 Synced        2018-11-04 00:45:48 MYT
  ORA-PROD-VG04.38  100 ORA-PROD-VG04.38 2454 Synced        2018-11-04 00:45:12 MYT
  ORA-PROD-VG05.39  101 ORA-PROD-VG05.39 2455 Synced        2018-11-04 00:45:12 MYT

Name              Target      Status   Role       Mode     Options
GRP001-PRD-SAP 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:24:25 MYT, Period 23m,auto_recover,over_per_alert
  LocalVV                 ID   RemoteVV                ID   SyncStatus    LastSyncTime
  SAP-PROD-APPS.4        80 SAP-PROD-APPS.4      1474 Synced        2018-11-04 01:24:28 MYT
  SAP-PROD-LOCK.19       95 SAP-PROD-LOCK.19     1490 Synced        2018-11-04 01:24:25 MYT
  SAP-PROD-SAPDT1.5      81 SAP-PROD-SAPDT1.5    1475 Synced        2018-11-04 01:25:16 MYT
  SAP-PROD-SAPDT2.6      82 SAP-PROD-SAPDT2.6    1476 Synced        2018-11-04 01:25:05 MYT
  SAP-PROD-SAPDT3.7      83 SAP-PROD-SAPDT3.7    1477 Synced        2018-11-04 01:25:07 MYT
  SAP-PROD-SAPDT4.8      84 SAP-PROD-SAPDT4.8    1478 Synced        2018-11-04 01:25:41 MYT
  SAP-PROD-SAPDT5.9      85 SAP-PROD-SAPDT5.9    1479 Synced        2018-11-04 01:25:35 MYT
  SAP-PROD-SAPDT6.10     86 SAP-PROD-SAPDT6.10   1480 Synced        2018-11-04 01:25:56 MYT

Name              Target      Status   Role       Mode     Options
GRP002-PRD-SAP 3PARSYSTEM1 Started  Primary    Periodic Last-Sync 2018-11-04 01:24:55 MYT, Period 23m,over_per_alert
  LocalVV          ID   RemoteVV          ID   SyncStatus    LastSyncTime
  SAP-PROD-VG01.10   15 SAP-PROD-VG01.10 29769 Synced        2018-11-04 01:28:44 MYT

and i want to use powershell to capture the group information and so that i can get the groupname and run another command to loop through the group name. example output is as per below.

GRP001Temp
GRP002-PHY01
GRP003-PHY02
GRP001-PRD-ORA
GRP001-PRD-SAP
GRP002-PRD-SAP

Hope you can help me with my problem. Thank You in advance.

You can split the output by the linebreak and loop over the result. If a line starts with Name, split the next line by a space and write the first element to the output.

Something like:

param($output)

$lines = $output -split [Environment]::NewLine
$name = $false
foreach($line in $lines) {
    if($name) {
        ($line -split ' ')[0] | Write-Output
        $name = $false
    }
    if($line.Startswith('Name')) {
        $name = $true
    }
}

How do I capture the output into a variable from an external process , $OutputVariable = (Shell command) | Out-String external utilities works the same as with PowerShell-native commands (you may want a Redirecting stderr to stdout with 2>&1 , so as to also capture it as part of the success  PowerShell now recognizes this as an error stream and treats part of it like a standard PowerShell error returning the red error text. Finally, another method to control command-line output is to use the Start-Process command. This PowerShell command allows you to explicitly redirect streams but not in memory.

if every group is in the Role "Primary" one easy way might be the following statement:

get-content Demo.txt | where { $_ -match "Primary" } | % { $_.Split(" ")[0] }  

It gets the lines which contain the word "Primary" and takes the first word (in your case the group name)

Parsing Command-Line Output with PowerShell -- Microsoft , When writing PowerShell scripts, in a perfect world, we'd all just use cmdlets and functions. For example, let's say I'd like to run ping.exe and capture the output to a variable. Pinging 4.4.4.4 with 32 bytes of data: PowerShell now recognizes this as an error stream and treats part of it like a standard  PreviousNext. Capture output from command line tools with PowerShell. A simple task and then again not. A customer asked me if it was possible to grab output from a command and analyze the output afterwards. In the particular case he needs to call a telnet session and check if there was a proper response from the server.

Obviously you are after the first word following the Header

Name         Target      Status   Role       Mode     Options

Not including the false header

Name        ID Type Status Options Policy

the other answers aren't excluding.

So I'd split the file content with a RegEx by this Header into sections and skip the first one. Then split each section into words separated with white space \s and get the first [0]

(Get-Content .\SO_53154266.txt -raw) -split "(?sm)^Name\s+Target.*?`r?`n" |
  Select-Object -skip 1|
    ForEach-Object { ($_ -split '\s')[0] }

Sample output:

GRP001Temp
GRP002-PHY01
GRP003-PHY02
GRP001-PRD-ORA
GRP001-PRD-SAP
GRP002-PRD-SAP

Capture output from command line tools with PowerShell, $OutputVariable = (cmd.exe /c ping localhost) | Out-String. The problem with this short sample is, the stderr is not capture, only the stdout. When I execute the command line syntax against the CMD console, the code returns the expected output; however, the Invoke-Expression does not capture said output as a usable variable. Specifically, the Array is null. In addition, the file that gets generated at C:\Log.Txt also has the expected data.

Redirecting Data with Out Cmdlets, The Out-Host cmdlet is no exception: the host window display is outside of unnecessary data that you get as a side-effect of running a command. is not recognized as a cmdlet, function, operabl e program, or script file. It turns out that executing the script through another instance of PowerShell.exe will allow you to capture all the output. For instance, consider the following script (test.ps1) that exercises the various output streams: 1: $DebugPreference = 2. 2: $VerbosePreference = 2. 3: $WarningPreference = 2. 4:

about_Redirection, By default, PowerShell sends its command output to the PowerShell console. This command sends all Success stream data to a file called script.log PS> if (​36 < 42) { "true" } else { "false" } At line:1 char:8 + if (36 < 42)  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more powershell script to capture part of data from command line output

How to save command output to file using Command Prompt or , In this guide, we'll show you the steps to quickly export a command output to a text file using Command Prompt or PowerShell on Windows 10. to practice from the command line, remember to collapse the double percent signs to singles. I’m cheating here because I know that words.txt contains one word per line. By default, the FOR command sets the loop variable to the first word of each line. If you want to capture the entire line, you need to change the delimiter.

Comments
  • thank you everyone for helping.. appreciate your kind contribution in providing the code to my problem. All code works as expected however this one is the best one to use in various situation.