Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lisa/tools/powershell.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,24 @@ def _initialize(self, *args: Any, **kwargs: Any) -> None:
)

def _parse_error_message(self, raw: str) -> str:
# remove first line, which is "#< CLIXML"
# If stderr is empty or doesn't contain CLIXML, return as-is
if not raw or not raw.strip():
return ""

leading = "#< CLIXML"
if raw.startswith(leading):
raw = raw[len(leading) :]
root = ElementTree.fromstring(raw)
namespaces = {"ns": "http://schemas.microsoft.com/powershell/2004/04"}
error_elements = root.findall(".//ns:S[@S='Error']", namespaces=namespaces)
result = "".join([e.text for e in error_elements if e.text])

result = result.replace("_x000D__x000A_", "\n")
return result
if not raw.startswith(leading):
# Not CLIXML format, return raw stderr
return raw

# Remove CLIXML header and parse XML
raw = raw[len(leading) :]
try:
root = ElementTree.fromstring(raw)
namespaces = {"ns": "http://schemas.microsoft.com/powershell/2004/04"}
error_elements = root.findall(".//ns:S[@S='Error']", namespaces=namespaces)
result = "".join([e.text for e in error_elements if e.text])
result = result.replace("_x000D__x000A_", "\n")
return result
except ElementTree.ParseError:
# If XML parsing fails, return raw stderr (after removing CLIXML header)
return raw
Loading