Parse data using Powershell and convert to Json

255 Views Asked by At

Background.

i have IBM CDC Replication engine and i need to check subscriptions status by using Zabbix.

Im calling subs status by cmd.exe /C "C:\Program Files\IBM\InfoSphere Data Replication\Management Console\bin\chcclp.exe" -f c:\CDC_Check.txt where CDC_Check.txt is script for that chcclp CLI

//Check the status of all subscriptions

//Source datastore:  ***
//Target datastore:  ***


chcclp session set to cdc;

// Turn on verbose output.
set verbose;

// Setting variables.
set variable name "ACCESS_HOSTNAME" value "";
set variable name "ACCESS_PORT" value "";
set variable name "ACCESS_USERNAME" value "";
set variable name "ACCESS_PASSWORD" value "";
set variable name "SOURCE_DATASTORE" value "";
set variable name "TARGET_DATASTORE" value "";

// Connecting to Access Server.
connect server
    hostname "%ACCESS_HOSTNAME%"
    port "%ACCESS_PORT%"
    username "%ACCESS_USERNAME%"
    password "%ACCESS_PASSWORD%";

// Connecting to the source and target datastores.
connect datastore name "%SOURCE_DATASTORE%";
connect datastore name "%TARGET_DATASTORE%";

// Setting the datastore context.
select datastore name "%SOURCE_DATASTORE%" context source;
select datastore name "%TARGET_DATASTORE%" context target;

// List replication state and latency of all subscriptions.
monitor replication;

// Disconnecting from datastores and Access Server.
disconnect datastore name "%SOURCE_DATASTORE%";
disconnect datastore name "%TARGET_DATASTORE%";

// Disconnect from Access Server and terminate the script.
disconnect server;
exit;

and im receiving following result:
enter image description here
enter image description here

Im trying to parse Subscription + Status and move it to Json for next integration with zabbix. Im very new in PS so i still have no normal progress. I understand idea that i need to capture anything that going under SUBSCRIPTIONS and STATE and write it to Json.

1

There are 1 best solutions below

8
On

The first step would be to redirect the output of the app so you can read it in for parsing.

cmd.exe /C "C:\Program Files\IBM\InfoSphere Data Replication\Management Console\bin\chcclp.exe" -f c:\CDC_Check.txt > C:\temp\file.log

Then you can use the get Get-Content cmdlet to get it in your console session

$fileContent = Get-Content -Path "C:\temp\file.log"

Once it's in an array you can parse it like so.

$i=0
$fileContent | %{
  $i++
  if($_ | Select-String -Pattern "SUBSCRIPTION       STATE" -CaseSensitive){
    $headerIndex = $i
  }
}
$headerIndex += 2 #start of report data to capture

This loop continues until it finds the blank line in the output

$hashObj= @{}
for ($x = $headerIndex; $fileContent[$x].length -gt 1 ;$x++){ 
  $hashObj.Add("$($fileContent[$x].Substring(0,19).Trim(' '))","$($fileContent[$x].Substring(20,19).Trim(' '))")
}    

#converts your hashtable to json object
$jsonObj = $hashObj | convertto-json

Not entirely sure how you need the json formatted but this will be what to expect your output to be similar to

{
  "S_ACC_D":  "Mirror Continuous",
  "S_ACC_E":  "Mirror Continuous",
  "S_ACC_A":  "Mirror Continuous",
  "S_ACC_B":  "Mirror Continuous",
  "S_ACC_C":  "Mirror Continuous"
}