在脚本中调用时,Get-Help 格式不同
Get-Help format is different when calling it in a script
我想知道为什么我的 PowerShell get-help
在使用我编写的脚本时输出如下图所示。该脚本的目的是在从数组中选择函数时显示 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 #> 注释块(您可以在提供的图像中查看函数的详细信息——来自函数的注释帮助块)。如果我 运行 获得有关目标脚本中函数的帮助,它的格式似乎正常——但使用我编写的脚本时情况并非如此。
真正困扰我的是@{Text = 'stuff'}格式等。提前致谢!
您正在通过 format-list 传输 get-help
的输出。 "overrides" 默认格式 PS 对 get-help
创建的 PSCustomObject
(至少在 PS 3.0 中)有效。您应该能够自己调用 get-help 而不是管道它。如果这不起作用,则通过 out-default
.
进行管道传输
有关详细信息,请参阅 help about_format
。
我想知道为什么我的 PowerShell get-help
在使用我编写的脚本时输出如下图所示。该脚本的目的是在从数组中选择函数时显示 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 #> 注释块(您可以在提供的图像中查看函数的详细信息——来自函数的注释帮助块)。如果我 运行 获得有关目标脚本中函数的帮助,它的格式似乎正常——但使用我编写的脚本时情况并非如此。
真正困扰我的是@{Text = 'stuff'}格式等。提前致谢!
您正在通过 format-list 传输 get-help
的输出。 "overrides" 默认格式 PS 对 get-help
创建的 PSCustomObject
(至少在 PS 3.0 中)有效。您应该能够自己调用 get-help 而不是管道它。如果这不起作用,则通过 out-default
.
有关详细信息,请参阅 help about_format
。