Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem using Get-Help from within PowerShell script

It must be something extremely simple but I just can't make Get-Help work from within my PowerShell script.

  • When I run Get-Help myscript -Examples in PowerShell command window, I get perfect help message. However,
  • When I call Get-Help myscript -Examples in my PowerShell script, it runs as if that the -Examples is not specified -- the normal help is shown, instead of the examples help.

UPDATE:

As @lit has suspect, the reason is that, I'm running Get-Help on the same script that is currently running.

I just want to show help message for for my script. Here is an example you can try out:

<#
.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.
like image 331
xpt Avatar asked Jun 25 '26 16:06

xpt


1 Answers

Short answer: use Out-String as follows:

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

In detail:

  • found that described behaviour appears for any valid cmdlet / function /script name in place of myscript in

when I call Get-Help myscript -Examples in my PowerShell script, it runs as if that the -Examples is not specified

  • found an innocent (however supposedly unrelated) note in Get-Help Get-Help -Online:

Because the Get-Help cmdlet generates a MamlCommandHelpInfo object, not a string, you have to use a cmdlet that transforms the help topic content into a string, such as Out-String or Out-File.

  • unsuccessfully sniffed in MamlCommandHelpInfo.cs for differences in using Get-Help from within a .ps1 script vs. PS prompt.
like image 160
JosefZ Avatar answered Jun 28 '26 06:06

JosefZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!