2017-10-04 58 views
0

我必須在這裏丟失一些簡單的東西。PowerShell GetHelp只返回script.ps1文件的文件名

我在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 

#> 

當我運行獲取幫助「功能名稱」功能後,直接運行通過PowerShell窗口中的PS1腳本,我得到了正確的迴應。現在

,我使用的代碼從這裏:http://ben.neise.co.uk/scriptdocumentationinmarkdown/

這是假設生成從我的腳本文件中的幫助評論一些降價文檔。運行時出現錯誤:

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

(GenerateScriptDocumentationInMarkdown是函數名稱)。

線它的錯誤是我調用該函數的一些文件路徑線:

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

什麼是錯誤的「未找到腳本內嵌幫助」是什麼意思?

編輯

這裏是它失敗的腳本:

$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 { 

通知狀態檢查,如果是,請將.Name字符串類型。在我看來,我腳本上的Get-Help沒有返回它想要的。

我期望'get-help {pathtofile.ps1}'會返回腳本文件中的所有註釋,但它返回的是腳本名稱?

+0

對不起@BaconBits我忘了補充它失敗。我添加了一條新評論。看起來get-help沒有返回預期的結果,只返回腳本文件的名字。有任何想法嗎 ? – thatOneGuy

+0

等一下,你是否在某個文件中的某個函數中實現了幫助?你試圖拉的幫助不是對scriptlet的內聯幫助嗎? 'Get-Help'只返回頂級項目的幫助。你必須加載函數定義,然後運行'Get-Help {function name}',並且你正在使用的腳本不是爲此而構建的。 –

+0

@BaconBits感謝您的幫助,設法解決。見下面的答案:) – thatOneGuy

回答

0

歸功於@Bacon Bits。

由於評論是在函數中,我誤解了生成函數的功能。我所要做的就是循環腳本文件中的函數,然後在函數上運行邏輯而不是整個文件。

因此,要獲得功能只在本文件:

$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 
     } 

然後遍歷和運行邏輯,像這樣:

$scriptFunctions | ForEach-Object { 

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