The PowerShell function uses $psboundparameters to get an instance of the input argument list _powershell

Source: Internet
Author: User

This article describes that when customizing the PowerShell function, there is a $psboundparameters variable to get to the input parameter list. Getting the input parameter list is a great help for our functional debugging.

When we execute a function, we can use the value of the input parameters, but I am in the PowerShell, the parameters are optional, some optional, then how do we know the user's actual input parameters?

In the PowerShell function, there is a global variable $psboundparameters, which is a hash table that holds all the input parameters and the values of the input parameters. By outputting This variable, we can see all the input parameters.

Copy Code code as follows:

function Get-parameter {
Param
(
$Name,
$LastName = ' Default ',
$Age,
$Id
)
$PSBoundParameters
}

This function defines four parameters, and the user can optionally select parameters for input, so we use the $psboundparameters variable to get the input parameter list.

The status of implementation is as follows:

Copy Code code as follows:

Ps> Get-parameter
Ps> Get-parameter-name Test-id 12
Key Value
--- -----
Name Test
Id 12

As you can see, the same call to this function, no input parameters, the output is empty. Parameter names and parameter values are output as they were when the parameters were entered.

About PowerShell function get input parameter list ($PSBoundParameters), this article introduces so much, hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.