Using Functions with Optional and Required Parameters

Just a quick example of how to pass parameters to a powershell function. This works also as script parameters…

#——————–
function test{
Param($inOptional1=”Hi”,$inRequired1=$(throw “inRequired1 is a required input”),$inOptional2=$null)
Write-Host $inOptional1
Write-Host $inRequired1
Write-Host $inOptional2
}
“——————-”
test “Hello” “World” “Lovely day”
“——————-”
test -inOptional1 “Hello” -inRequired1 “World”
“——————-”
test -inOptional1 “Hello” -inRequired1 “World” -inOptional2 “Lovely day”
“——————-”
test -inRequired1 “World”
“——————-”
test -inOptional1 “Hello”
“——————-“

Advertisement

5 thoughts on “Using Functions with Optional and Required Parameters

  1. AJ December 20, 2012 / 4:00 pm

    Shalom Andrew,
    I need to open from jscript SCCM – which I succeed.
    My problem is that I need the SCCM to open to connection mode and fill in a parameter.
    meanwhile I succeeded copying to the clipboard the text.
    So the user needs to press file->connect and press ctrl+V.

    is there a way to send parameters on opening the SCCM (jscript opens with “location /app-v version” ) and it will open the file-> connect and put the parameter for connection?

    Thanks a lot,

    AJ

  2. AJ December 22, 2012 / 7:46 pm

    Instead of SCCM It’s remote Configuration Manager 2007 Remote Access.
    Sorry for confusing. If I understood correctly it’s a program added through the SCCM.

    • andrewdcraig December 22, 2012 / 10:07 pm

      Hi AJ, do you mean rc.exe? I do have a custom version of this somewhere and I’ll dig it out in the new year.
      One thing to mention about rc.exe is that when used standalone it does not log to sccm log files like it does when accessed from the console. The audit trail from the console can be pretty useful and if you don’t use it like that then it only logs to eventvwr on the remote machine which is pretty useless…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s