@@ -87,11 +87,13 @@ $CompatibleModules = @(
8787# Module-scope variable to hold the active compatibility session name
8888$SessionName = $null
8989
90+ # The computer name to use if one isn't provided.
91+ $SessionComputerName = ' localhost'
92+
9093# Specifies the default configuration to connect to when creating the compatibility session
91- $DefaultConfigurationName = ' Microsoft.PowerShell'
94+ $SessionConfigurationName = ' Microsoft.PowerShell'
9295
93- # Specifies the default name of the computer on which to create the compatibility session
94- $DefaultComputerName = ' localhost'
96+ Set-Alias - Name Add-WinPSModulePath - Value Add-WindowsPSModulePath
9597
9698# Location Changed handler that keeps the compatibility session PWD in sync with the parent PWD
9799# This only applies on localhost.
@@ -123,17 +125,16 @@ function Initialize-WinSession
123125 # If you don't want to use the default compatibility session, use
124126 # this parameter to specify the name of the computer on which to create
125127 # the compatibility session.
126- # (Defaults to 'localhost')
127128 [Parameter (Mandatory = $false , Position = 0 )]
128129 [String ]
129130 [Alias (" Cn" )]
130- $ComputerName = $ script :DefaultComputerName ,
131+ $ComputerName ,
131132
132133 # Specifies the configuration to connect to when creating the compatibility session
133134 # (Defaults to 'Microsoft.PowerShell')
134135 [Parameter ()]
135136 [String ]
136- $ConfigurationName = $ script :DefaultConfigurationName ,
137+ $ConfigurationName ,
137138
138139 # The credential to use when connecting to the target machine/configuration
139140 [Parameter ()]
@@ -154,6 +155,25 @@ function Initialize-WinSession
154155 }
155156
156157 Write-Verbose - Verbose:$verboseFlag " Initializing the compatibility session on host '$ComputerName '."
158+
159+ if ($ComputerName )
160+ {
161+ $script :SessionComputerName = $ComputerName
162+ }
163+ else
164+ {
165+ $ComputerName = $script :SessionComputerName
166+ }
167+
168+ if ($ConfigurationName )
169+ {
170+ $script :SessionConfigurationName = $ConfigurationName
171+ }
172+ else
173+ {
174+ $ConfigurationName = $script :SessionConfigurationName
175+ }
176+
157177 if ($Credential )
158178 {
159179 $script :SessionName = " wincompat-$ComputerName -$ ( $Credential.UserName ) "
@@ -162,13 +182,14 @@ function Initialize-WinSession
162182 {
163183 $script :SessionName = " wincompat-$ComputerName -$ ( [environment ]::UserName) "
164184 }
185+
165186 Write-Verbose - Verbose:$verboseFlag " The compatibility session name is '$script :SessionName '."
166187
167188 $session = Get-PSSession | Where-Object {
168189 $_.ComputerName -eq $ComputerName -and
169190 $_.ConfigurationName -eq $ConfigurationName -and
170191 $_.Name -eq $script :SessionName
171- }
192+ } | Select-Object - First 1
172193
173194 # Deal with the possibilities of multiple sessions. This might arise
174195 # from the user hitting ctrl-C. We'll make the assumption that the
@@ -206,16 +227,17 @@ function Initialize-WinSession
206227 {
207228 $newPSSessionParameters.EnableNetworkAccess = $true
208229 }
209- Write-Verbose - Verbose:$verboseFlag " Creating a new compatibility session."
210- $session = New-PSSession @newPSSessionParameters
230+
231+ Write-Verbose - Verbose:$verboseFlag " Created new compatibiilty session on host '$computername '"
232+ $session = New-PSSession @newPSSessionParameters | Select-Object - First 1
211233 if ($session.ComputerName -eq " localhost" )
212234 {
213235 Invoke-Command $session { Set-Location $using :PWD }
214236 }
215237 }
216238 else
217239 {
218- Write-Verbose - Verbose:$verboseFlag " Reusing the existing compatibility session."
240+ Write-Verbose - Verbose:$verboseFlag " Reusing the existing compatibility session; 'host = $ script :SessionComputerName ' ."
219241 }
220242
221243 if ($PassThru )
@@ -244,34 +266,30 @@ function Add-WinFunction
244266 # If you don't want to use the default compatibility session, use
245267 # this parameter to specify the name of the computer on which to create
246268 # the compatibility session.
247- # (Defaults to 'localhost')
248269 [Parameter ()]
249270 [String ]
250271 [Alias (" Cn" )]
251- $ComputerName = $ script :DefaultComputerName ,
272+ $ComputerName ,
252273
253274 # Specifies the configuration to connect to when creating the compatibility session
254275 # (Defaults to 'Microsoft.PowerShell')
255276 [Parameter ()]
256277 [String ]
257- $ConfigurationName = $ script :DefaultConfigurationName ,
278+ $ConfigurationName ,
258279
259280 # The credential to use when creating the compatibility session
260281 # using the target machine/configuration
261282 [Parameter ()]
262283 [PSCredential ]
263284 $Credential
264285 )
265-
266286 # Make sure the session is initialized
267287 [void ] $PSBoundParameters.Remove (' Name' )
268288 [void ] $PSBoundParameters.Remove (' ScriptBlock' )
269289
270- Initialize-WinSession @PSBoundParameters
271-
272- $localSessionName = $script :SessionName
290+ # the session variable will be captured in the closure
291+ $session = Initialize-WinSession @PSBoundParameters - PassThru
273292 $wrapper = {
274- $session = Get-PSsession - Name $localSessionName
275293 Invoke-Command - Session $session - Scriptblock $ScriptBlock - ArgumentList $args
276294 }
277295 Set-item function:Global:$Name $wrapper.GetNewClosure ();
@@ -291,17 +309,16 @@ function Invoke-WinCommand
291309 # If you don't want to use the default compatibility session, use
292310 # this parameter to specify the name of the computer on which to create
293311 # the compatibility session.
294- # (Defaults to 'localhost')
295312 [Parameter ()]
296313 [String ]
297314 [Alias (" cn" )]
298- $ComputerName = $ script :DefaultComputerName ,
315+ $ComputerName ,
299316
300317 # Specifies the configuration to connect to when creating the compatibility session
301318 # (Defaults to 'Microsoft.PowerShell')
302319 [Parameter ()]
303320 [String ]
304- $ConfigurationName = $ script :DefaultConfigurationName ,
321+ $ConfigurationName ,
305322
306323 # The credential to use when connecting to the compatibility session.
307324 [Parameter ()]
@@ -338,16 +355,15 @@ function Get-WinModule
338355 # If you don't want to use the default compatibility session, use
339356 # this parameter to specify the name of the computer on which to create
340357 # the compatibility session.
341- # (Defaults to 'localhost')
342358 [Alias (" cn" )]
343359 [String ]
344- $ComputerName = $ script :DefaultComputerName ,
360+ $ComputerName ,
345361
346362 # Specifies the configuration to connect to when creating the compatibility session
347363 # (Defaults to 'Microsoft.PowerShell')
348364 [Parameter ()]
349365 [String ]
350- $ConfigurationName = $ script :DefaultConfigurationName ,
366+ $ConfigurationName ,
351367
352368 # The credential to use when creating the compatibility session
353369 # using the target machine/configuration
@@ -413,17 +429,16 @@ function Import-WinModule
413429 # If you don't want to use the default compatibility session, use
414430 # this parameter to specify the name of the computer on which to create
415431 # the compatibility session.
416- # (Defaults to 'localhost')
417432 [Parameter ()]
418433 [String ]
419434 [Alias (" cn" )]
420- $ComputerName = $ script :DefaultComputerName ,
435+ $ComputerName ,
421436
422437 # Specifies the configuration to connect to when creating the compatibility session
423438 # (Defaults to 'Microsoft.PowerShell')
424439 [Parameter ()]
425440 [String ]
426- $ConfigurationName = $ script :DefaultConfigurationName ,
441+ $ConfigurationName ,
427442
428443 # Prefix to prepend to the imported command names
429444 [Parameter ()]
@@ -516,10 +531,10 @@ function Import-WinModule
516531 }
517532 if ($noClobberNames )
518533 {
519- $importModuleParameters.PassThru = $true
520- foreach ($name in $noClobberNames )
521- {
522- $module = Import-Module - Name $name - NoClobber @importModuleParameters
534+ $importModuleParameters.PassThru = $true
535+ foreach ($name in $noClobberNames )
536+ {
537+ $module = Import-Module - Name $name - NoClobber @importModuleParameters
523538 # Hack using private reflection to keep the proxy module from shadowing the real module.
524539 $null = [PSModuleInfo ].
525540 GetMethod(' SetName' , [System.Reflection.BindingFlags ]' Instance, NonPublic' ).
@@ -552,17 +567,16 @@ function Compare-WinModule
552567 # If you don't want to use the default compatibility session, use
553568 # this parameter to specify the name of the computer on which to create
554569 # the compatibility session.
555- # (Defaults to 'localhost')
556570 [Parameter ()]
557571 [String ]
558572 [Alias (" cn" )]
559- $ComputerName = $ script :DefaultComputerName ,
573+ $ComputerName ,
560574
561575 # Specifies the configuration to connect to when creating the compatibility session
562576 # (Defaults to 'Microsoft.PowerShell')
563577 [Parameter ()]
564578 [String ]
565- $ConfigurationName = $ script :DefaultConfigurationName ,
579+ $ConfigurationName ,
566580
567581 # If needed, use this parameter to specify credentials for the compatibility session
568582 [Parameter ()]
@@ -613,17 +627,16 @@ function Copy-WinModule
613627 # If you don't want to use the default compatibility session, use
614628 # this parameter to specify the name of the computer on which to create
615629 # the compatibility session.
616- # (Defaults to 'localhost')
617630 [Parameter ()]
618631 [String ]
619632 [Alias (" cn" )]
620- $ComputerName = $ script :DefaultComputerName ,
633+ $ComputerName ,
621634
622635 # Specifies the configuration to connect to when creating the compatibility session
623636 # (Defaults to 'Microsoft.PowerShell')
624637 [Parameter ()]
625638 [String ]
626- $ConfigurationName = $ script :DefaultConfigurationName ,
639+ $ConfigurationName ,
627640
628641 # If needed, use this parameter to specify credentials for the compatibility session
629642 [Parameter ()]
@@ -720,7 +733,6 @@ function Copy-WinModule
720733
721734function Add-WindowsPSModulePath
722735{
723-
724736 if ($PSVersionTable.PSEdition -eq ' Core' -and -not $IsWindows )
725737 {
726738 throw " This cmdlet is only supported on Windows"
@@ -731,10 +743,24 @@ function Add-WindowsPSModulePath
731743 return
732744 }
733745
734- $WindowsPSModulePath = [System.Environment ]::GetEnvironmentVariable(" psmodulepath" , [System.EnvironmentVariableTarget ]::Machine)
735- if (-not ($env: PSModulePath ).Contains($WindowsPSModulePath ))
746+ $paths = @ (
747+ $Env: PSModulePath -split [System.IO.Path ]::PathSeparator
748+ " ${Env: UserProfile} \Documents\WindowsPowerShell\Modules"
749+ " ${Env: ProgramFiles} \WindowsPowerShell\Modules"
750+ " ${Env: WinDir} \system32\WindowsPowerShell\v1.0\Modules"
751+ [System.Environment ]::GetEnvironmentVariable(' PSModulePath' ,
752+ [System.EnvironmentVariableTarget ]::Machine) -split [System.IO.Path ]::PathSeparator
753+ )
754+
755+ $pathTable = [ordered ] @ {}
756+ foreach ($path in $paths )
736757 {
737- $env: PSModulePath += " ;${env: userprofile} \Documents\WindowsPowerShell\Modules;${env: programfiles} \WindowsPowerShell\Modules;${WindowsPSModulePath} "
758+ if ($pathTable [$path ])
759+ {
760+ continue
761+ }
762+ $pathTable [$path ] = $true
738763 }
739764
765+ $Env: PSModulePath = $pathTable.Keys -join [System.IO.Path ]::PathSeparator
740766}
0 commit comments