如何找到给定cmdlet的模块?

use*_*792 18 powershell

如何确定给定cmdlet的模块,以便从覆盖cmdlet的函数直接调用?

例如,我怎么能发现Start-Transcript存在于Microsoft.Powershell.Host?

Get-Module Start-Transcript
Run Code Online (Sandbox Code Playgroud)

不产生任何东西


更新以下答案.

这是输出:

PS C:\Windows> Get-Command -type cmdlet start-transcript | fl *

HelpUri             : http://go.microsoft.com/fwlink/?LinkID=113408
DLL                 : C:\Windows\assembly\GAC_MSIL\Microsoft.PowerShell.ConsoleHost\1.0.0.0__31bf3856ad364e35\Microsoft
                      .PowerShell.ConsoleHost.dll
Verb                : Start
Noun                : Transcript
HelpFile            : Microsoft.PowerShell.ConsoleHost.dll-Help.xml
PSSnapIn            : Microsoft.PowerShell.Host
ImplementingType    : Microsoft.PowerShell.Commands.StartTranscriptCommand
Definition          : Start-Transcript [[-Path] <String>] [-Append] [-Force] [-NoClobber] [-Verbose] [-Debug] [-ErrorAc
                      tion <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningV
                      ariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]

DefaultParameterSet :
OutputType          : {}
Name                : Start-Transcript
CommandType         : Cmdlet
Visibility          : Public
ModuleName          : Microsoft.PowerShell.Host <------------ HERE IT IS
Module              :
Parameters          : {[Path, System.Management.Automation.ParameterMetadata], [Append, System.Management.Automation.Pa
                      rameterMetadata], [Force, System.Management.Automation.ParameterMetadata], [NoClobber, System.Man
                      agement.Automation.ParameterMetadata]...}
ParameterSets       : {[[-Path] <String>] [-Append] [-Force] [-NoClobber] [-Verbose] [-Debug] [-ErrorAction <ActionPref
                      erence>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>
                      ] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]}
Run Code Online (Sandbox Code Playgroud)

CB.*_*CB. 13

使用

Get-Command Start-Transcript | fl *
Run Code Online (Sandbox Code Playgroud)

查找有关该命令的信息.

  • 一个更简洁的版本:`(Get-Command Start-Transcript).ModuleName` (7认同)
  • 这更有效:get-command -type cmdlet start-transcript | 选择模块名称 (2认同)

Pat*_*ryk 8

PowerShell中有一些选项.为了将结果缩小到您正在寻找的特定信息 - 可以使用以下方法之一:

(Get-Command -Name Start-Transcript).ModuleName
Run Code Online (Sandbox Code Playgroud)

要么

Get-Command -Name Start-Transcript | Select-Object -Property ModuleName
Run Code Online (Sandbox Code Playgroud)

要么

Get-Command -Name Start-Transcript | Format-List -Property ModuleName
Run Code Online (Sandbox Code Playgroud)

注意:

在PowerShell脚本中使用或使用自定义PowerShell模块时,通常认为使用完整的cmdlet名称instaed of alias(如fl,ft,select等)是一种很好的做法.它提高了代码的可读性.

  • 其中一项原则规定代码应该是自记录的,请参阅:http://shorturl.at/cC289 别名违背了这一目的。在脚本中使用它们通常是一个坏主意,它们是设计好的并且应该主要用于临时命令。有人可能会争辩说,只要经验丰富的用户保持一致的脚本编写习惯,他们就可能会接受内置别名。然而,再次共享这样的代码会使其变得不那么清晰并且更难以被社区理解。 (3认同)
  • 如今,有了像 VSCode 这样具有自动完成功能的优秀编辑器,内置了 IntelliSense 功能,输入完整的 cmdlet 名称不再是问题,并且通常被认为是 PowerShell 世界中的最佳实践。 (2认同)