Windows PowerShell Compatibility

背景

Win11 21H2(OS Build 22000.2124)

Windows PowerShell 7.3.5

简述

在试用 PowerShell 7.x 时,部分命令仍需 PS 5.1 .

具体来说,是指 %windir%\system32\WindowsPowerShell\v1.0\Modules 下的 Module[2],例如 Appx 的 Get-AppxPackage.

Unless the module manifest indicates that module is compatible with PowerShell Core, modules in the %windir%\system32\WindowsPowerShell\v1.0\Modules folder are loaded in a background Windows PowerShell 5.1 process by Windows PowerShell Compatibility feature.

表现

~ $ Get-AppxPackage
Get-AppxPackage: The 'Get-AppxPackage' command was found in the module 'Appx', but the module could not be loaded. 
                 For more information, run 'Import-Module Appx'.

禁用 PS 5.1 的情况下,Import-Module appx 无效。

启用 PS 5.1,则会启用 WinPSCompatSession remoting session.

~ $ Import-Module appx
WARNING: Module Appx is loaded in Windows PowerShell using WinPSCompatSession remoting session; 
         please note that all input and output of commands from this module will be deserialized objects. 
         If you want to load this module into PowerShell please use 'Import-Module -SkipEditionCheck' syntax.

验证 WinPSCompatSession remoting session.

$s = Get-PSSession -Name WinPSCompatSession
Invoke-Command -Session $s -ScriptBlock {
  "Running in Windows PowerShell version $($PSVersionTable.PSVersion)"
}

TEMP 目录

  • remoteIpMoProxy_Appx_2.0.1.0_localhost_ed4a3308-b75a-4eb4-919f-90167dba6ac4

禁用隐式兼容层,DisableImplicitWinCompat

~ $ Import-Module appx
Import-Module: Module 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\appx\appx.psd1' supports PowerShell edition 'Desktop' 
               and cannot be loaded implicitly using the Windows Compatibility feature because it is disabled in the settings file. 
               Use 'Import-Module -UseWindowsPowerShell' to load this module with Windows PowerShell or 'Import-Module -SkipEditionCheck' 
               to try to load the module with the current PowerShell.

更多

参考

附录

Improved backwards compatibility with Windows PowerShell

For Windows, a new switch parameter UseWindowsPowerShell (aliases: UseWinPS) is added to Import-Module. This switch creates a proxy module in PowerShell 7 that uses a local Windows PowerShell process to implicitly run any cmdlets contained in that module. For more information, see Import-Module.

Cmdlet changes: Check system32 for compatible built-in modules on Windows

In the Windows 10 1809 update and Windows Server 2019, we updated a number of built-in PowerShell modules to mark them as compatible with PowerShell.

When PowerShell starts up, it automatically includes $windir\System32 as part of the PSModulePath environment variable. However, it only exposes modules to Get-Module and Import-Module if its CompatiblePSEdition is marked as compatible with Core.

You can override this behavior to show all modules using the -SkipEditionCheck switch parameter. We've also added a PSEdition property to the table output.

Compatible modules

Get-Module -ListAvailable

Beginning in PowerShell 6, the CompatiblePSEditions value is used to decide if a module is compatible when modules are imported from $env:windir\System32\WindowsPowerShell\v1.0\Modules. This behavior only applies to Windows. Outside of this scenario, the value is only used as metadata.

posted @ 2023-06-29 15:51  UPeRVv  阅读(106)  评论(0编辑  收藏  举报