如何在 PowerShell Core (pwsh) 中查找或安装缺少的命令?

How to find or install missing Commands in PowerShell Core (pwsh)?

我正在使用多个版本的 PowerShell,但只有 2 个版本可以找到所有标准命令(或者是 commandlets?)。

原来安装的是WindowsPowerShellv5.1,后来我也安装了PowerShell Core(pwsh.exe)v6.1.1 .

问题是我正在尝试 运行 PowerShell Core 中的一些防火墙相关内容,但是找不到 Get-NetFirewallProfile 命令。

Get-NetFirewallProfile -Profile Domain, Public, Private | Select-Object Name, Enabled

但是,运行在 Windows PowerShell 中就可以了,因为所需的模块 NetSecurity 在那里可用。

如何让 PowerShell Core 找到已经存在的模块或重新安装它们?
(它们甚至兼容吗?-如果不兼容,如何更新它们?)


附加信息

PowerShell Core v6.1 我只有:

$ Get-Module -ListAvailable

    Directory: C:\Program Files\PowerShell\Modules

ModuleType Version    Name                                PSEdition ExportedCommands
---------- -------    ----                                --------- ----------------
Script     1.8.1      PSVersion                           Desk      {Get-PSVersion, Update-P
Binary     2.1.0.1    PSWindowsUpdate                     Desk      {Add-WUServiceManager, E

    Directory: C:\program files\powershell\Modules

ModuleType Version    Name                                PSEdition ExportedCommands
---------- -------    ----                                --------- ----------------
Manifest   6.1.0.0    CimCmdlets                          Core      {Get-CimAssociatedInstan
Manifest   1.2.2.0    Microsoft.PowerShell.Archive        Desk      {Compress-Archive, Expan
Manifest   6.1.0.0    Microsoft.PowerShell.Diagnostics    Core      {Get-WinEvent, New-WinEv
Manifest   6.1.0.0    Microsoft.PowerShell.Host           Core      {Start-Transcript, Stop-
Manifest   6.1.0.0    Microsoft.PowerShell.Management     Core      {Add-Content, Clear-Cont
Manifest   6.1.0.0    Microsoft.PowerShell.Security       Core      {Get-Acl, Set-Acl, Get-P
Manifest   6.1.0.0    Microsoft.PowerShell.Utility        Core      {Format-List, Format-Cus
Manifest   6.1.0.0    Microsoft.WSMan.Management          Core      {Disable-WSManCredSSP, E
Script     1.1.7.2    PackageManagement                   Desk      {Find-Package, Get-Packa
Script     1.6.7      PowerShellGet                       Desk      {Find-Command, Find-DSCR
Script     0.0        PSDesiredStateConfiguration         Desk      {GetSyntax, Write-MetaCo
Script     6.1.0.0    PSDiagnostics                       Core      {Disable-PSTrace, Disabl
Script     2.0.0      PSReadLine                          Desk      {Get-PSReadLineKeyHandle
Binary     1.1.2      ThreadJob                           Desk      Start-ThreadJob

而在 Windows PowerShell v5.1 中,我有:

$ Get-Module -ListAvailable *

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     1.0.0.1    PackageManagement                   {Find-Package, Get
Script     1.0.0.1    PowerShellGet                       {Install-Module, F
Script     1.8.1      PSVersion                           {Get-PSVersion, Up

    Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.0.0    AppBackgroundTask                   {Disable-AppBackgr
Manifest   2.0.0.0    Appx                                {Add-AppxPackage,
Manifest   1.0.0.0    BitLocker                           {Unlock-BitLocker,
Manifest   1.0.0.0    BitsTransfer                        {Add-BitsFile, Com
Manifest   1.0.0.0    CimCmdlets                          {Get-CimAssociated
Manifest   1.0        Defender                            {Get-MpPreference,
Manifest   1.0.0.0    DirectAccessClientComponents        {Disable-DAManualE
Script     3.0        Dism                                {Add-AppxProvision
Manifest   1.0.0.0    DnsClient                           {Resolve-DnsName,
Manifest   2.0.0.0    International                       {Get-WinDefaultInp
Manifest   1.0.0.0    iSCSI                               {Get-IscsiTargetPo
Script     1.0.0.0    ISE                                 {New-IseSnippet, I
Manifest   1.0.0.0    Kds                                 {Add-KdsRootKey, G
Manifest   1.0.1.0    Microsoft.PowerShell.Archive        {Compress-Archive,
Manifest   3.0.0.0    Microsoft.PowerShell.Diagnostics    {Get-WinEvent, Get
Manifest   3.0.0.0    Microsoft.PowerShell.Host           {Start-Transcript,
Manifest   1.0.0.0    Microsoft.PowerShell.LocalAccounts  {Add-LocalGroupMem
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clea
Script     1.0        Microsoft.PowerShell.ODataUtils     Export-ODataEndpoi
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {Get-Acl, Set-Acl,
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Format-List, Form
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Disable-WSManCred
Manifest   1.0        MMAgent                             {Disable-MMAgent,
Manifest   1.0.0.0    MsDtc                               {New-DtcDiagnostic
Manifest   2.0.0.0    NetAdapter                          {Disable-NetAdapte
Manifest   1.0.0.0    NetConnection                       {Get-NetConnection
Manifest   1.0.0.0    NetEventPacketCapture               {New-NetEventSessi
Manifest   2.0.0.0    NetLbfo                             {Add-NetLbfoTeamMe
Manifest   1.0.0.0    NetNat                              {Get-NetNat, Get-N
Manifest   2.0.0.0    NetQos                              {Get-NetQosPolicy,
Manifest   2.0.0.0    NetSecurity                         {Get-DAPolicyChang
Manifest   1.0.0.0    NetSwitchTeam                       {New-NetSwitchTeam
Manifest   1.0.0.0    NetTCPIP                            {Get-NetIPAddress,
Manifest   1.0.0.0    NetworkConnectivityStatus           {Get-DAConnectionS
Manifest   1.0.0.0    NetworkSwitchManager                {Disable-NetworkSw
Manifest   1.0.0.0    NetworkTransition                   {Add-NetIPHttpsCer
Manifest   1.0.0.0    PcsvDevice                          {Get-PcsvDevice, S
Manifest   1.0.0.0    PKI                                 {Add-CertificateEn
Manifest   1.1        PrintManagement                     {Add-Printer, Add-
Manifest   1.1        PSDesiredStateConfiguration         {Set-DscLocalConfi
Script     1.0.0.0    PSDiagnostics                       {Disable-PSTrace,
Binary     1.1.0.0    PSScheduledJob                      {New-JobTrigger, A
Manifest   1.5.2.6    PSWindowsUpdate                     {Add-WUOfflineSync
Manifest   2.0.0.0    PSWorkflow                          {New-PSWorkflowExe
Manifest   1.0.0.0    PSWorkflowUtility                   Invoke-AsWorkflow
Manifest   1.0.0.0    ScheduledTasks                      {Get-ScheduledTask
Manifest   2.0.0.0    SecureBoot                          {Confirm-SecureBoo
Manifest   2.0.0.0    SmbShare                            {Get-SmbShare, Rem
Manifest   2.0.0.0    SmbWitness                          {Get-SmbWitnessCli
Manifest   1.0.0.0    StartScreen                         {Export-StartLayou
Manifest   2.0.0.0    Storage                             {Add-InitiatorIdTo
Manifest   2.0.0.0    TLS                                 {New-TlsSessionTic
Manifest   1.0.0.0    TroubleshootingPack                 {Get-Troubleshooti
Manifest   2.0.0.0    TrustedPlatformModule               {Get-Tpm, Initiali
Manifest   2.0.0.0    VpnClient                           {Add-VpnConnection
Manifest   1.0.0.0    Wdac                                {Get-OdbcDriver, S
Manifest   1.0.0.0    WindowsDeveloperLicense             {Get-WindowsDevelo
Script     1.0        WindowsErrorReporting               {Enable-WindowsErr
Manifest   1.0.0.0    WindowsSearch                       {Get-WindowsSearch

为了以防万一有人想知道,模块中所有可用的命令都可以列出:
(Get-Module -ListAvailable NetSecurity).ExportedCommands

更新:

我设法找到了正确的模块,给定一个 command/comdlet,使用这个:

(Get-Module -ListAvailable -SkipEditionCheck *).ExportedCommands.Values |select CommandType,Source,Version,Name | sort Name

(或者将 ExportedCommands 替换为 ExportedCmdlets。)

Core 不支持 NetSecurity。如果您在 Windows OS 上,您可以使用参数 -SkipEditionCheck

Import-Module NetSecurity -SkipEditionCheck

您可以在 Get-Module

上使用相同的参数
Get-Module NetSecurity -ListAvailable -SkipEditionCheck

适用于 这种 情况,但重要的是要注意 -SkipEditionCheck 显式 绕过 给定模块自己的[缺少]声明它与版本一起工作的PowerShell: (Desktop (Windows PowerShell) and/or Core (PowerShell Core)).

你不能通常期望它起作用

在撰写本文时,较旧的模块 - 在只有 Windows PowerShell 存在的时候创建 - 正在 已评估 用于 PowerShell Core 兼容性,如果是,将通过新的 CompatiblePSEditions 模块清单条目.

较旧的模块 - 总是 - 只为 Windows PowerShell 创建并且 缺少 CompatiblePSEditions 声明(这会导致 PowerShell Core 默认忽略它们),可能 也适用于 PowerShell Core,但是 仅当 它们被实施时:

  • 仅使用 PowerShell 代码*.psm1 包含 高级函数的文件像 cmdlet)
  • and/or 通过 CDXML,与 NetSecurity 模块的情况一样。

值得注意的是,这 不包括包含 cmdlets 的模块,它们总是作为(总是 compiled) .NET 程序集, and/or 包含辅助 DLL(程序集)。

如果您不确定给定旧模块的兼容性(尚未明确(尚未)与哪些版本兼容),您可以将 试错 -SkipEditionCheck 一起使用(尽管检查模块的实现 - 它是否使用 .NET 程序集? - 事先会给你一个很好的感觉) .
但是,鉴于包含 .NET 程序集的模块总体上更典型,我不希望许多旧模块兼容。

相反,如果模块 确实 有一个 CompatiblePSEditions 条目并表明 运行ning 版本是 支持,可以安全地假设它工作。

PowerShellModuleCoverage GitHub repository 专用于跟踪 盒内 模块的问题 一旦它们被标记为交叉版本 可能为此目的进行了修改 ,在撰写本文时这是一个持续的过程。
(对于 第三方模块 ,由其维护者以这种方式更新它们。)

但是,只有使用最新的 Windows 10 版本(更新频道)和更新,您才能看到这些努力的成果。

在旧版本上,包括一直回到 Windows 7,您仍然可以使用 -SkipEditionCheck 加载您测试过并发现 隐含的旧模块 与 PowerShell Core 兼容。


如果/一旦您知道给定模块不能在PowerShell中工作Core:

您有两个选择:

  • 使用 WindowsCompatibility 模块,它使用 隐式远程处理 使 Windows PowerShell Core 中可用的仅限 PowerShell 的 cmdlet,在最简单的情况下,通过与 相同 机器上的 Windows PowerShell 通信,但您甚至可以远程瞄准机器 (在这种情况下,命令将 运行 那里 );安装模块后,使用 Import-WinModule 为给定的 Windows PowerShell-only 模块导入[代理函数]。

    • 请注意,虽然隐式远程处理可能按预期工作,但它确实有其局限性,并且比进程内执行
    • 一个值得注意的限制是涉及序列化和反序列化,这意味着只有有限数量的类型可以以类型保真度反序列化,而所有其他类型都被反序列化为近似值原始类型,具有静态属性。
  • 临时:通过其 CLI powershell.exe 调用 Windows PowerShell,传递 任意命令通过 脚本块 ({ ... }) - 请参见下面的示例。
    这与通过兼容性模块使用隐式远程处理有类似的限制和注意事项。 此外,由于每次调用都会创建一个新的 Windows PowerShell 进程,因此每次都会产生启动它并导入感兴趣的模块的开销;如果你需要 运行 多个 命令,最好将它们捆绑在一起。

# Call the Windows PowerShell CLI from PowerShell Core, using a script block.
# (Add -noprofile to suppress $PROFILE loading.)
powershell { Get-NetFirewallProfile -Profile Domain, Public, Private | Select-Object Name, Enabled }

看起来自 Windows 10 版本 1809 以来,其中一些缺失的命令又回到了 PS 6,例如 "get-netadapter" 和 "get-netfirewallrule"。事实上还有 47 个模块可用,主要是 "manifest" 类型。