none
Parsing command output to custom

    Question

  • I have a DP backup command "Omnisv -status" and output as below:

        ProcName      Status  [PID]    
    ===============================
        crs         : Active  [10748]
        mmd         : Active  [7388]
        kms         : Active  [6628]
        hpdp-idb    : Active  [7108]
        hpdp-idb-cp : Active  [7692]
        hpdp-as     : Active  [11196]
        omniinet    : Active  [7244]
        Sending of traps disabled.
    ===============================
    Status: All Data Protector processes/services up and running.

    I need to parse this output in powershell like:

    ProcName="crs" Status="Active"

    ProcName="mmd" Status="Active"
    .
    .
    .
    ProcName="omniinet" Status="Active"
    Status = "All Data Protector processes/services up and running."

    Can anybody please help me in this?

    Thursday, May 17, 2018 10:20 AM

Answers

  • $txt = @'
    ProcName      Status  [PID]    
     ===============================
         crs         : Active  [10748]
         mmd         : Active  [7388]
         kms         : Active  [6628]
         hpdp-idb    : Active  [7108]
         hpdp-idb-cp : Active  [7692]
         hpdp-as     : Active  [11196]
         omniinet    : Active  [7244]
         Sending of traps disabled.
     ===============================
    '@
    $txt | ?{$_ -match ':'} | %{ [pscustomobject]@{ProcName = $_.Split(':')[0];Status =  $_.Split(':')[1]}}


    \_(ツ)_/

    • Marked as answer by Ansif Rahman Friday, May 18, 2018 5:11 AM
    Thursday, May 17, 2018 10:34 AM
    Moderator
  • $Time = Get-Date
    $ServiceStatus = omnisv -status
    $ServiceStatus = $ServiceStatus -replace '\[.*\]' | ?{$_ -match ':'} | %{ [pscustomobject]@{ProcName = $_.Split(':')[0];Status =  $_.Split(':')[1]}}
    $FinalOutput=$ServiceStatus |Where-Object {$_.ProcName -NotMatch "Status"}|select-object @{name="ProcName"; expression={"Process_Name=`""+($_.ProcName).ToString().Trim()+"`""}},@{name="Status"; expression={"Status=`""+($_.Status).ToString().Trim()+"`""}},@{name="Timestamp"; expression={"Timestamp=`""+($Time)+"`""}}| ft -HideTableHeaders
    $FinalOutput

    Output:

    Process_Name="crs"                           Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="mmd"                           Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="kms"                           Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="hpdp-idb"                      Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="hpdp-idb-cp"                   Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="hpdp-as"                       Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="omniinet"                      Status="Active"                              Timestamp="05/18/2018 07:09:50"            

    • Marked as answer by Ansif Rahman Friday, May 18, 2018 5:11 AM
    Friday, May 18, 2018 5:11 AM

All replies

  • Split the lines on the ":" and create custom objects for each row.


    \_(ツ)_/

    Thursday, May 17, 2018 10:30 AM
    Moderator
  • $txt = @'
    ProcName      Status  [PID]    
     ===============================
         crs         : Active  [10748]
         mmd         : Active  [7388]
         kms         : Active  [6628]
         hpdp-idb    : Active  [7108]
         hpdp-idb-cp : Active  [7692]
         hpdp-as     : Active  [11196]
         omniinet    : Active  [7244]
         Sending of traps disabled.
     ===============================
    '@
    $txt | ?{$_ -match ':'} | %{ [pscustomobject]@{ProcName = $_.Split(':')[0];Status =  $_.Split(':')[1]}}


    \_(ツ)_/

    • Marked as answer by Ansif Rahman Friday, May 18, 2018 5:11 AM
    Thursday, May 17, 2018 10:34 AM
    Moderator
  • Thanks @jrv 

    But the command you provided is not looping.It is just giving me the first proc.

    PS C:\Users\an382696.WIPRO> $txt = @'
         crs         : Active  [10748]
         mmd         : Active  [7388]
         kms         : Active  [6628]
         hpdp-idb    : Active  [7108]
         hpdp-idb-cp : Active  [7692]
         hpdp-as     : Active  [11196]
         omniinet    : Active  [7244]
    '@
    $txt | ?{$_ -match ':'} | %{ [pscustomobject]@{ProcName = $_.Split(':')[0];Status =  $_.Split(':')[1]}}

    ProcName          Status                             
    --------          ------                             
         crs           Active  [10748]...       

    And could you please help me to ignore [10748] and all other things within "[]"

    Friday, May 18, 2018 3:52 AM
  • $Time = Get-Date
    $ServiceStatus = omnisv -status
    $ServiceStatus = $ServiceStatus -replace '\[.*\]' | ?{$_ -match ':'} | %{ [pscustomobject]@{ProcName = $_.Split(':')[0];Status =  $_.Split(':')[1]}}
    $FinalOutput=$ServiceStatus |Where-Object {$_.ProcName -NotMatch "Status"}|select-object @{name="ProcName"; expression={"Process_Name=`""+($_.ProcName).ToString().Trim()+"`""}},@{name="Status"; expression={"Status=`""+($_.Status).ToString().Trim()+"`""}},@{name="Timestamp"; expression={"Timestamp=`""+($Time)+"`""}}| ft -HideTableHeaders
    $FinalOutput

    Output:

    Process_Name="crs"                           Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="mmd"                           Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="kms"                           Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="hpdp-idb"                      Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="hpdp-idb-cp"                   Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="hpdp-as"                       Status="Active"                              Timestamp="05/18/2018 07:09:50"            
    Process_Name="omniinet"                      Status="Active"                              Timestamp="05/18/2018 07:09:50"            

    • Marked as answer by Ansif Rahman Friday, May 18, 2018 5:11 AM
    Friday, May 18, 2018 5:11 AM
  • What about this?

    $txt = @'
    ProcName      Status  [PID]    
     ===============================
         crs         : Active  [10748]
         mmd         : Active  [7388]
         kms         : Active  [6628]
         hpdp-idb    : Active  [7108]
         hpdp-idb-cp : Active  [7692]
         hpdp-as     : Active  [11196]
         omniinet    : Active  [7244]
         Sending of traps disabled.
     ===============================
    '@
    
    function Remove-EmptySpaces{
        [CmdletBinding()]
        param(
            [Parameter(Mandatory=$true,Position=0)]$string
        )
        Begin{
            [String]$result=[String]::Empty
        }
        Process{
            foreach($char in $string){
                if(![string]::IsNullOrWhiteSpace($char)){
                    $result+=$char
                }
            }
        }
        End{
            $result
        }
    }
    
    $pattern="(?<= )(.*)(?= )"
    $regex = New-Object System.Text.RegularExpressions.Regex -ArgumentList($pattern,[System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
    [System.Text.RegularExpressions.MatchCollection]  $matches = $regex.Matches($txt)
    
    $vals=@()
    foreach($match in $matches){
        $val=  $match.Groups[1].Value
        if($val.Contains("Status")){
            continue;
        }
        else{
            if(![string]::IsNullOrEmpty($val)){
                $vals+=$val
            }
        }
    }
    
    $TheObjects = $vals.split(':')
    $obj = New-Object psobject
    
    
    #pair are values and impair are name of values
    for($i=0;$i -lt $TheObjects.Count;$i++){
        [bool]$counter=$false
        if($i%2){
            $Tvalue= $TheObjects[$i]
            $counter=$true
        }
        else{
            $Tname = $TheObjects[$i]
        }
    
        if($counter){
            $obj | Add-Member Noteproperty $Tname $Tvalue
            $conter=$false
        }
    }
    

    • Proposed as answer by j0rt3g4 Friday, May 18, 2018 5:55 AM
    Friday, May 18, 2018 5:54 AM
  • Thanks @jrv 

    But the command you provided is not looping.It is just giving me the first proc.

    PS C:\Users\an382696.WIPRO> $txt = @'
         crs         : Active  [10748]
         mmd         : Active  [7388]
         kms         : Active  [6628]
         hpdp-idb    : Active  [7108]
         hpdp-idb-cp : Active  [7692]
         hpdp-as     : Active  [11196]
         omniinet    : Active  [7244]
    '@
    $txt | ?{$_ -match ':'} | %{ [pscustomobject]@{ProcName = $_.Split(':')[0];Status =  $_.Split(':')[1]}}

    ProcName          Status                             
    --------          ------                             
         crs           Active  [10748]...       

    And could you please help me to ignore [10748] and all other things within "[]"

    Works absolutely fine for me.


    \_(ツ)_/

    Friday, May 18, 2018 6:41 AM
    Moderator