0

我想知道为什么get-help 在使用我编写的脚本时我的 PowerShell 输出如下图所示。该脚本的目的是在get-help从数组中选择函数时显示信息。

#Run this file in the same directory as the Functions file.

#this function validates user input
function getInput
{
    do
    {
            $input = Read-Host "`n>Enter function # to see its description"

    }until(([int]$input -gt 0) -and ([int]$input -le $flist.count))

    $input
}

#include the script we want
. "$PSScriptRoot\functions.ps1"

#This operates on a loop. After viewing your help info, press key and you will be prompted to choose another function.
$quit = 0
while(!$quit){
    #get all functions
    $f = @(get-content functions.ps1 | where-object { $_.StartsWith("function", "CurrentCultureIgnoreCase") -and (-not $_.Contains("#")); $c++} | sort-object)

     "There are " + $f.count + " functions!"

    #split on ' ', get second word (function name), add to array
    $flist = @{}
    $i = 0
    foreach($line in $f){
        $temp = $line.split(' ')
        $temp[1]
        $i++
        $flist.add($i, $temp[1])
    }

    #print, order ascending
    $flist.GetEnumerator() | sort -Property name

    #accept user input
    $input = getInput

    #get-help about the chosen function
    "Get-Help " + $flist[[int]$input]
    Get-Help Add-ADGrouptoLocalGroup | format-list
    #Get-Help $flist[[int]$input] -full
    Get-Command $flist[[int]$input] -syntax
    Pause
}

目标脚本$PSScriptRoot\Functions.ps1中有一堆函数。我的脚本正在做的是:

  • 列出在目标文件中找到的所有函数。
  • 将他们的名字放在索引数组中
  • 在给定索引处提示用户获取帮助的功能
  • 打印所选函数的 get-help 和 get-syntax

每个函数都有 <#.SYNOPSIS .DESCRIPTION ... etc #> 注释块(您可以在提供的图像中看到函数的详细信息 - 从函数的注释帮助块中)。如果我在目标脚本中对函数运行 get-help,它似乎是正常格式化的——但在使用我编写的脚本时情况并非如此。

真正困扰我的是 @{Text = 'stuff'} 格式等。提前谢谢!

我的脚本的 Get-Help 输出

4

1 回答 1

1

您正在get-help通过格式列表传递输出。这“覆盖”了 PS 在创建的PSCustomObject(至少在 PS 3.0 中)上执行的默认格式设置get-help。您应该能够自己调用 get-help 而不是管道它。如果这不起作用,则将其通过out-default.

有关help about_format更多详细信息,请参阅。

于 2016-01-15T23:52:50.220 回答