HomeLinuxStudy How PowerShell CmdletBinding Enhances Features

Study How PowerShell CmdletBinding Enhances Features


A perform is just a chunk of code that accommodates directions that can be utilized to create output from its enter. A perform will be reused over and over. The performance of a perform will be enhanced utilizing the “CmdletBinding” attribute. It helps the perform to look and function like a compiled cmdlet in PowerShell. Doing so will present the perform changed into a cmdlet and entry to all of the cmdlet options.

The next put up will present particulars in regards to the attribute “CmdletBinding”.

Study How PowerShell CmdletBinding Enhances Features

The attribute “CmdletBinding” is utilized to reinforce the perform. Notably, the core perform of this attribute is to show the perform into an operable cmdlet.

Examples explaining the said attribute are given beneath.

Instance 1: Use the “CmdletBinding” Attribute to Rework the String From Higher Case to Decrease Case

On this instance, the “CmdletBinding” attribute will remodel the string to decrease case:

Perform StringToLowerCase {
    [CmdletBinding()]Param()
    “THIS IS LINUX HINT PORTAL.”.ToLower();
}
StringToLowerCase

Within the talked about code above:

  • First, create a perform and specify a reputation for it.
  • Then, create a “Param()” and specify the “[CmdletBinding()]” parameter earlier than it.
  • After that, write a string inside inverted quotes and concatenate it with the “ToLower()” technique.
  • Lastly, name the perform by specifying its identify outdoors the curly braces:

Instance 2: Use the “CmdletBinding” Attribute in a Perform Alongside With the “-Verbose” Parameter

This demonstration will remodel the string into lowercase. Furthermore, it is going to show the verbose message with the help of the “-Verbose” parameter:

Perform StringToLowerCase {
    [CmdletBinding()]Param()
    Write-Verbose “The -verbose parameter will show the verbose assertion.”
    “WELC0ME TO THE CONSOLE.”.ToLower();
}
StringToLowerCase -Verbose

Within the above-stated code:

  • The verbose assertion is given utilizing the “Write-Verbose” cmdlet.
  • Then, the perform identify is specified outdoors the curly braces together with the “-Verbose” parameter:

Instance 3: Use the “CmdletBinding” Attribute Alongside With the “SupportsShouldProcess” and “PSCmdlet” Object

This illustration will create a immediate, which is able to verify whether or not to remodel the string to higher case or not:

Perform StringToLowerCase {
    [CmdletBinding(SupportsShouldProcess=$True)]Param()
    Write-Verbose “The -verbose parameter will show the verbose assertion.”
    if ($PSCmdlet.ShouldContinue(“Verify?”, “Rework string to LowerCase”)) {
        “HELLO WORLD”.ToLower();
    } Else {
        “HELLO WORLD”
    }
}

Within the above-stated code:

  • First, create a perform and specify a reputation.
  • Contained in the perform, cross the “SupportsShouldProcess=$True” contained in the “CmdletBinding()” attribute.
  • After that, create an “if” situation and cross the “$PSCmdlet.ShouldContinue()” parameter inside it.
  • Then, add the textual content contained in the above-stated parameter to be displayed on the time of getting affirmation from the consumer.
  • The “if” situation will remodel the string to lower-case if the consumer clicks on the “Sure” button else the string case received’t change:

Click on on the “Sure” button to remodel the string right into a lowercase:

StringToLowerCase -Verify

It may be noticed that the string has been remodeled to decrease case.

Conclusion

The “CmdletBinding” attribute in PowerShell is used to transform the perform into an operable cmdlet. Doing so will present entry to all cmdlet options to the perform changed into a cmdlet. This weblog has elaborated on PowerShell’s “CmdletBinding” attribute to reinforce the perform.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments