54
54
$ConfirmInstall
55
55
)
56
56
57
+ # Are we running in PowerShell 5 or later?
58
+ $isPS5orLater = $PSVersionTable.PSVersion.Major -ge 5
59
+
57
60
# This variable will be assigned later to contain information about
58
61
# what happened while attempting to launch the PowerShell Editor
59
62
# Services host
@@ -81,8 +84,14 @@ function Test-PortAvailability($PortNumber) {
81
84
$portAvailable = $true ;
82
85
83
86
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 )
86
95
$tcpListener.Start ();
87
96
$tcpListener.Stop ();
88
97
@@ -100,7 +109,7 @@ function Test-PortAvailability($PortNumber) {
100
109
return $portAvailable ;
101
110
}
102
111
103
- $rand = [ System.Random ]::new()
112
+ $rand = New-Object System.Random
104
113
function Get-AvailablePort {
105
114
$triesRemaining = 10 ;
106
115
@@ -128,9 +137,9 @@ if ((Test-ModuleAvailable "PowerShellGet") -eq $false) {
128
137
129
138
# Check if the expected version of the PowerShell Editor Services
130
139
# module is installed
131
- $parsedVersion = [ System.Version ]::new ($EditorServicesVersion )
140
+ $parsedVersion = New-Object System.Version @ ($EditorServicesVersion )
132
141
if ((Test-ModuleAvailable " PowerShellEditorServices" - RequiredVersion $parsedVersion ) -eq $false ) {
133
- if ($ConfirmInstall ) {
142
+ if ($ConfirmInstall -and $isPS5orLater ) {
134
143
# TODO: Check for error and return failure if necessary
135
144
Install-Module " PowerShellEditorServices" - RequiredVersion $parsedVersion - Confirm
136
145
}
@@ -141,7 +150,12 @@ if ((Test-ModuleAvailable "PowerShellEditorServices" -RequiredVersion $parsedVer
141
150
}
142
151
}
143
152
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
+ }
145
159
146
160
# Locate available port numbers for services
147
161
$languageServicePort = Get-AvailablePort
0 commit comments