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

时间:2019-02-03 21:59:15

标签: powershell repository powershell-core pwsh

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

原始安装是 Windows PowerShell v5.1,但随后我还安装了PowerShell Corepwsh.exe)v6.1.1。

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

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

但是,由于所需的模块NetSecurity在Windows PowerShell中可用,因此它在Windows PowerShell中运行得很好。

如何使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\6\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

更新

我设法使用以下命令找到了正确的模块(给定了命令/ comdlet):

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

(或者将ExportedCommands替换为ExportedCmdlets。)

3 个答案:

答案 0 :(得分:2)

Core不支持NetSecurity。如果您使用的是Windows操作系统,则可以使用参数-SkipEditionCheck

Import-Module NetSecurity -SkipEditionCheck

您可以在Get-Module上使用相同的参数

Get-Module NetSecurity -ListAvailable -SkipEditionCheck

答案 1 :(得分:2)

ArcSet's helpful answer this 情况下有效,但需要注意的是, -SkipEditionCheck明确绕过给定模块自己的[缺少]声明适用的PowerShell 版本 :(Desktop(Windows PowerShell)和/或Core(PowerShell Core ) )。

您通常不能 期望它起作用

在撰写本文时,仅在存在 Windows PowerShell 时创建的旧模块-正在对PowerShell 进行评估的过程核心兼容性(如果有),则将通过新的 CompatiblePSEditions模块清单条目进行标记。

较旧的模块-始终都是为Windows PowerShell而创建的,并且 lack CompatiblePSEditions声明(这会导致PowerShell Core)默认情况下忽略它们), **也许在PowerShell Core中也可以使用,但是仅在已实现的情况下:

  • 仅使用 PowerShell代码 *.psm1个包含高级功能的命令类似于cmdlet的文件)
  • 和/或通过CDXML ,例如NetSecurity模块。

值得注意的是,此排除了包含 cmdlet 的模块,这些模块始终作为 .NET 程序集 ,和/或包含辅助DLL(程序集)。

如果不确定给定的旧模块的兼容性(尚未明确(尚未)与哪个版本兼容),您可以使用 trial -SkipEditionCheck 中出现错误和错误(尽管检查模块的实现-它是否使用.NET程序集?-可以使您事前有所了解)。
但是,考虑到总体上来说,包括.NET程序集的模块是更典型的模块,因此我不希望许多旧模块兼容。

相反,如果模块 有一个CompatiblePSEditions条目,并表示不支持运行版本 ,则可以安全地假定它将起作用。

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

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

在较旧的版本(包括从Windows 7开始一直使用),您仍然可以使用-SkipEditionCheck加载经过测试并发现与Powershell Core隐式兼容的较旧模块。


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

您有两个选择:

  • 使用WindowsCompatibility模块,该模块使用隐式远程处理使仅Windows PowerShell的cmdlet在PowerShell Core中可用,在最简单的情况下,与相同机器上的Windows PowerShell进行通信,但您甚至可以远程 锁定机器(在这种情况下,命令将运行 there )。 br /> 请注意,虽然隐式远程处理通常按预期工作,但它确实有其局限性,并且比进程内执行要慢

  • 临时:通过其CLI powershell.exe 调用Windows PowerShell,并通过 script块传递任意命令{ ... })-请参见下面的示例。
    就像通过兼容性模块进行隐式远程处理一样,它使用了远程处理基础结构,并具有相同的限制和警告。
    另外,由于每次调用都会创建一个新的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 }

答案 2 :(得分:0)

从Windows 10版本1809开始,这些丢失的命令中的一些已恢复到PS 6中,例如“ get-netadapter”和“ get-netfirewallrule”。实际上,还有47个可用模块,主要是“清单”类型。