Monday, December 23, 2013

Microsoft Virtual Academy: PowerShell M05

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/0B1fwreWrAZioUWpaeFNSTFVXUEU/edit?usp=sharing)

Module 5 "Te pipeline:deeper" consists of 3 video segments totaling ≈ 45 minutes along with a PowerPoint of 12 slides.

The focus of this segment is on the pipeline ( | )

As briefly mentioned in past segments the pipeline allows us to pass objects along a string of cmdlets. The pipeline has four (4) ways to be used:

  1. ByValue
  2. ByPropertyName
  3. Customized properties
  4. Parenthetical
First and most common use is through the ByValue. In order to use ByValue we need to know the following: type of object we sending and if the receiving cmdlet can accept it.  To determine the type of our object we use get-member (gm).  Know to validate the receiving cmdlet we run a get-help [cmdlet].  There are two (2) things we are looking for in the help file.  First, if any parameters accept pipeline input and second if any parameters accept our object type.  

Example: Get-Service | Stop-Service
  1. Determine object type: 
    • Get-Service | gm
    • Results: TypeName: System.ServiceProcess.ServiceController
  2. Verify receive cmdlet will accept
    • Get-Help stop-service -full
      • If using v3 use -showWindow and utilize the search feature
    • Verify accept pipeline input: true (.., ByValue,..)
    • Verify input value is sending object type
      • -InputObject <ServiceController[]>
Hint:  if the nouns match most likely will work.


Next we move to the ByPropertyName.  Works very similar to ByValue but is more forgiving.  We follow the same process of verify.  This process works on the concept of a property of sending object matching a parameter in receiving cmdlet.


Example: Get-Service | Stop-Process
  1. Determine object type: 
    • Get-Service | gm
    • Results: TypeName: System.ServiceProcess.ServiceController
  2. Verify receive cmdlet will accept
    • Get-Help stop-process -full
    • Verify accept pipeline input: true (.., ByValue,..)
    • Verify input value is sending object type
      • -InputObject <Process[]>
        • Not the same type so now we look for ByPropertyName
      • -Name <String[]>
        • Will accept input from sending object's property name and process based on that
So what if ByValue and ByPropertyName don't work?  We can create a custom property.  This is accomplished by creating a custom column (calculated property).  With a custom property are goal is to meet the ByPropertyName process.  In the example below and in segment we try to send ADComputer to Get-Service.

Command used:  @{name=’[propertyname]’;expression={$_.[property]}}

Example: Get-ADComputer -filter * | Get-Service -name bits

  1. Determine object type: 
    • Get-ADComputer | gm
    • Results: TypeName: Microsoft.ActiveDirectory.Management.ADComputer
  2. Verify receive cmdlet will accept
    • Get-Help Get-Service -full
      • ByValue: -InputObject <ServiceController[]> - No Go
      • ByPropertyName: -ComputerName (for systems), -Name (for services)
        • We want to use the -ComputerName and not -Name, this is where custom property comes in
  3. Map ADComputer -Name to -ComputerName
    • @{name=’ComputerName’;expression={$_.name}}
  4. Verify Object Property
    • Get-ADComputer | gm
      • Should now see a property called ComputerName

Finally we use Parenthetical when all else has failed us.  Command process what is in parenthesis first.

For this example we want to pull all BIOS details for all ADComputers.

Example: Get-ADComputer –filter * | get-WMIObject –class win32bios

  1. Determine object type: 
    • Get-ADComputer | gm
    • Results: TypeName: Microsoft.ActiveDirectory.Management.ADComputer
  2. Verify receive cmdlet will accept
    • Get-Help Get-WMIObject -full
      • Notice there is no pipeline support
As we see WMIObject does not support pipeline.  To work around this we use -ExpandProperty.  -ExpandProperty will expand a single property into a string object. There are two ways to accomplish this dependent on version you are running.

Command used: Get-adcomputer –filter * | select –ExpandProperty name

If we run this through gm we find object type is no a string.

  • v2 and v3
    • Get-wmiobject –class  win32_bios –ComputerName (Get-adcomputer –filter * | select –ExpandProperty name)
  • v3 only
    • Get-wmiobject –class  win32_bios –ComputerName (Get-adcomputer –filter *).name
That wraps up segment 5.  I hope to have segment 6 up next week but dependent on the holidays and work it may not be up until Mid-January. 

I hope everyone has Happy Holidays.

No comments:

Post a Comment