Monday, November 25, 2013

Microsoft Virtual Academy: PowerShell M03

Today I continue forward on my goal of completing Microsoft's Virtual Academy's PowerShell 3.0 training.
(http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start?o=3276#?fbid=aVs9FfAH2DJ)
(rewritten based on my notes available here:
https://drive.google.com/file/d/0B1fwreWrAZioQ0twZXB3b0J2eXM/edit?usp=sharing)

Module 3 "The Pipeline"consists of 3 video segments totaling ≈ 30 minutes along with two PowerPoints of 12 and 10 slides.

This module was short and sweet, teasing you with the power of Powershell while also showing you how to protect yourself from destroying your system OS.

To start of the module we learn what is the pipeline (|) which if you are wondering is the key above the enter on keyboard (shift + \).   Now what is the benefit of the pipeline in Powershell, well is allows the connecting of cmdlets to accomplish a larger task. In the simplest form it passes the results of one cmdlet to an other to be processed.

  • get-service -name bits | stop-service   is the same as    stop-service -name bits
Now this is the simplest form and may be hard to see the benefit in above case but it is just for reference. Now as you notice with the above example couldn't I just send all services to stop and yes you could, though you would in essence be crippling/bricking your system as Powershell by default acts on all commands without questioning you.  This is were the last part covered is important.  Powershell provides ways to safeguard yourself with -whatif and -confirm.
  • -whatif can be added to almost every cmdlet statement to see output results of the cmdlet without actually executing it
  • -confirm will prompt you if you wish to execute (take note of Yes, Yes to all, No, Not to all)
One of the huge benefits brought to light in this module is auto loading of modules which was not possible pre-v3.  In addition to PowerShell v3 auto loading modules for you it also has a complete understanding of the modules help (requirement is module must be installed on the system).  In pre-v3 you had to mount a snap-in before you could use the cmdlets or even have help understand them.  This is a huge time saver and will reduce frustration. 

Lastly they touched on features of exporting, importing and some comparing. They didn't go into to much detail as they will be covering this later in the series.
  • Export to many popular formats
    • csv, xml
  • Import files back in for processing
  • Ability to compare file to running system
    • In the module they demonstrated making a known good xml file of system process and then comparing to another system
    • Great way to see what has changed from baseline of systems
    • I will be creating baselines for all system types when I re-image next time for future troubleshooting 
      • Get-process | export-clixml –path C:\good.xml
        • creates the xml
      •  Compare-Object -ReferenceObject (Import-Clixml C:\good.xml) -DifferenceObject (Get-Process) -Property name
        • compares the baseline created before to current running process names
Until next week.

Friday, November 22, 2013

Microsoft Virtual Academy: PowerShell M02

Today I continue forward on my goal of completing Microsoft's Virtual Academy's PowerShell 3.0 training.
(http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start?o=3276#?fbid=aVs9FfAH2DJ)
(rewritten based on my notes available here:
https://drive.google.com/file/d/0B1fwreWrAZioNW5OZzFLTmFHTzA/edit?usp=sharing)

Module 2 "The Help System"consists of 3 video segments totaling ≈ 50 minutes along with a PowerPoint of 10 slides.

The first 13 minutes are more of an overview of help in PowerShell v3 and older systems.  During this 13 minutes they cover how to update help, briefly explain the difference between get-help, help, and man and their biggest point learn to discover.

One of the biggest improvements to PowerShell v3 is the ability to update outside of product releases.  By using the cmdlet update-help PowerShell v3 will download current update files from the internet.  If your system is not internet connected you can use save-help from an internet connected system to save the help updates for offline update (http://technet.microsoft.com/en-us/library/hh849724.aspx). Now v2 doesn't support this feature but does have a way for you to ensure you view the most recent help information.  Simply add the parameter -online to your PowerShell v2 get-help statement:

  • get-help get-service -online
    • will open a browser window to must current help for that cmdlet


Next, they explain the difference between get-help, help and man (well help and man are the same just aliases).

  • Get-help
    • partial help file displayed
    • focus is at the end of the file, requiring you to scroll up to see everything
  • help / man
    • displays full help file
    • allows paging through the file one screen at a time
Lastly the biggest point to take away from Module 2 is learn to discover not memorize.  By learning how to use the help cmdlet you set yourself up to discover functions easier and quicker then trying to remember the thousands of cmdlets. There are over 96 verbs alone (get-verb | measure).

The remaining time is spent going over PowerShell syntax to include wildcards, how to navigate the CLI, reading a get-help and more.  One of the features they don't mention until near the end is the use of tab completion.  I feel this should be more towards the beginning as it is a huge time saver.  As you are type cmdlets you can press tab to cycle forward through matching cmdlets or shift-tab to cycle backwards. In addition to tab completion they mention using semi-colon (;) as a cmdlet separator allowing more then one cmdlet statement to be written at a time.  By now you may have already noticed some of the ways to navigate the CLI, such as moving left and right with the arrows but did you know you can recall past cmdlets by pressing up and cancel the current cmdlet by pressing ESC?

Now one of PowerShell's greatest features is the use of  wildcards (*).  Basically the asterisk (*) states allow anything before or after my location.

  • get-help *service*
    • returns all cmdlets with service anywhere in them
  • get-help g*service
    • returns any cmdlets the start with g and contain service within them
To further breakdown the help system they go into explaining the segments/sections of get-help: Name, Synopsis, Syntax, Description, Related links, and remarks.  I will let you check out the video for their descriptions of these.  Next they covered some of the parameters that can be used with get-help:

  • -detailed
    • more detail then just get-help
    • includes listing of cmdlet parameters and examples
  • -examples
    • displays just the cmdlets examples
  • -full
    • you guessed it this displays it all
  • -showwindow
    • new parameter in v3
    • opens a window displaying the cmdlet help information
    • can be configured to display only certain sections via the settings
    • great for copying and pasting syntax
    • over a find/search function to current displayed help file

Syntax explained?!?!
By now you have seen a few cmdlets using the get-help/help cmdlet.  What does all the information after the cmdlet mean/do.  Well they are call parameters and they allow the refining/altering of the cmdlet.   Simple enough right.  Well there is a certain format/order that these parameters may need to applied.

  • First there is the [ ] this one has two meanings depending on its placement
    1. At its top most level it means optional parameter.  This can be seen in most cmdlets as most parameters are enclosed in [ ].  But further review and you will find some cmdlets that over optional content within a parameter
      • [[-name] <string[]>]
        • This whole parameter is optional but if you choose to use it you can with out first calling -name and instead providing the string value
    2. Deeper is the defining of multiple values separated by commas
      • [[-name] <string[]>]
        • for the string value we could define: bob, rob, *ob
          • (Yes you can include wildcards)
  • Next there is the use of < > as you may have already noticed this typically denotes format the parameter is looking for arguments/values
    • string = alphanumeric
That concludes module 2. If you stop here you will already have placed yourself ahead of many other users.  Just remember to learn to discover.