Skip to content
This repository was archived by the owner on Jun 16, 2021. It is now read-only.

Commit 06cd58a

Browse files
author
Bruce Payette
authored
Merge branch 'master' into brucepay_DirMirror
2 parents 328d512 + 344c096 commit 06cd58a

3 files changed

Lines changed: 78 additions & 44 deletions

File tree

WindowsCompatibility/WindowsCompatibility.psd1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Author = 'PowerShell'
1414
CompanyName = 'Microsoft Corporation'
1515
Copyright = 'Copyright (c) Microsoft Corporation. All rights reserved'
1616
Description = @'
17-
This module Provides compatibility utilities that allow PowerShell Core sessions to
17+
This module provides compatibility utilities that allow PowerShell Core sessions to
1818
invoke commands that are only available in Windows PowerShell. These utilities help you
1919
to discover available modules, import those modules through proxies and then use the module
2020
commands much as if they were native to PowerShell Core.
@@ -30,11 +30,12 @@ FunctionsToExport = @(
3030
'Copy-WinModule',
3131
'Add-WindowsPSModulePath'
3232
)
33+
AliasesToExport = @('Add-WinPSModulePath')
3334
PrivateData = @{
3435
PSData = @{
3536
Tags = @('Compatibility', 'Core')
3637
LicenseUri = 'https://opensource.org/licenses/MIT'
37-
ProjectUri = 'https://github.com/PowerShell/WindowsPowerShellCompatibilityPack'
38+
ProjectUri = 'https://github.com/PowerShell/WindowsCompatibility'
3839
ReleaseNotes = @'
3940
This is the first release of this module with the basic commands:
4041
Initialize-WinSession

WindowsCompatibility/WindowsCompatibility.psm1

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

721734
function 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
}

docs/Module/Import-WinModule.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ Import-WinModule [[-Name] <String[]>] [-Exclude <String[]>] [-ComputerName <Stri
2323

2424
This command allows you to import proxy modules from a local or remote session.
2525
These proxy modules will allow you to invoke cmdlets that are not directly supported in this version of PowerShell.
26+
2627
There are commands in the Windows PowerShell core modules that don't exist natively in PowerShell Core.
2728
If these modules are imported, proxies will only be created for the missing commands.
28-
Commands that already exist in PowerShell core will not be overridden.
29+
Commands that already exist in PowerShell Core will not be overridden.
30+
The modules subject to this restriction are:
31+
32+
- Microsoft.PowerShell.Management
33+
- Microsoft.PowerShell.Utility
34+
- Microsoft.PowerShell.Security
35+
- Microsoft.PowerShell.Diagnostics
2936

3037
By default, when executing, the current compatibility session is used,
3138
or, in the case where there is no existing session, a new default session will be created.

0 commit comments

Comments
 (0)