在PowerShell脚本中使用Get-Help时出现问题

时间:2019-03-13 17:37:30

标签: powershell command-line

它必须非常简单,但是我无法在我的PowerShell脚本中使Get-Help正常工作。

  • 当我在PowerShell命令窗口中运行Get-Help myscript -Examples时,会得到完美的帮助消息。但是,
  • 当我在PowerShell脚本中调用Get-Help myscript -Examples时,它的运行就像未指定-Examples一样-显示常规帮助,而不是示例帮助。

更新:

@lit怀疑,原因是,我在当前正在运行的同一脚本上运行Get-Help。

我只想显示有关我的脚本的帮助消息。这是您可以尝试的示例:

<#
.SYNOPSIS

Calculates the number of possible passwords

.DESCRIPTION

Calculates the number of possible passwords based
on the input so you will know the actual number before
you proceed to create your dictionary file.

.PARAMETER CharacterSet

Specifies the characters (letters, numbers, symbols) that
need to be included. Parameter is mandatory.

.PARAMETER MinCharacters

Specifies the minimum characters of the generated passwords.
Parameter is mandatory.

.PARAMETER MaxCharacters

Specifies the maximum characters of the generated passwords.
Parameter is mandatory.

.PARAMETER IncludeCapital

Specifies whether or not to include upper case letters along with
the lower case letters.

.PARAMETER CapitalOnly

Specifies whether or not all lower case letters to be converted to
upper case letters.

.INPUTS

System.String. Get-PasswordNumber can accept a string value to
determine the CharacterSet parameter.

.OUTPUTS

System.Double. Get-PasswordNumber returns the number of passwords that
can be created.

.EXAMPLE

C:\PS> Get-PasswordNumber -CharacterSet "a,b,c,1,2,3,$,*,&" -MinCharacters 2 -MaxCharacters 5
66420

.EXAMPLE

C:\PS> Get-PasswordNumber -Characters "a,b,c,1,2,3,$,*,&" -MinCharacters 2 -MaxCharacters 5 -IncludeCapital
271440

.EXAMPLE

C:\PS> Get-PasswordNumber -Characters "a,b,c,1,2,3,$,*,&" -MinCharacters 2 -MaxCharacters 5 -CapitalOnly
66420

.EXAMPLE

C:\PS> Get-PasswordNumber -Characters alphabet -MinCharacters 2 -MaxCharacters 5
12356604

.LINK

PowerShell Module DictionaryFile
#>


param(
        [switch]$IncludeCapital,
        [switch]$CapitalOnly)

write-host program started.

if (!$CapitalOnly) {
  Get-Help myscript
  Get-Help myscript -Examples

}

write-host program ended.

1 个答案:

答案 0 :(得分:1)

简短答案:按如下方式使用Out-String

Get-Help $MyInvocation.InvocationName -Examples | Out-String

详细信息

  • 发现对于所有有效cmdlet /函数/脚本名称,出现的描述的行为都代替了{li}中的myscript
  

当我在PowerShell脚本中调用Get-Help myscript -Examples时,它的运行就像未指定-Examples

  

因为Get-Help cmdlet生成了MamlCommandHelpInfo   对象,而不是字符串,则必须使用一个cmdlet来转换   帮助主题内容转换为字符串,例如Out-StringOut-File