Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function with optional call variables

Is there a way to create a parameter in a PowerShell function where you have to call it in order to have it considered?

An example given by commandlet (the bold being what I want to do):

Invoke-Command -computername Server01 -Scriptblock {...}

Here is an example of what I want to do with the function

Function DoStuff($computername, -arg2, -domain $domain)
    Test-parameter(-domain) if (-domain -eq $true) {
        use $domain
    }
    Else {
        $domain = "Domain1"
    }
    test-parameter($arg2) {
        if ($arg2 -eq $true) {
            Do something
        }
        else {
            Do the opposite
        }
    }

So in summary:

If "-arg2" is present, I want something to happen in the script. If "-Domain" is present and has an argument with it, I want that to be used rather then the set argument.

like image 467
user100541 Avatar asked Dec 03 '12 16:12

user100541


People also ask

How do you make a function an optional argument?

You can define Python function optional arguments by specifying the name of an argument followed by a default value when you declare a function. You can also use the **kwargs method to accept a variable number of arguments in a function.

Can you have optional parameters in Python?

You can now start writing functions with required and optional parameters and with a variable number of non-keyword and keyword arguments. Mastering these skills will help you take your Python coding to the next level.

How can I pass optional or keyword parameters from one function to another?

Users can either pass their values or can pretend the function to use theirs default values which are specified. In this way, the user can call the function by either passing those optional parameters or just passing the required parameters. Without using keyword arguments. By using keyword arguments.

How do you make a variable optional?

If you want a variable to be optional, you can overload the method using a signature which doesn't require the parameter. Method overloading is a good answer in my opinion, while varargs is a very bad answer.


3 Answers

Powershell provides a lot of built-in support for common parameter scenarios, including mandatory parameters, optional parameters, "switch" (aka flag) parameters, and "parameter sets."

By default, all parameters are optional. The most basic approach is to simply check each one for $null, then implement whatever logic you want from there. This is basically what you have already shown in your sample code.

If you want to learn about all of the special support that Powershell can give you, check out these links:

about_Functions

about_Functions_Advanced

about_Functions_Advanced_Parameters

like image 77
latkin Avatar answered Oct 05 '22 13:10

latkin


I don't think your question is very clear, this code assumes that if you're going to include the -domain parameter, it's always 'named' (i.e. dostuff computername arg2 -domain domain); this also makes the computername parameter mandatory.

Function DoStuff(){
    param(
        [Parameter(Mandatory=$true)][string]$computername,
        [Parameter(Mandatory=$false)][string]$arg2,
        [Parameter(Mandatory=$false)][string]$domain
    )
    if(!($domain)){
        $domain = 'domain1'
    }
    write-host $domain
    if($arg2){
        write-host "arg2 present... executing script block"
    }
    else{
        write-host "arg2 missing... exiting or whatever"
    }
}
like image 32
nimizen Avatar answered Oct 05 '22 11:10

nimizen


Not sure I understand the question correctly.

From what I gather, you want to be able to assign a value to Domain if it is null and also what to check if $args2 is supplied and according to the value, execute a certain code?

I changed the code to reassemble the assumptions made above.

Function DoStuff($computername, $arg2, $domain)
{
    if($domain -ne $null)
    {
        $domain = "Domain1"
    }

    if($arg2 -eq $null)
    {
    }
    else
    {
    }
}

DoStuff -computername "Test" -arg2 "" -domain "Domain2"
DoStuff -computername "Test" -arg2 "Test"  -domain ""
DoStuff -computername "Test" -domain "Domain2"
DoStuff -computername "Test" -arg2 "Domain2"

Did that help?

like image 33
user983965 Avatar answered Oct 05 '22 11:10

user983965