PowerShell Gallery PSScriptInfo comment header breaking "Get-Help .\MyScriptName.ps1 -Full"

Viewed 114

So I've been publishing my script to the PowerShell Gallery, using their required comment header with version, GUID, author, etc. The problem is, I've noticed that this comment header addition has broken Get-Help, which used to parse the comment based help header in my script, generating formatted help output to the console. I've submitted feedback about allowing the PSScriptInfo header to come AFTER the other comment header, to the PS Gallery team, but I'm seeking a workaround in the meantime.

I'm looking for a hopefully one-line solution, where maybe I could skip over or drop/parse out the PSScriptInfo header, and then feed the rest to Get-Help? Going to try this approach and report back if no one beats me to it. I'm thinking a simple temp file. I searched stackoverflow and didn't see anyone else asking this question, so I wanted to make this problem/solution searchable by others.

This is what I'm actually seeing (with PSScriptInfo header):

PS> Get-Help .\New-SubredditHTMLArchive.ps1 -Full
New-SubredditHTMLArchive.ps1 [[-Subreddit] <string>] [[-Subreddits] <string[]>] [-InstallPackages] [-Background]

This is what I'm expecting to see (without PSScriptInfo header):

PS> Get-Help .\New-SubredditHTMLArchive.ps1 -Full

NAME
    C:\github\New-SubredditHTMLArchive\New-SubredditHTMLArchive.ps1

SYNOPSIS
    Checks for (or installs) prerequisites, then uses BDFR and BDFR-HTML Python modules to generate a subreddit HTML archive.
    By default, creates root 'New-SubredditHTMLArchive' output folder and under your %USERPROFILE% ($env:USERPROFILE) Documents folder.
    Runs itself as a scheduled task as the current user, as an interactive console by default. The task can be run as a background task with the -Background parameter, allowing use of the lock screen.


SYNTAX
    C:\github\New-SubredditHTMLArchive\New-SubredditHTMLArchive.ps1 [[-Subreddit] <String>] [[-Subreddits] <String[]>] [-InstallPackages] [-Background] [<CommonParameters>]


DESCRIPTION
    If you already have Python 3.9+, Git 2+, and GitHub CLI 2+ installed, you can skip this section.
    This script does NOT require administrator privileges to run, or to install the Python modules, WITHOUT the -InstallPackages parameter.
    On first run, you must include the -InstallPackages parameter, or manually install the below software packages before running this script.
    When installing these packages automatically, the user must confirm a UAC admin prompt for each package, allowing the installer to make changes to their computer.
        1. Git: ... (only when manually installing)
        2. GitHub CLI: ... (only when manually installing)
            i. You'll need to launch cmd.exe and authenticate with 'gh auth login', and follow the prompts, pasting the OTP into your browser, after logging into your GitHub account (or make a new account).
        3. Python 3.9+ (includes pip): ... (only when manually installing)
            i. At beginning of install, YOU MUST CHECK 'Add Python 3.x to PATH'. (So PowerShell can call python.exe and pip.exe from anywhere)
    This script uses the following Python modules, which are detected and installed automatically via pip:
        1. BDFR: ...
        2. BDFR-HTML: ...
            i. When running setup.py to install BDFR-HTML (via script or manually), you may get an install error from Pillow about zlib being missing. You may need to run 'pip install pillow' from an elevated command prompt, so that Pillow
    installs correctly.
            ii. For manual BDFR-HTML install in case of Pillow install error: From an elevated CMD window, type these two quoted commands: 1) "cd %USERPROFILE%\Documents\BDFR\module_clone\bdfr-html", 2) "python.exe setup.py install"
            iii. ...


PARAMETERS
    -Subreddit <String>
        The name of the subreddit (as it appears after the /r/ in the URL) that will be archived.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -Subreddits <String[]>
        An array of subreddit names (as they appear after the /r/ in the URL) that will be archived.
        Also generates a master index.html containing links to all of the other generated subreddit index.html files.
        All generated subreddit folders, files, and index pages, are automatically packaged into a ZIP file.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -InstallPackages [<SwitchParameter>]
        The script will attempt to install ONLY MISSING pre-requisite packages: Python 3, GitHub, and Git
        When 'python.exe', 'gh.exe', or 'git.exe' are already in your $env:path, and executable from PowerShell, they will NOT be installed or modified.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -Background [<SwitchParameter>]
        The script will spawn the scheduled task with S4U logon type instead of Interactive logon type. Requires approval of an admin UAC prompt to spawn the task.
        This switch allows the script to keep running in the background, regardless of user's logon state (such as lock screens, when running overnight).

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).

INPUTS

OUTPUTS

NOTES

        Last update: Friday, March 18, 2022 6:44:49 PM

    -------------------------- EXAMPLE 1 --------------------------

    PS>.\New-SubredditHTMLArchive.ps1 -Subreddit PowerShell -InstallPackages

    -------------------------- EXAMPLE 2 --------------------------

    PS>.\New-SubredditHTMLArchive.ps1 -Subreddit PowerShell

    -------------------------- EXAMPLE 3 --------------------------

    PS>.\New-SubredditHTMLArchive.ps1 -Subreddits (Get-Content "$($env:USERPROFILE)\Desktop\subreddit_list.txt") -InstallPackages

    -------------------------- EXAMPLE 4 --------------------------

    PS>.\New-SubredditHTMLArchive.ps1 -Subreddits 'PowerShell','Python','AmateurRadio','HackRF','GNURadio','OpenV2K','DataHoarder','AtheistHavens','Onions' -Background
1 Answers

Below is my one liner, however, when I point Get-Help at the path, the cmdlet is now just displaying its own "Windows PowerShell Help System", as if I didn't call it with any parameters? What??

PS> (Get-Content .\New-SubredditHTMLArchive.ps1 | Select-Object -Skip 7 | Set-Content "$($env:temp)\help.ps1"); Get-Help -Name "$($env:temp)\help.ps1" -Full

Update: so for some undocumented reason, the Get-Help cmdlet only parses the comment header on a script if the execution policy is relaxed ..because reasons. So to get the above one-liner working, you cannot have the execution policy set to 'AllSigned' like my environment was, you have to turn it down to 'Unrestricted'. But setting the execution policy does get it working:

PS> Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

Update 2: to further clarify the execution policy issue, when the ExecutionPolicy is set to AllSigned, Get-Help looks online and then gives up, even with a temp.ps1 script that has been re-signed after stripping out the first 7 lines:

PS C:\> Get-ExecutionPolicy
Unrestricted
PS C:\> Get-Help -Name "$($env:temp)\temp.ps1" -Full

NAME
    C:\Users\username\AppData\Local\Temp\temp.ps1

SYNOPSIS
    Checks for (or installs) prerequisites, then uses BDFR and BDFR-HTML Python modules to generate a subreddit HTML
    archive.
    By default, creates root 'New-SubredditHTMLArchive' output folder and under your %USERPROFILE% ($env:USERPROFILE)
    Documents folder.
    Runs itself as a scheduled task as the current user, as an interactive console by default. The task can be run as
    a background task with the -Background parameter, allowing use of the lock screen.
    The reddit API returns a maximum of 1000 posts per BDFR pull, so only the newest 1000 posts will be included:
    https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/queries.py
    Script download URL from web browsers, so the code signature still works (Save As):
    https://raw.githubusercontent.com/mbarr564/powershell/master/New-SubredditHTMLArchive.ps1


SYNTAX
    C:\Users\username\AppData\Local\Temp\temp.ps1 [[-Subreddit] <String>] [[-Subreddits] <String[]>] [-InstallPackages]
    [-Background] [<CommonParameters>]


DESCRIPTION
    If you already have Python 3.9+, Git 2+, and GitHub CLI 2+ installed, you can skip this section.
    This script does NOT require administrator privileges to run, or to install the Python modules, WITHOUT the
    -InstallPackages parameter.
    On first run, you must include the -InstallPackages parameter, or manually install the below software packages
    before running this script.
    When installing these packages automatically, the user must confirm a UAC admin prompt for each package, allowing
    the installer to make changes to their computer.
        1. Git: https://github.com/git-for-windows/git/releases/ (only when manually installing)
        2. GitHub CLI: https://github.com/cli/cli/releases/ (only when manually installing)
            i. You'll need to launch cmd.exe and authenticate with 'gh auth login', and follow the prompts, pasting
    the OTP into your browser, after logging into your GitHub account (or make a new account).
        3. Python 3.9+ (includes pip): https://www.python.org/downloads/windows/ (only when manually installing)
            i. At beginning of install, YOU MUST CHECK 'Add Python 3.x to PATH'. (So PowerShell can call python.exe
    and pip.exe from anywhere)
    This script uses the following Python modules, which are detected and installed automatically via pip:
        1. BDFR: https://pypi.org/project/bdfr/
        2. BDFR-HTML: https://github.com/BlipRanger/bdfr-html
            i. When running setup.py to install BDFR-HTML (via script or manually), you may get an install error from
    Pillow about zlib being missing. You may need to run 'pip install pillow' from an elevated command prompt, so that
    Pillow installs correctly.
            ii. For manual BDFR-HTML install in case of Pillow install error: From an elevated CMD window, type these
    two quoted commands: 1) "cd %USERPROFILE%\Documents\BDFR\module_clone\bdfr-html", 2) "python.exe setup.py install"
            iii. https://stackoverflow.com/questions/64302065/pillow-installation-pypy3-missing-zlib


PARAMETERS
    -Subreddit <String>
        The name of a single subreddit that will be archived.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -Subreddits <String[]>
        An array of subreddit names that will be archived.
        Also generates a master index.html containing links to all of the other generated subreddit index.html files.
        All generated subreddit folders, files, and index pages, are automatically packaged into a ZIP file.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -InstallPackages [<SwitchParameter>]
        The script will attempt to install ONLY MISSING pre-requisite packages: Python 3, GitHub, and Git
        When 'python.exe', 'gh.exe', or 'git.exe' are already in your $env:path, and executable from PowerShell, they
        will NOT be installed or modified.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -Background [<SwitchParameter>]
        The script will spawn the scheduled task with S4U logon type instead of Interactive logon type. Requires
        approval of an admin UAC prompt to spawn the task.
        This switch allows the script to keep running in the background, regardless of user's logon state (such as
        lock screens, when running overnight).

        Required?                    false
        Position?                    named                                                                                      Default value                False                                                                                      Accept pipeline input?       false                                                                                      Accept wildcard characters?  false                                                                                                                                                                                                          <CommonParameters>                                                                                                          This cmdlet supports the common parameters: Verbose, Debug,                                                             ErrorAction, ErrorVariable, WarningAction, WarningVariable,                                                             OutBuffer, PipelineVariable, and OutVariable. For more information, see                                                 about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).                                                                                                                                                                 INPUTS                                                                                                                                                                                                                                          OUTPUTS                                                                                                                 
NOTES

        Last update: Monday, March 21, 2022 10:13:13 PM

    -------------------------- EXAMPLE 1 --------------------------

    PS>.\New-SubredditHTMLArchive.ps1 -Subreddit PowerShell -InstallPackages

    -------------------------- EXAMPLE 2 --------------------------

    PS>.\New-SubredditHTMLArchive.ps1 -Subreddits (Get-Content "$($env:USERPROFILE)\Desktop\subreddit_list.txt")
    -Background

    -------------------------- EXAMPLE 3 --------------------------

    PS>.\New-SubredditHTMLArchive.ps1 -Subreddits
    'PowerShell','Python','AmateurRadio','HackRF','GNURadio','OpenV2K','DataHoarder','AtheistHavens','Onions'
    -Background

RELATED LINKS

PS C:\> Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope Process

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
PS C:\> Get-ExecutionPolicy
AllSigned
PS C:\> Get-Help -Name "$($env:temp)\temp.ps1" -Full
Get-Help : Get-Help could not find C:\Users\username\AppData\Local\Temp\temp.ps1 in a help file in this session. To
download updated help topics type: "Update-Help". To get help online, search for the help topic in the TechNet library
at https:/go.microsoft.com/fwlink/?LinkID=107116.
At line:1 char:1
+ Get-Help -Name "$($env:temp)\temp.ps1" -Full
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Get-Help], HelpNotFoundException
    + FullyQualifiedErrorId : HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand

PS C:\>
Related