如何在PowerShell Core(pwsh)中查找或安装缺少的命令?
我正在使用多个版本的PowerShell,但只有2个版本可以找到所有标准命令(或者是Commandlet?) 最初安装的是Windows PowerShell v5.1,但我也安装了(如何在PowerShell Core(pwsh)中查找或安装缺少的命令?,powershell,repository,powershell-core,Powershell,Repository,Powershell Core,我正在使用多个版本的PowerShell,但只有2个版本可以找到所有标准命令(或者是Commandlet?) 最初安装的是Windows PowerShell v5.1,但我也安装了(pwsh.exe)v6.1.1 问题是我试图在PowerShell Core中运行一些与防火墙相关的东西,但是找不到Get-NetFirewallProfile命令 Get-NetFirewallProfile -Profile Domain, Public, Private | Select-Object Nam
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\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
万一有人想知道,模块中所有可用的命令都可以用:
(获取模块-列出可用的NetSecurity)。导出命令
更新:
我使用以下命令/comdlet找到了正确的模块:
(Get-Module -ListAvailable -SkipEditionCheck *).ExportedCommands.Values |select CommandType,Source,Version,Name | sort Name
(或者将ExportedCommands
替换为exportedcmdlet
)核心不支持NetSecurity。但是,如果您使用的是Windows操作系统,则可以使用Param-SkipEditionCheck
Import-Module NetSecurity -SkipEditionCheck
您可以在Get模块上使用相同的参数
Get-Module NetSecurity -ListAvailable -SkipEditionCheck
在这种情况下有效,但需要注意的是,-SkipEditionCheck
明确绕过了给定模块自己的[缺少]声明,即它使用的PowerShell版本是什么:(Desktop
(Windows PowerShell)和/或Core
(PowerShell Core))
一般来说,你不能指望这会奏效
在撰写本文时,旧模块(仅在Windows PowerShell存在时创建)正在评估PowerShell核心兼容性,如果存在,将通过新的CompatiblePreditions
模块清单条目标记为旧模块
以前的模块总是只为Windows PowerShell创建,并且缺少兼容的ePSEditions
声明(默认情况下会导致PowerShell Core忽略它们),也可以在PowerShell Core中工作,但前提是实现了这些模块:
- 仅使用PowerShell代码(
*.psm1
包含类似cmdlet的高级函数的文件)
- 和/或通过
,NetSecurity
模块李>
值得注意的是,这排除了包含cmdlet的模块,这些cmdlet总是作为.NET程序集的一部分(总是编译的),和/或包含辅助DLL(程序集)
如果您不确定给定旧模块的兼容性(尚未明确说明它与哪个版本兼容),您可以对-SkipEditionCheck
使用试错法(尽管检查模块的实现——它是否使用.NET程序集?——会让您事先有一个良好的感觉)。
然而,考虑到包含.NET程序集的模块总体上更为典型,我不希望有很多旧模块是兼容的
相反,如果模块确实有一个CompatiblePSEditions
条目,并且指示运行的版本不受支持,则可以安全地假设它将不工作。
专门用于跟踪框内模块的问题,一旦这些模块被标记为交叉版,并可能被修改为交叉版。,这是截至本文撰写之时的一个持续过程。
(对于第三方模块,由其维护人员以这种方式进行更新。)
但是,只有使用最新的Windows 10版本(更新频道)和更新,您才能看到这些努力的成果
在旧版本(包括所有返回Windows 7的版本)上,您仍然可以使用-SkipEditionCheck
加载已测试并发现与PowerShell Core隐式兼容的旧模块
如果/一旦您知道给定模块在PowerShell Core中不工作:
您有两个选择:
- 使用模块,该模块使用隐式远程处理,在最简单的情况下,通过与同一台计算机上的Windows PowerShell通信,使PowerShell Core中仅Windows PowerShell cmdlet可用,但您甚至可以远程定位一台计算机(在这种情况下,命令将在那里运行);安装模块后,使用
Import WinModule
导入给定的仅Windows PowerShell模块的[代理函数]
- 请注意,虽然隐式远程处理可以按预期工作,但它确实有其局限性,并且比进程内执行慢
- 一个显著的限制是涉及序列化和反序列化,这意味着只有有限数量的类型可以使用类型保真度反序列化,而所有其他类型都是使用静态属性反序列化为原始类型的近似值
- 即席:通过其CLI调用Windows PowerShell,
PowerShell.exe
,通过脚本块传递任意命令({…}
)-请参见下面的示例。
这与通过兼容性模块使用隐式远程处理具有类似的限制和警告。
此外,由于每次调用都会创建一个新的Windows PowerShell进程,因此每次启动该进程并导入感兴趣的模块都会产生开销;如果需要运行多个命令,最好将它们捆绑起来
看起来从Windows10版本1809开始,一些缺少的命令又回到了PS6中,比如“GetNetad”
# 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 }