Skip to content

Commit

Permalink
powershell - ouput as json
Browse files Browse the repository at this point in the history
This patch enables the run_cmdlet method in lisa/tools/powershell.py to output the result as JSON when the output_json parameter is set to True.

Getting Powershell output as JSON is beneficial because it allows for easier parsing and processing of the output data.
  • Loading branch information
SRIKKANTH committed Dec 31, 2024
1 parent bcaefdb commit 9fe17ee
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lisa/tools/powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import base64
from typing import Any
import json
from xml.etree import ElementTree

from lisa.executable import Tool
Expand Down Expand Up @@ -48,14 +49,17 @@ def run_cmdlet_async(
def run_cmdlet(
self,
cmdlet: str,
output_json: bool = False,
force_run: bool = False,
sudo: bool = False,
fail_on_error: bool = True,
timeout: int = 600,
# Powershell error log is the xml format, it needs extra decoding. But
# for long running script, it needs to look real time results.
no_debug_log: bool = True,
) -> str:
) -> Any:
if output_json:
cmdlet = f"{cmdlet} | ConvertTo-Json"
process = self.run_cmdlet_async(
cmdlet=cmdlet, force_run=force_run, sudo=sudo, no_debug_log=no_debug_log
)
Expand All @@ -68,7 +72,8 @@ def run_cmdlet(
# if stdout is output already, it doesn't need to output again.
no_debug_log=not no_debug_log,
)

if output_json and result.stdout:
return json.loads(result.stdout)
return result.stdout

def wait_result(
Expand Down

0 comments on commit 9fe17ee

Please sign in to comment.