Powershell Quick Parameters for Scripts

Steve Jones, 2015-02-19

I was working on a script recently to manage a particular process and wanted to make it generic by allowing the user to pass in a parameter. I have seen lots of examples, especially those that work with SQL Servers, using text files and other items as parameters, but in this case I wanted an easy, quick, command like parameter.

This post looks at what I chose to check parameters. I had a couple requirements.

  • display message if no parameter is passed in.
  • display some help if /? is passed in.

I know that my cmdlets should contain help from the PoSh command line, and I’ll get to that. For now, I’m managing things the way I was taught when I wrote C. A /? should get me help.

$Args

I did a little research on parameters and found a few things, but decided to use the $args variable. This is an array of undeclared parameters. I grab the first value (the only one I care about like this.

$instance = $args[0]

Note the [0]. As with many things in Computer Science, we’re zero based arrays.

I could allow for other parameters, but this gets me what I want.

Testing

The test for /? is easy. That’s like this:

if ($instance -eq “/?”) {
  write-host “Please enter the instance you wish to detach all databases from as a parameter.”
  }

If this is equal to my help request, write something out.

Next I needed to add another test. In this case I found that I could easily look for NULL variables, or blanks, with the !. As in this:

if (!$instance -or $instance -eq “/?”) {
  write-host “Please enter the instance you wish to detach all databases from.”
  }

That worked well and lets me remind myself if I’ve forgotten to pass in a parameter. The one thing I experimented a few times with was the OR clause. I tried these, none of which worked:

  • if (!$instance OR $instance -eq “/?”) {
  • if (!$instance) or ($instance -eq “/?”) {
    if (!$instance) -or ($instance -eq “/?”) {

A little experimenting got me to remember that PoSh is fairly consistent, and the plain -or should work inside the parenthesis.

Everything Else

When I first ran this without a parameter, my script froze. That’s because I hit the IF clause, wrote out the message, and then executed.

Fortunately I’ve done this type of stupid programming before, so I added this:

if (!$instance -or $instance -eq “/?”) {
  write-host “Please enter the instance you wish to detach all databases from.”
  }
else {

The rest of my script fits in the else clause.

Reference

Filed under: Blog Tagged: powershell, syndicated

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis

2009-02-23

1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren

2009-02-17

1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren

2009-02-13

360 reads