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
104113function 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 )
132141if ((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