Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

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.

Scripting with Microsoft's PowerShell - Beginner

We will be taking a step into the world of scripting with PowerShell. PowerShell scripts file extension end with ".ps". They are normal text documents and can be edited in any text editor.

Here are some instructions on creating and writing your Hello World for PowerShell


  1. Create a text file and name it "Hello.ps1"
  2. Open up this file in a text editor of your choosing.
  3. Write inside of this text file: "write-host "Hello World.""
  4. Open up PowerShell 
  5. Make sure your default location is set to the same folder of the location of the file. You can also drag and drop the file to your PowerShell window and press enter.
  6. Once the command runs, you will see it write Hello World.

If you are running PowerShell for the first time, you may see this:


If you see this error, this means that you have unsigned PowerShell scripts set to restricted or as in they cannot run. In order to check this, type: "GET-ExecutionPolicy".

If it returns "Restricted", then this means you need to change the Execution Policy.


In order to do this, type "SET-ExecutionPolicy remotesigned". Before you do this, you will need to re-open PowerShell with Administrative Privileges.

Once again check to see what the policy is by typing: "GET-ExecutionPolicy". Verify it does not say "Restricted".

In order to return the settings back to the original settings, simply type: "SET-ExecutionPolicy restricted"

Congratulations, you just wrote your first PowerShell script!

Saturday, February 15, 2014

Basics of Microsoft's PowerShell

Window's PowerShell (PS) is Microsoft's task automation and configuration management framework, consisting of a command-line shell and associated scripting language built on .NET Framework. Today I will be going over a few basic commands you can run immediately to get you going into exploring the full capabilities of PS.

Starting with the GET commands try going to the Start pearl in the lower left hand corner of Windows 7 and in the search bar type in "Power Shell". Once it is up and running, you can type in "GET-ACL". ACL stands for Access Control List.



The next simple command is just to get the date. Type in "GET-DATE". Now this may  seem like a simple function that serves no real use, but when you start writing scripts and you want the script to perform a particular action at a certain time, you'll need to call this function and store the data, then write an IF/ELSE statement to handle it. This gives you more control over your Operating System and within your Script.



If you are unsure on what commands are available or need help, you can type "GET-HELP".


Instead of using Windows Task Manager which lists all the currently running processes, you can run "GET-PROCESS" to dump a listing of currently running processes and you can save this data in a text file. In your script, you can say something of the nature of "IF problem GET-PROCESS". This is, of course, pseudocode.


If you would like a full listing of a specific kind of command, you can use regular expression within GET-HELP. You can type something such as "GET-HELP-Name Get-*" and this will dump out a full listing of all Get commands.



Just like dumping out what the currently running processes are, you can also dump out the currently running services and their current state. Use the command "GET-SERVICE" to get a listing of this.


I did not jump into the scripting capabilities, yet. I will have another posting for diving into the scripting facet.