Friday, April 19, 2024
HomePowershellIncluding a Assist Parameter to a Operate

Including a Assist Parameter to a Operate


Edit: There’s a fast addition on the backside of this publish. I attempted one thing else, it labored, and so I made a decision to incorporate it.

I began writing a PowerShell operate to copy the work of an outdated executable referred to as ICSWEEP. ICSWEEP “is a command-line utility to clear the Non permanent Web Information Cache and/or the TEMP recordsdata folder of ALL consumer profiles which might be NOT in use when this command is executed.” ICSWEEP Info. I’ll or might not stroll by the method of scripting this operate right here; it’s to be decided. What I do need to talk about for positive, nonetheless, is the final change within the above picture. It’s /?.

We can not add ? parameter to a operate. Beneath is a picture of the message after we try to try this in VS Code.

That’s proper, proper!? You do bear in mind the $? variable. It shops the execution standing of the final command as True or False. As a substitute of ? as a parameter, I used the worth of Assist. Subsequently, somebody can invoke my operate and use -Assist to get assist with the operate. However may they?

I sat there, questioning, are you able to create a parameter of a operate in order that it’ll return its personal, comment-based assist? It seems you’ll be able to. Right here’s the early stage of this operate. It consists of the comment-based assist, a couple of parameters, and a small quantity of code.


operate Clear-UserTemporaryFile {
<#
.SYNOPSIS
    The Clear-UserTemporaryFile command ...
.DESCRIPTION
.PARAMETER <Parameter>
.EXAMPLE
.NOTES
    Identify: Clear-UserTemporaryFile
    Writer: Tommy Maynard
    Feedback: --
    Final Edit: 12/13/2022 [1.0.0]
    Model: 1.0.0
#>
    Param (
        [Parameter()]
        [switch]$ALL,
        [Parameter()]
        [string]$TIF,
        [Parameter()]
        [string]$TMP,
        [Parameter()]
        [string]$SIZE,
        [Parameter()]
        [switch]$HELP
        
    )

    if ($HELP) {
       Get-Assist -Identify "$($MyInvocation.MyCommand.Identify)"
       Exit
    }
}

Within the above code, I added a easy if assertion. It screens whether or not or not the Assist parameter is used when the operate is invoked. Whether it is, it runs the Get-Assist cmdlet towards the identify of the executing operate—itself—utilizing the $MyInvocation variable after which exits the operate.


Clear-UserTemporaryFile -Assist

NAME
    Clear-UserTemporaryFile

SYNOPSIS
    The Clear-UserTemporaryFile command ...

SYNTAX
    Clear-UserTemporaryFile [-ALL] [[-TIF] <String>] [[-TMP] <String>] [[-SIZE] <String>] [-HELP] [<CommonParameters>]

DESCRIPTION

RELATED LINKS

REMARKS
    To see the examples, sort: "Get-Assist Clear-UserTemporaryFile -Examples"
    For extra data, sort: "Get-Assist Clear-UserTemporaryFile -Detailed"
    For technical data, sort: "Get-Assist Clear-UserTemporaryFile -Full"

At this level, a few of you might already know the place I’m going to go subsequent. There was one thing else I had forgotten about each cmdlet and performance written. All of them have one thing in frequent. And what’s that? There’s a strategy to return a command’s assist utilizing, of all issues, the query mark! Have a look utilizing the built-in Get-Verb and Get-Command cmdlets under.


Get-Verb -?

NAME
    Get-Verb

SYNTAX
    Get-Verb [[-Verb] <string[]>] [[-Group] 
    Safety] [<CommonParameters>]

ALIASES
    None

REMARKS
    Get-Assist can not discover the Assist recordsdata for this cmdlet on this laptop. It's displaying solely partial assist.
        -- To obtain and set up Assist recordsdata for the module that features this cmdlet, use Replace-Assist.
        -- To view the Assist subject for this cmdlet on-line, sort: "Get-Assist Get-Verb -On-line" or
           go to https://go.microsoft.com/fwlink/?LinkID=2097026.

Get-Command -?

NAME
    Get-Command

SYNTAX
    Get-Command [[-ArgumentList] <Object[]>] [-Verb <string[]>] [-Noun <string[]>] [-Module <string[]>]
    [-FullyQualifiedModule <ModuleSpecification[]>] [-TotalCount <int>] [-Syntax] [-ShowCommandInfo] [-All]
    [-ListImported] [-ParameterName <string[]>] [-ParameterType <PSTypeName[]>] [<CommonParameters>]

    Get-Command [[-Name] <string[]>] [[-ArgumentList] <Object[]>] [-Module <string[]>] [-FullyQualifiedModule
    <ModuleSpecification[]>] [-CommandType  Cmdlet ] [-TotalCount <int>] [-Syntax] [-ShowCommandInfo] [-All] [-ListImported] [-ParameterName
    <string[]>] [-ParameterType <PSTypeName[]>] [-UseFuzzyMatching] [-UseAbbreviationExpansion] [<CommonParameters>]

ALIASES
    gcm

REMARKS
    Get-Assist can not discover the Assist recordsdata for this cmdlet on this laptop. It's displaying solely partial assist.
        -- To obtain and set up Assist recordsdata for the module that features this cmdlet, use Replace-Assist.
        -- To view the Assist subject for this cmdlet on-line, sort: "Get-Assist Get-Command -On-line" or
           go to https://go.microsoft.com/fwlink/?LinkID=2096579.

This implies I don’t even want a Assist parameter. PowerShell already has this lined, and right here’s the proof.


Clear-UserTemporaryFile -?

NAME
    Clear-UserTemporaryFile

SYNOPSIS
    The Clear-UserTemporaryFile command ...

SYNTAX
    Clear-UserTemporaryFile [-ALL] [[-TIF] <String>] [[-TMP] <String>] [[-SIZE] <String>] [-HELP] [<CommonParameters>]

DESCRIPTION

RELATED LINKS

REMARKS
    To see the examples, sort: "Get-Assist Clear-UserTemporaryFile -Examples"
    For extra data, sort: "Get-Assist Clear-UserTemporaryFile -Detailed"
    For technical data, sort: "Get-Assist Clear-UserTemporaryFile -Full"

Edit: We all know that we have been ready to make use of this PowerShell to return the assistance for our operate,


Get-Assist -Identify "$($MyInvocation.MyCommand.Identify)"

however we may’ve simply as simply completed this:


Invoke-Expression -Command "$($MyInvocation.MyCommand.Identify) -?"

The primary choice invokes the Get-Assist cmdlet towards our operate. The second choice invokes the identical operate that’s already being invoked, including a -? to the top of the entire command.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments