Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Getting confused on how to manipulate output concatenation to adhere to JSON array outfile?

$
0
0

Hi all,

I think I’m siking myself out here. I am trying to run this function that checks Windows processes and outputs back whether it is “running” or “not running”:

function Get-Processes { $processes = Get-Content -Path "C:scriptsprocessprocesses.txt" $ProcessActive = Get-Process -ProcessName $processes -ErrorAction SilentlyContinue foreach($p in $processes) { if($ProcessActive -eq $null) { "$p " + "not running" } else { "$p " + "running" } } } 

When I initialize the function Get-Processes inside an array and ConvertTo-JSON the array, the JSON array exports the following:

 "processes": [ "spotify running" "chrome running" "DWM running" ], 

Ideally, I would like the JSON output to be exported as:

"processes": [ { "Name": "spotify", "Status": "running" }, { "Name": "chrome", "Status": "running" }, { "Name": "DWM", "Status": "running" }, ] 

Is there a trick on how to concatenate the if() inside the foreach() to get the desired JSON format?

Thank you!

submitted by /u/networkhappi
[link] [comments]

The post Getting confused on how to manipulate output concatenation to adhere to JSON array outfile? appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles