Skip to content

Commit 3246362

Browse files
committed
Remove PSv3-incompatible lines from startup script
This change fixes #248 which reports that the extension cannot be loaded on Windows 7 machines running PowerShell v3 (and probably also any machine running PowerShell v4). The Start-EditorServices.ps1 script relied in features only present in PowerShell v5. New code paths have been added to the script to make it compatible with v3 and v4.
1 parent 40bca0a commit 3246362

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

scripts/Start-EditorServices.ps1

+20-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ param(
5454
$ConfirmInstall
5555
)
5656

57+
# Are we running in PowerShell 5 or later?
58+
$isPS5orLater = $PSVersionTable.PSVersion.Major -ge 5
59+
5760
# This variable will be assigned later to contain information about
5861
# what happened while attempting to launch the PowerShell Editor
5962
# Services host
@@ -81,8 +84,14 @@ function Test-PortAvailability($PortNumber) {
8184
$portAvailable = $true;
8285

8386
try {
84-
$ipAddress = [System.Net.Dns]::GetHostEntryAsync("localhost").Result.AddressList[0];
85-
$tcpListener = [System.Net.Sockets.TcpListener]::new($ipAddress, $portNumber);
87+
if ($isPS5orLater) {
88+
$ipAddress = [System.Net.Dns]::GetHostEntryAsync("localhost").Result.AddressList[0];
89+
}
90+
else {
91+
$ipAddress = [System.Net.Dns]::GetHostEntry("localhost").AddressList[0];
92+
}
93+
94+
$tcpListener = New-Object System.Net.Sockets.TcpListener @($ipAddress, $portNumber)
8695
$tcpListener.Start();
8796
$tcpListener.Stop();
8897

@@ -100,7 +109,7 @@ function Test-PortAvailability($PortNumber) {
100109
return $portAvailable;
101110
}
102111

103-
$rand = [System.Random]::new()
112+
$rand = New-Object System.Random
104113
function Get-AvailablePort {
105114
$triesRemaining = 10;
106115

@@ -128,9 +137,9 @@ if ((Test-ModuleAvailable "PowerShellGet") -eq $false) {
128137

129138
# Check if the expected version of the PowerShell Editor Services
130139
# module is installed
131-
$parsedVersion = [System.Version]::new($EditorServicesVersion)
140+
$parsedVersion = New-Object System.Version @($EditorServicesVersion)
132141
if ((Test-ModuleAvailable "PowerShellEditorServices" -RequiredVersion $parsedVersion) -eq $false) {
133-
if ($ConfirmInstall) {
142+
if ($ConfirmInstall -and $isPS5orLater) {
134143
# TODO: Check for error and return failure if necessary
135144
Install-Module "PowerShellEditorServices" -RequiredVersion $parsedVersion -Confirm
136145
}
@@ -141,7 +150,12 @@ if ((Test-ModuleAvailable "PowerShellEditorServices" -RequiredVersion $parsedVer
141150
}
142151
}
143152

144-
Import-Module PowerShellEditorServices -RequiredVersion $parsedVersion -ErrorAction Stop
153+
if ($isPS5orLater) {
154+
Import-Module PowerShellEditorServices -RequiredVersion $parsedVersion -ErrorAction Stop
155+
}
156+
else {
157+
Import-Module PowerShellEditorServices -Version $parsedVersion -ErrorAction Stop
158+
}
145159

146160
# Locate available port numbers for services
147161
$languageServicePort = Get-AvailablePort

0 commit comments

Comments
 (0)