我使用的是 WINDOWS 10 并使用 POWERSHELL 7.4.2。
我的脚本将每个模块脚本 (.psm1) 的文档文件写入为 Markdown 文件 (.md)。我可以使用 Get-Help 访问每个 commentHelp 关键字,但输出和注释除外。
这就是我访问模块文件的 commentHelp 部分的方式:
# extract AST from source-file
$tokens = §null
$errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($sourceFile, [ref]$tokens, [ref]$errors)
# get data of all functions in file
$functionDefinitions = $ast.FindAll( {
param([System.Management.Automation.Language.Ast] $ast)
$ast -is [System.Management.Automation.Language.FunctionDefinitionAst] -and ($PSVersionTable.PSVersion.Major -lt 5 -or $ast.Parent -isnot [System.Management.Automation.Language.FunctionMemberAst])
}, $true)
我可以通过 Get-Help 命令访问每个函数的 commentHelp 中的关键字(.SYNOPSIS、.DESCRIPTION、.PARAMETER、.EXAMPLE ...)
$functionDescription = (Get-Help -Name $functionDefinition.Name).Description
其中 $functionDefinition.Name 是具有单个函数的 commentHelp 内容的对象。
$examples = Get-Help -Name $FunctionDefinition.Name -Examples
为我提供了包含所有示例的对象。
使用
查看commentHelp对象$functionDescription | format-list
正确显示所有commentHelp关键字及其值。 OUTPUTS、NOTES 和 ROLE 的值以字符串形式出现。但是 OUTPUTS 和 Notes 的 Get-Help 命令提供空值,ROLE 工作正常。
解析commentHelp时没有执行错误,只是没有OUTPUTS和NOTES的值。
输出和注释有什么特别之处吗?有没有人有办法解决这个问题?
平安并保持健康
我也研究过同样的问题。这是我的解决方案。
Notes 实际上位于
alertSet
属性中,而 Output 位于 returnValues
属性中。它们也可以是数组,有时数组中的第一项是一个空字符串,如果您不迭代数组,PowerShell 会自动为您显示该空字符串,因此,该属性中可能没有数据。
这是我的实现:
不要问我为什么要这样写模块。这是一个阶段。