Skip to content

Commit 306a520

Browse files
committed
Fix #401: Unsupported PowerShell version should display an error
This change improves a previous fix where we report an unsupported PowerShell version when the user attempts to start the extension. The previous fix utilized ConvertTo-Json to write out the session details; this caused a failure since this cmdlet was introduced in PowerShell v3. We now write out teh JSON manually in this case.
1 parent a86354d commit 306a520

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

scripts/Start-EditorServices.ps1

+17-13
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,28 @@ param(
6565
$ConfirmInstall
6666
)
6767

68-
function WriteSessionFile($sessionInfo) {
69-
ConvertTo-Json -InputObject $sessionInfo -Compress | Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop
68+
function ExitWithError($errorString) {
69+
70+
Write-Host -ForegroundColor Red "`n`n$errorString"
71+
72+
# Sleep for a while to make sure the user has time to see and copy the
73+
# error message
74+
Start-Sleep -Seconds 300
75+
76+
exit 1;
7077
}
7178

7279
# Are we running in PowerShell 2 or earlier?
7380
if ($PSVersionTable.PSVersion.Major -le 2) {
74-
$resultDetails = @{
75-
"status" = "failed"
76-
"reason" = "unsupported"
77-
"powerShellVersion" = $PSVersionTable.PSVersion.ToString()
78-
};
81+
# No ConvertTo-Json on PSv2 and below, so write out the JSON manually
82+
"{`"status`": `"failed`", `"reason`": `"unsupported`", `"powerShellVersion`": `"$($PSVersionTable.PSVersion.ToString())`"}" |
83+
Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop
7984

80-
# Notify the client that the services have started
81-
WriteSessionFile $resultDetails
82-
83-
Write-Host "Unsupported PowerShell version $($PSVersionTable.PSVersion), language features are disabled.`n"
85+
ExitWithError "Unsupported PowerShell version $($PSVersionTable.PSVersion), language features are disabled."
86+
}
8487

85-
exit 0;
88+
function WriteSessionFile($sessionInfo) {
89+
ConvertTo-Json -InputObject $sessionInfo -Compress | Set-Content -Force -Path "$SessionDetailsPath" -ErrorAction Stop
8690
}
8791

8892
# Are we running in PowerShell 5 or later?
@@ -240,5 +244,5 @@ catch [System.Exception] {
240244
$e = $e.InnerException;
241245
}
242246

243-
Write-Error ("`r`nCaught error while waiting for EditorServicesHost to complete:`r`n" + $errorString)
247+
ExitWithError ("Caught error while waiting for EditorServicesHost to complete:`r`n" + $errorString)
244248
}

0 commit comments

Comments
 (0)