Skip to content

Commit 7456bb4

Browse files
committed
fix(ScriptTemplate): fix ConvertTo-CliXml handling for PowerShell versions < 7.5
1 parent 987ba5b commit 7456bb4

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Compiler/Resources/ScriptTemplate.ps1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,15 @@ process {
132132
}
133133
return '[Tuple]::Create(' + ($Elements -join ', ') + ')';
134134
} elseif ($Type.IsSerializable -and -not $Type.IsPrimitive) {
135-
return "(ConvertFrom-CliXML -InputObject '$(($Value | ConvertTo-CliXml -Depth 5) -replace "'", "''")')";
135+
# For PowerShell versions without native inline ConvertTo-CliXml support,
136+
# export the argument to a temporary CLIXML file and import it at runtime.
137+
if ($PSVersionTable.PSVersion -lt [Version]'7.5') {
138+
$argFile = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), ('arg_' + ([System.Guid]::NewGuid().ToString()) + '.clixml'))
139+
$Value | ExportTo-CliXml -Path $argFile -Depth 5
140+
return "(Import-Clixml -Path '$argFile')";
141+
} else {
142+
return "(ConvertFrom-CliXML -InputObject '$(($Value | ConvertTo-CliXml -Depth 5) -replace "'", "''")')";
143+
}
136144
}
137145

138146
return ConvertTo-Json -InputObject $Value;
@@ -240,6 +248,10 @@ try {
240248
return $capturedErrors
241249
}
242250
} finally {
251+
# Always clean arguments
252+
$argPattern = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'arg_*.clixml')
253+
Get-ChildItem -Path $argPattern -File -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
254+
243255
if ($DebugPreference -ne 'Continue') {
244256
if (Test-Path $wrapperPath) {
245257
Remove-Item -Path $wrapperPath -Force -ErrorAction SilentlyContinue -WhatIf:$False

0 commit comments

Comments
 (0)