Im fairly new to powershell and am starting to write my own functions. I am trying to figure out the writing of parameters in a function. I always see the -property parameter in the powershell cmdlets.
Is there anyway to duplicate a -property parameter in my functions? I cant find anything anywhere on this. I had thought about a parameter that does a select-object but i cant seem to make it work.
Any help would be appreciated
function foo($string,$string2,$string3){
write-host $string write-host $string2 write-host $string3}
"foo a b c" would outputabc"foo -string a -string2 b -string3 c" would outputabc
The neat thing is that you can scramble the order you input parameters."foo -string2 b -string a -string3 c" would outputabc To create a switch usefunction foo([switch]$string){ if($string){write-host "String switched on"}}
The function will only return something if you write "foo -string"
It is generally accepted best practice starting with PowerShell v2 to use advanced function sytnax like this:
function foo(){
param(
[string]$string,
[string]$string2,
[string]$string3
)
It accomplishes the same goal but there are many other things you can do using this technique. You can read more about it by looking at the topic about_advanced_functions:
http://technet.microsoft.com/en-us/library/dd315326.aspx
Thanks. Ill give this a shot and see what i can come up with.