简体   繁体   English

PowerShell GetHelp仅返回script.ps1文件的文件名

[英]PowerShell GetHelp only returning file name of script.ps1 file

I must be missing something simple here. 我一定在这里缺少一些简单的东西。

I have implemented some help comments in one of my functions in my ps1 file : 我已经在ps1文件的其中一个函数中实现了一些帮助注释:

<#
.SYNOPSIS
Adds a file name extension to a supplied name.

.DESCRIPTION
Adds a file name extension to a supplied name.
Takes any strings for the file name or extension.

.PARAMETER Name
Specifies the file name.

.PARAMETER Extension
Specifies the extension. "Txt" is the default.

.INPUTS
None. You cannot pipe objects to Add-Extension.

.OUTPUTS
System.String. Add-Extension returns a string with the extension or file name.

.EXAMPLE
C:\PS> extension -name "File"
File.txt   

#>

When I run the Get-Help "Function Name" directly after the function and run the ps1 script via powershell window, I get the correct response. 当我在函数之后直接运行Get-Help“函数名称”并通过powershell窗口运行ps1脚本时,我得到了正确的响应。

Now, I am using the code from here : http://ben.neise.co.uk/scriptdocumentationinmarkdown/ 现在,我在这里使用代码: http : //ben.neise.co.uk/scriptdocumentationinmarkdown/

This is suppose to generate some Markdown documentation from the help comments within my script files. 假设这是从脚本文件中的帮助注释中生成一些Markdown文档的。 When running it I get the error : 运行它时出现错误:

GenerateScriptDocumentationInMarkdown : Inline help not found for script C:\****\testScript.ps1

(GenerateScriptDocumentationInMarkdown being the function name). (GenerateScriptDocumentationInMarkdown是函数名称)。

The line it errors is the line where I call the function with some filepaths : 它出错的行是我使用一些文件路径调用该函数的行:

GenerateScriptDocumentationInMarkdown -SourceScriptFolder "C:\****\testScripts"  -DocumentationOutputFolder "C:\****\GeneratingMarkdownFileTest" -DocumentationIndexPath "C:\****\GeneratingMarkdownFileTest\scripts_Ps1.markdown"

What does the 'inline help not found for script' error mean ? “找不到脚本的内联帮助”错误是什么意思?

EDIT 编辑

Here is where it fails in the script : 这是脚本失败的地方:

 $help = Get-Help $script.FullName -ErrorAction "SilentlyContinue"       
        if ($help.getType().Name -eq "String"){
            # If there's no inline help in the script then Get-Help returns a string
            Write-Error -Message "Inline help not found for script $($script.FullName)"
        } else {

Notice the condition checking if the .Name is of type string. 注意条件检查.Name是否为字符串类型。 Looks to me the Get-Help on my script is not returning what it is suppose to. 在我看来,我脚本上的Get-Help没有返回它应该的内容。

I would expect the 'get-help {pathtofile.ps1}' would return all the comments in the script file, but all it returns is the scripts name ? 我希望'get-help {pathtofile.ps1}'将返回脚本文件中的所有注释,但返回的只是脚本名称?

Figured it out thanks to @Bacon Bits. 通过@Bacon Bits弄清楚了。

As the comments were in the functions, I was misinterpreting what the generate function does. 由于注释在函数中,因此我误解了generate函数的作用。 What I had to do was loop through the functions within the script file and then run the logic on the functions rather than the whole file. 我要做的是遍历脚本文件中的功能,然后在功能上而不是整个文件上运行逻辑。

So to get the functions in only this file : 因此,仅在此文件中获取功能:

 $currentFunctions = Get-ChildItem function:
        # dot source your script to load it to the current runspace
        . $script.FullName
        $scriptFunctions = Get-ChildItem function: | Where-Object { $currentFunctions -notcontains $_ }

        $scriptFunctions | ForEach-Object {
              & $_.ScriptBlock
        }

Then loop through and run the logic like so : 然后遍历并运行逻辑,如下所示:

$scriptFunctions | ForEach-Object {

        $fullName = $_.Name
        $help = Get-Help $fullName -ErrorAction "SilentlyContinue" 
.
.
.

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

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