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

标签 powershell repository powershell-core

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

原始安装是 Windows PowerShell v5.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 核心 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 。)

最佳答案

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

Import-Module NetSecurity -SkipEditionCheck

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

关于powershell - 如何在 PowerShell Core (pwsh) 中查找或安装缺少的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54508011/

相关文章:

c# - 使用 Powershell Core 进行多线程在线交换 - C#

powershell - powershell、pwsh 和 powershell_ise 之间奇怪的[类型]行为差异

powershell - 批量编辑文件创建日期

powershell - nltest 输出对于与 new-object 一起使用无效吗?

Git用户权限

android - 为什么 Maven 存储库中没有 Android SDK 4.4?

GitHub:如何删除自己作为另一个人的资料库的贡献者

powershell - 如何将.xml文件复制到zip文件夹?

powershell - 如何在 Powershell 中为 Format-Table 创建别名?

powershell - 即使发生异常,如何确保可步进管道使用的文件也已关闭?