Repository navigation
Closed
Labels
Resolution-AnsweredThe question is answered.
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Cert:\ PSDrive provided by Certificate provider is unavailable on PowerShell 5.1 launched by cmd.exe if and only if the cmd.exe was launched on PowerShell 7.3. This issue causes an error on running a batch script which launches PowerShell 5.1.
Although this issue happens on PowerShell 5.1, I file it for PowerShell 7.3 because it is reproduced only when cmd.exe is launched on PowerShell 7.3.
| Environment | Issue |
|---|---|
| PS 5.1 | Not repro |
| PS 5.1 on PS 7.3 | Not repro |
| PS 5.1 on cmd on PS 5.1 | Not repro |
| PS 5.1 on cmd on PS 7.2.7 | Not repro |
| PS 5.1 on cmd on PS 7.3 | Repro |
Expected behavior
PS> cmd /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command Get-PSDrive
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Alias Alias
C 445.17 30.10 FileSystem C:\ ..._7.3.0.0_x64__8wekyb3d8bbwe
Cert Certificate \
D 906.38 25.13 FileSystem D:\
E 0.06 0.42 FileSystem E:\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
Variable Variable
WSMan WSManActual behavior
PS> cmd /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command Get-PSDrive
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Alias Alias
C 445.17 30.10 FileSystem C:\ ..._7.3.0.0_x64__8wekyb3d8bbwe
Cert Certificate \
D 906.38 25.13 FileSystem D:\
E 0.06 0.42 FileSystem E:\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
Variable Variable
WSMan WSManError details
No error is returned.Environment data
PS> $PSVersionTable
Name Value
---- -----
PSVersion 7.3.0
PSEdition Core
GitCommitId 7.3.0
OS Microsoft Windows 10.0.22623
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
CertPSDriveUnavailable.zip is a step recording capturing following steps.
- Launch PowerShell 7.3 with no profile.
- Run "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command Get-PSDrive" and get Cert PSDrive as expected.
- Run "cmd /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command Get-PSDrive" and get no Cert PSDrive.
Metadata
Metadata
Assignees
Labels
Resolution-AnsweredThe question is answered.
Activity
dwtaber commentedon Nov 12, 2022
It looks like this bug was introduced in 7.3.0-preview.7. More specifically, I believe it was introduced by #16355. Check this out:
Here's what I think is happening: PowerShell 7 modifies the value of
PSModulePathfor the special case of launching Windows PowerShell from PowerShell 7. In this scenario, however, PowerShell 7 is launchingcmd, notPowerShell.exe, so the special handling ofPSModulePathdoesn't happen, and Windows PowerShell launches with PowerShell 7 modules in itsPSModulePath. (What initially tipped me off was the surprise of seeing history-based prediction in Windows PowerShell.)Since the PowerShell 7 version of
Microsoft.PowerShell.Securityis the latest version available to it in this scenario, that's what Windows PowerShell tries to load. Until 7.3.0-preview.7, Windows PowerShell was able to load this PowerShell 7 module. Now, when it tries to import the PowerShell 7 version, it throws errors for the types that were moved into the module in #16355.In the meantime, as a workaround, if you're able to modify the scripts that started failing on you, have them explicitly import version 3.0.0.0 of
Microsoft.PowerShell.Securitybefore attempting to accesscert:\masaru-iritani commentedon Nov 14, 2022
Thank you for the information, dwtaber. Importing Microsoft.PowerShell.Security 3.0.0.0 explicitly mitigated the error.
SteveL-MSFT commentedon Nov 23, 2022
When we approved the change to move the types out of SMA, we didn't anticipate the impact on the
cert:drive that multiple users have reported. Committee will discuss this after the long weekend.daxian-dbw commentedon Nov 23, 2022
The root problem is that when doing
pwsh -> cmd -> powershell, the$env:PSModulePathin thepowershellprocess will be incorrectly using the module path frompwsh. This is because thecmdderives that environment value frompwsh, and thenpowershellderives that fromcmd. Below is a screenshot of the value of that env variable from withinpowershell.With the incorrect module path value, when doing
Import-Module Microsoft.PowerShell.Securityinpowershell, it’s actually attempting to import thepwshversion of theMicrosoft.PowerShell.Securitymodule, not the Windows PowerShell version that comes in-box withpowershell.exe.Doing the
pwsh -> cmd -> powershellis doomed to run into problems because the module paths are just wrong forpowershell.exe-- even if we reverted this change, there will be another change in future that just make thepwshversion of a built-in module no longer work inpowershell.exe.That’s why we special-cased the
pwsh -> powershell.exescenario to fix thePSModulePathenvironment variable before startingpowershell.exe.Masaru Iritani (@masaru-iritani) In your workflow, I suggest you unset the environment variable
PSModulePathincmdbyset PSModulePath=before startingpowershell.exe, so that thepowershell.exeprocess will be guaranteed to use the correct module path.SteveL-MSFT commentedon Nov 28, 2022
Ok, what Dongbo Wang (@daxian-dbw) says makes sense to me based on how this works and "supposed" to work.
cmdbeing in the middle will not hit the code paths that explicitly fixes upPSModulePathbut unsetting it will cause eitherpwshorpowershellto create it.zurcher commentedon Dec 14, 2022
Copying my comment over from a duplicate bug:
I'm hitting this bug in a long-term product support codebase and probably can't check in the workaround. I also suspect we'll see more reports of this issue as more people move to 7.3, and the error message gives no easy indication of what the problem is.
If
pwshandpowershellalready know how to rebuildPSModulePath, could thepwsh -> powershell.exespecial-case be applied to all executable calls?eliassal commentedon Dec 26, 2022
I had azure pipeline that uses powershell inline script which runs on an self-hosted build agent where I have powershell standard and Ps7, Pipeline used to work like a charm until I upgraded 2 weeks back PS7 to 7.3

Script started to fail with the following error
I opened a ticket with VS community at
https://developercommunity.visualstudio.com/t/dotnet-coverage-not-showing-analysis-onl/10177746#T-ND10238787
They pointed me to this ticket
As you will notice in PS command line it calls the right 3.0.0.0 version and in PS7 as well it calls its correct version and script on both works fine
I issued set "PSModulePath=" in PS command line even I added
import-module -Name 'Microsoft.PowerShell.Security' -RequiredVersion 3.0.0.0in the inline script in the pipeline but nothing helped
So not sure what is needed to be doen in order to make the script in the pipeline work correctly
Attempt to fix build: fix import PWS import module
Update sign.py: set PSModulePath
debug sign build (#158)
- Reset `PSModulePath` environment variable in the ext to handle pwsh…
fix(smoke-test,windows): clear PSModulePath before launching powershe…
Stage the VC++ runtime from a PowerShell 7 shell on Windows
chore(release): v10.3.5 - PSModulePath repair for 5.1 under pwsh laun…
Do not pass PowerShell 7's module path to the Windows installer