Sunday, February 16, 2014

Scripting with Microsoft's PowerShell - Advanced

Lets sharpen our PowerShell scripting skills. Today we will be looking at a script and going through it. Here is the script:
  1. write-host
  2. write-host "Which option would you like:"
  3. write-host 1: "Choice 1"
  4. write-host 2: "Choice 2"
  5. write-host 3: "Choice 3"
  6. write-host 4: "Choice 4"
  7. write-host 5: "Exit."

  8. $userinput = [Console]::ReadLine()

  9. Function switchUserInput ($userinput)
  10. {
  11. switch ($userinput) 
  12. 1 {write-host "You chose option 1"} 
  13. 2 {write-host "You chose option 2"} 
  14. 3 {write-host "You chose option 3"} 
  15. 4 {write-host "You chose option 4"} 
  16. 5 {exit} 
  17. default {"You must enter a number between 1 and 5"}
  18. }
  19. }

  20. switchUserInput($userinput)

So lines 1-7 provide the user with options/choices to pick from. Line 9 waits for the user to enter a key. Once the user enters the key, we check what key they entered on line 24.

Line 24 calls a function that is defined on line 11. We need to define the function we want to use before we use it so we defined it first here. The function is called "switchUserInput" and it takes a variable named "$userinput". Once the function is called and ran, it puts the variable inside a Switch statement. Based on the value of what $userinput is, it executes the command. You can have it written to the screen or have it execute another function.

The below image is what the script looks like when ran:


In order to make this script slightly better, we can make it so the user has to exit the script instead of auto-exiting.

In order to do this, we just have to expand the function. Add this after the switch statement:
  1. write-host "Would you like to return to main menu?"
  2. $choice = [Console]::ReadLine()
  3. if(($choice.ToLower() -eq "yes") -or ($choice.ToLower() -eq "y"))
  4. {
  5. write-host
  6. write-host "Which option would you like:"
  7. write-host 1: "System related."
  8. write-host 2: "Group related."
  9. write-host 3: "Server task related."
  10. write-host 4: "Query related."
  11. write-host 5: "Exit."
  12. $mainMenuReturn = [Console]::ReadLine()
  13. switchUserInput($mainMenuReturn)
  14. }
  15. elseif (($choice.ToLower() -eq "no") -or ($choice.ToLower() -eq "n"))
  16. {
  17. exit
  18. }
  19. else
  20. {
  21. write-host
  22. write-host "You did not enter a valid option."
  23. write-host "Main menu: "
  24. write-host
  25. write-host "Which option would you like:"
  26. write-host 1: "System related."
  27. write-host 2: "Group related."
  28. write-host 3: "Server task related."
  29. write-host 4: "Query related."
  30. write-host 5: "Exit."
  31. $mainMenuReturn = [Console]::ReadLine()
  32. switchUserInput($mainMenuReturn)
  33. }

So line 1 we ask them if they want to return to the main menu, then line 2 we wait for their input and store it in a variable named "$choice". We then have an IF statement testing what they entered. We do not want to be case sensitive here so we convert their answer TOLOWER. If they entered "yes" or "y”, then we post the choices again in lines 5 through 11. We then wait for their answer and store it in "$mainMenuReturn". We then call our function: "switchUserInput" and pass it the appropriate variable.

If the user enters "no" or "n", then we simply exit the script.

If they enter something besides "yes", "y", "no" or "n", then we wait to tell them that they entered the wrong choice and repeat the choices for them.

Now our script is complete! We can now provide a menu of choices for the user and recursively run until the user does not want the script running anymore.

No comments: