From 9fe17ee61f3dc841b90b419770081f360695ffcc Mon Sep 17 00:00:00 2001 From: SrikanthMyakam Date: Tue, 31 Dec 2024 13:11:44 +0530 Subject: [PATCH] powershell - ouput as json 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. --- lisa/tools/powershell.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lisa/tools/powershell.py b/lisa/tools/powershell.py index d6107a5cca..1e14f9f38c 100644 --- a/lisa/tools/powershell.py +++ b/lisa/tools/powershell.py @@ -3,6 +3,7 @@ import base64 from typing import Any +import json from xml.etree import ElementTree from lisa.executable import Tool @@ -48,6 +49,7 @@ 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, @@ -55,7 +57,9 @@ def run_cmdlet( # 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 ) @@ -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(