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

Commit 01c0aaf

Browse files
author
Bruce Payette
authored
Merge pull request #47 from BrucePay/brucepay_DirMirror
Added code so the current directory will be tracked in the compat session.
2 parents 344c096 + 06cd58a commit 01c0aaf

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Tests/CompatibilitySession.Tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,15 @@ Describe "Test the Windows PowerShell Compatibility Session functions" {
193193
Remove-Item -Recurse $fullModulePath
194194
}
195195
}
196+
197+
It "Should mirror directory changes in the compatibility session" {
198+
# Verify that the initial directories are sync'ed
199+
Invoke-WinCommand { $pwd.Path } | Should -BeExactly $pwd.Path
200+
# Change location and verify that the compat session directory also changed
201+
Push-Location ..
202+
Invoke-WinCommand { $pwd.Path } | Should -BeExactly $pwd.Path
203+
# Change back and verify again
204+
Pop-Location
205+
Invoke-WinCommand { $pwd.Path } | Should -BeExactly $pwd.Path
206+
}
196207
}

WindowsCompatibility/WindowsCompatibility.psm1

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,27 @@ $SessionConfigurationName = 'Microsoft.PowerShell'
9595

9696
Set-Alias -Name Add-WinPSModulePath -Value Add-WindowsPSModulePath
9797

98+
# Location Changed handler that keeps the compatibility session PWD in sync with the parent PWD
99+
# This only applies on localhost.
100+
$locationChangedHandler = {
101+
[PSSession] $session = Initialize-WinSession @PSBoundParameters -PassThru
102+
if ($session.ComputerName -eq "localhost")
103+
{
104+
$newPath = $_.newPath
105+
Invoke-Command -Session $session { Set-Location $using:newPath}
106+
}
107+
}
108+
109+
$ExecutionContext.InvokeCommand.LocationChangedAction = $locationChangedHandler
110+
111+
# Remove the location changed handler if the module is removed.
112+
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
113+
if ($ExecutionContext.InvokeCommand.LocationChangedAction -eq $locationChangedHandler)
114+
{
115+
$ExecutionContext.InvokeCommand.LocationChangedAction = $null
116+
}
117+
}
118+
98119
function Initialize-WinSession
99120
{
100121
[CmdletBinding()]
@@ -170,6 +191,18 @@ function Initialize-WinSession
170191
$_.Name -eq $script:SessionName
171192
} | Select-Object -First 1
172193

194+
# Deal with the possibilities of multiple sessions. This might arise
195+
# from the user hitting ctrl-C. We'll make the assumption that the
196+
# first one returned is the correct one and we'll remove the rest.
197+
$session, $rest = $session
198+
if ($rest)
199+
{
200+
foreach ($s in $rest)
201+
{
202+
Remove-PSSession $s
203+
}
204+
}
205+
173206
if ($session -and $session.State -ne "Opened")
174207
{
175208
Write-Verbose -Verbose:$verboseFlag "Removing closed compatibility session."
@@ -194,8 +227,13 @@ function Initialize-WinSession
194227
{
195228
$newPSSessionParameters.EnableNetworkAccess = $true
196229
}
197-
Write-Verbose -Verbose:$verboseFlag "Created new session on host '$computername'"
230+
231+
Write-Verbose -Verbose:$verboseFlag "Created new compatibiilty session on host '$computername'"
198232
$session = New-PSSession @newPSSessionParameters | Select-Object -First 1
233+
if ($session.ComputerName -eq "localhost")
234+
{
235+
Invoke-Command $session { Set-Location $using:PWD }
236+
}
199237
}
200238
else
201239
{

0 commit comments

Comments
 (0)