简体   繁体   English

如何找到 PowerShell cmdlet 的模块?

[英]How do I find the module for a PowerShell cmdlet?

I'm trying to setup a VM with Hyper-V.我正在尝试使用 Hyper-V 设置 VM。 I need to run get-vmserver and some related comdlets, but I can't find what module they are in. When I run:我需要运行 get-vmserver 和一些相关的 comdlet,但我找不到它们所在的模块。当我运行时:

(get-command -name start-transcript).modulename

I get a response我得到回应

Microsoft.Powershell.Host

But when I run但是当我跑步时

(get-command -name get-vm).modulename

I get the response that get-vm is not recognized.我得到 get-vm 无法识别的响应。

I believe this means I must run import-module with some module name, but I can't find a list of "standard" cmdlets and their modules.我相信这意味着我必须使用某些模块名称运行 import-module,但我找不到“标准”cmdlet 及其模块的列表。

That means that PowerShell is not aware of any command called Get-VM .这意味着 PowerShell 不知道任何名为Get-VM的命令。

For a command to be recognized, it must either:要识别一个命令,它必须:

  • Be defined in the session.在会话中定义。
  • Be defined in a loaded module.在加载的模块中定义。
  • Be defined in a module that meets the criteria for auto-loading在满足自动加载标准的模块中定义

Microsoft's modules tend to be well-formed and support auto-loading, so if it were installed, it would be working.微软的模块往往格式正确并支持自动加载,所以如果安装了它,它就会工作。

Therefore I strongly suspect you did not install the Hyper-V module (which is where Get-VM is from).因此,我强烈怀疑您没有安装Hyper-V模块(这是Get-VM的来源)。

To see modules that are installed but not necessarily imported, use Get-Module -ListAvailable .要查看已安装但不一定要导入的模块,请使用Get-Module -ListAvailable

But note that if the module is listed there, and is well-formed, the commands would be available in Get-Command as well.但请注意,如果该模块在此处列出并且格式正确,则这些命令也可以在Get-Command中使用。

Demo演示

Look at a module listed in Get-Module -ListAvailable but not listed if you explicitly call Get-Command -Name .查看Get-Module -ListAvailable中列出的模块,但如果您明确调用Get-Command -Name则未列出。 On my system, I'll use VpnClient .在我的系统上,我将使用VpnClient

From the output of Get-Module -ListAvailable I can see it has a command Add-VpnConnection .Get-Module -ListAvailable的输出中,我可以看到它有一个命令Add-VpnConnection

If I call Get-Command -Name Add-VpnConnection , the call works, and it shows the source of the command as VpnClient .如果我调用Get-Command -Name Add-VpnConnection ,则该调用有效,并且它将命令的源显示为VpnClient

Also, if I now call Get-Module -Name VpnClient , it will show up whereas before it didn't.另外,如果我现在调用Get-Module -Name VpnClient ,它会显示出来,而之前没有。

This is because the module was implicitly loaded when Get-Command referenced it.这是因为模块在Get-Command引用它时被隐式加载。

You are correct in that you must run Import-Module and select the Hyper-V module.您是正确的,您必须运行 Import-Module 并选择 Hyper-V 模块。 Make sure that you have installed the Hyper-V feature on your Windows Server.确保您已在 Windows Server 上安装了 Hyper-V 功能。 This can be performed in server manager.这可以在服务器管理器中执行。

Try:尝试:

Find-Module -Command YourCmdletName

Find-Module can find modules that you may not yet have installed, whilst Get-Command only checks modules that you have already installed. Find-Module可以找到您可能尚未安装的模块,而Get-Command仅检查您已经安装的模块。

Find-Module checks all registered repositories, by default, only PSGallery. Find-Module检查所有已注册的存储库,默认情况下仅检查 PSGallery。 You see the list of repositories that Find-Module searches with:您会看到Find-Module搜索的存储库列表:

Get-PSRepository

You can register additional PowerShell repositories with您可以使用以下方式注册其他 PowerShell 存储库

Register-PSRepository

Once the desired module is identified, you can install it with the Install-Module command:确定所需模块后,您可以使用Install-Module命令安装它:

Install-Module ModuleName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM