Wondering mind

The thoughts of a Microsoft Premier Field Engineer on anything and everything SharePoint.

Arguments what are they good for

Arguments what are they good for

  • Comments 1
  • Likes

If your like me you have slowly (or maybe quickly) been getting frustrated with using arguments in your PowerShell scripts, they have none of the nice built in functionality that function parameters have like Named,  Mandatory, etc

On the Pro side I am grateful that they are collected into an array so that I can easily manipulate them.  

Because of this I have recently found myself asking what are arguments good for?  If you think about it arguments are a quick way to get one to many variables into your script, and really it is simple enough for admins of all knowledge levels to use.  However they don't present any improvements from the VBScript days which makes me wonder why not.

I believe that it comes down to one of the following elements writing style, or needed functionality. 

Writing Style:

Arguments have been made common place and popular, many admins will feel comfortable using them in PowerShell scripts since they used them in there VBScripts and batch files.  I don't see a caveat, if your comfortable in your writing style and using arguments then go for it.  However if you want to step out of your comfort zone I think you will find that there are other great choices that could replace arguments for you in the future.

Needed functionality

What is your script doing?  Who is using the script? Is it for an automation project? 

Arguments are really only handy if you enter all the arguments and in the correct order which means it lends itself to batching but not actual human interaction as well optional variables could be troublesome.  But if you need to have a user enter the info ie User Creation you can write a function like the following to create an Input Box which will present your users with an input box where they can enter the info and you can present reminders to what info you are looking for, or maybe your skillset is set at a lower bar then I would suggest using a line like this

 

$ServiceAccountUserName = Read-Host "Please enter Username for Excel Service:"

 

Using Read-Host forces the script to stop and wait for data entry, and if you want to get really fancy you could use Write-Host to give further details to your user like this.

 

Write-Host "This account name is required and should already be created in Active Directory."

Write-Host "Please enter in the format Domain\Username"

Write-Host "`r"

$ServiceAccountUserName = Read-Host "Please enter Username for Excel Service:

 

If you do choose to still use arguments I would suggest that you write a layer to do things like insure your getting all of the required arguments, and arguments were entered in the correct order and in the correct format.

Comments
  • As an alternate solution, we use configuration files to parametrize our scripts using xml files. You don't have to worry about parameter order, xml is easy to use with powershell.

    Using Read-Host to get the parameters makes easier to ensure the script gets all the parameters, but as newcomers to powershell is important to us to triple check the values we are entering.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment