PowerShell / WindowsCompatibility

Module that allows Windows PowerShell Modules to be used from PSCore6
Other
137 stars 33 forks source link

Set-Location generates an error but still works #66

Open jdhitsolutions opened 5 years ago

jdhitsolutions commented 5 years ago

I create PSDrives to use in my daily sessions, like having an S: drive that points to C:\Scripts. I can then use the built-in function S: to change locations. Works just fine in Windows PowerShell. It also works in PowerShell Core, until you import the WindowsCompatibilityModule.

PS S:\> ipmo WindowsCompatibility
PS S:\> c:
PS C:\Users\Jeff\my documents\magic briefcase\training> s:
Cannot find drive. A drive with the name 'S' does not exist.
+ CategoryInfo          : ObjectNotFound: (S:String) [Set-Location], 
DriveNotFoundException
+ FullyQualifiedErrorId : 
DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
+ PSComputerName        : localhost

PS S:>

I get an error, even though PowerShell actually changes location.

Judging by the exception details it now looks like everything is going 
through the local proxy connection.

SerializedRemoteException      : 
System.Management.Automation.DriveNotFoundException: Cannot find drive. 
A drive with the name 'S' does not exist.
                                     at 
System.Management.Automation.SessionStateInternal.GetDrive(String name, 
Boolean automount)
                                     at 
System.Management.Automation.SessionStateInternal.GetDrive(String name, 
Boolean automount)
                                     at 
System.Management.Automation.SessionStateInternal.SetLocation(String 
path, CmdletProviderContext context)
                                     at 
Microsoft.PowerShell.Commands.SetLocationCommand.ProcessRecord()
jdhitsolutions commented 5 years ago

For clarity, everything works as expected in PowerShell Core without the Windows Compatibility Module. But once that is added then things break for any drive other than a fixed disk like C:.


**********************
PowerShell transcript start
Start time: 20190124134409
Username: BOVINE320\Jeff
RunAs User: BOVINE320\Jeff
Configuration Name: 
Machine: BOVINE320 (Microsoft Windows NT 10.0.17763.0)
Host Application: C:\Program Files\PowerShell\6\pwsh.dll -noprofile
Process ID: 19868
PSVersion: 6.1.2
PSEdition: Core
GitCommitId: 6.1.2
OS: Microsoft Windows 10.0.17763 
Platform: Win32NT
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.1.2
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
WSManStackVersion: 3.0
**********************
Transcript started, output file is c:\work\cd.txt
PS C:\> get-module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   6.1.0.0    Microsoft.PowerShell.Host           {Start-Transcript, Stop-Transcript}
Manifest   6.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemPropert...
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...

PS C:\> get-pssession
PS C:\> get-command s:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        S:

PS C:\> New-PSDrive -Name S -PSProvider FileSystem -Root c:\scripts

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
S                 157.18         80.05 FileSystem    C:\scripts

PS C:\> s:
PS S:\> cd c:\
PS C:\> cd s:\
PS S:\> import-module WindowsCompatibility
PS S:\> get-module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   6.1.0.0    Microsoft.PowerShell.Host           {Start-Transcript, Stop-Transcript}
Manifest   6.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemPropert...
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     2.0.0      PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...
Script     1.0.0      WindowsCompatibility                {Add-WindowsPSModulePath, Add-WinFunction, Compare-WinModu...

PS S:\> c:
PS C:\> s:
Cannot find drive. A drive with the name 'S' does not exist.
+ CategoryInfo          : ObjectNotFound: (S:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
+ PSComputerName        : localhost
Cannot find drive. A drive with the name 'S' does not exist.
+ CategoryInfo          : ObjectNotFound: (S:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
+ PSComputerName        : localhost

PS S:\> get-pssession

 Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            --------- ------------    ------------    -----         -----------------     ------------
  1 wincompat-lo... WSMan     localhost       RemoteMachine   Opened        Microsoft.PowerShell     Available

PS S:\> stop-transcript
**********************
PowerShell transcript end
End time: 20190124134608
**********************
``
jdhitsolutions commented 5 years ago

I see now that when you run Set-Location it is actually running it through the Wincompat pssession. I looked at the session and see that it doesn't inherit my PSDrives, which maybe it should if it can. My workaround for now is to dot source the script I use that defines my PSDrives. Once I do that, everything works as expected.


if (Get-PSSession -name "wincompat-localhost-$env:username") {
  . C:\scripts\jdh-psdrives.ps1
}
sql-sith commented 5 years ago

I am also having this problem. However, in my case, the PSDrives are created in my profile, which is (of course) already dot-sourced. I can easily split that out into a separate file, but then where do I put the code to dot-source it? Is there a special profile for the "wincompat-localhost-$env:username" PSSession?