Create a function with optional call variables

后端 未结 3 373
南笙
南笙 2021-01-01 11:39

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 bein

3条回答
  •  攒了一身酷
    2021-01-01 11:58

    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?

提交回复
热议问题