functions

write a function, put it down.  All the day you’ll wear a frown.  Write a function, load it up, all the day you’ll have good luck.

Load the function, and what happens?  Nothing.  Unless the last line of the script that loads the function is written to call the function, or it writes a message to the console on usage or writes a ‘help’ message.  the function is (probably) loaded – and we could be doing this from your profile so we don’t have to explicitly load the function – and we can call it.  It will then do whatever we wrote it to do. And again, and again.

We could have written the function to expect input – and if we make a parameter required (in the param block), powershell will prompt us for it visually.  We can expect parameters to be added by position or by referencing them as named parameters, but we can also provide default values for the parameters. “You can tell me the computername but if not, i will use localhost as the computer”.

We also could write the function to look to the args array – these are the command-line entries after we call the function.  They are stored in args[] and we can retrieve them as args[0], [args[1] and see how many with args.count.  Best practive is not to use both named parameters and args in the same script or function. 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.