【发布时间】:2016-11-04 10:06:14
【问题描述】:
有人可以提供 PowerShell 脚本来从模块 (.psm1) 中获取所有功能的帮助消息。
使用Get-Content 我可以读取.psm1 文件的内容,但我无法仅获取模块中所有可用功能的帮助消息。
【问题讨论】:
标签: powershell
有人可以提供 PowerShell 脚本来从模块 (.psm1) 中获取所有功能的帮助消息。
使用Get-Content 我可以读取.psm1 文件的内容,但我无法仅获取模块中所有可用功能的帮助消息。
【问题讨论】:
标签: powershell
一个快速的google search 生成了这个方便的链接
PowerTip: Use PowerShell to Display Help for Module Cmdlets
在 Windows PowerShell 3.0 中更新您的帮助后,使用 Get-Command cmdlet 从特定模块检索所有 cmdlet, 将结果传送到 Foreach-Object cmdlet,并使用 Get-Help 脚本块内的 cmdlet。
以下是 PrintManagement 模块的示例:
Get-Command -Module PrintManagement | Foreach-Object {Get-Help $_.Name -Examples}这是同一命令的较短版本:
gcm -mo *print* | % {get-help $_.name -ex}
参考这个答案:How do I retrieve the available commands from a module?
如果尚未导入模块 (.psm1),您需要先加载它。然后你可以像上面的例子一样调用 cmdlet
Import-Module -Name <ModuleName>
Get-Command -Module <ModuleName> | Foreach-Object {Get-Help $_.Name -Examples}
【讨论】:
使用下面的 PowerShell 脚本,我可以在具有函数名称的单独文本文件中获取模块中每个函数的帮助消息。
$ModuleName = 'ModuleName' #Specify the Module
$path ="D:\Helpmessage\" #Specify Folder path for each functions help message
Import-Module $ModuleName -Force
$FunctionName = Get-Command -Module $ModuleName
$FunctionName = $FunctionName | where {$_.CommandType -eq 'Function'}
Foreach ($Function in $FunctionName.Name)
{
$result =
@("Function $Function `n {"
(get-command $Function).Definition
"}")
Invoke-expression "$result"
get-help $Function | out-file $Path\$Function.txt #Export help message for all the function
}
【讨论】: