3

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.

1 Answer 1

2

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

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.