PowerShell and Desire State Configuration (DSC) – The Basics

With all the latest Microsoft releases it’s apparent that PowerShell and Desire State Configuration (DSC) is at the core of build, releases, and configuration management. If you have not learned PowerShell, now is the time to get on board. DSC is a recent practice that was released in early 2013. The DSC uses PowerShell’s core features as its foundational building blocks.

Since PowerShell is at the foundation of DSC, we first need to start with the basics of PowerShell. Those of you that already know PowerShell, this blog post might be a good overview.

What I will be covering in this post:

Some basic scripts and objects that are helpful to know when you are getting started.

Want I will not be covered in this post:

I will not be covering the DSC. I will be covering DSC in later posts, in what I am calling the Powerbits series.

PowerShell it is a very large topic and cannot be covered in one single post. There are a number of great PowerShell resources available. I have listed a few of my favorite resource links that might be helpful.

· Learn PowerShell 3 in a Month of Lunches (2nd ed.)

· Getting Started with Windows PowerShell Workflow

· Windows and Windows Server Automation with Windows PowerShell

· Quick Reference for Server Core Tasks

The Basics:

One of the first objects that is different from the good old batch scripts is the PowerShell Pipeline. Here is an example:

 

Exploring Objects:

Get-Item C:scripts | Get-Members

Get-items retrieves the info from C:Scripts and passes (using the | ) the output which is an object to the Get-Member.

The Get-Members tells us that the type of object we are examining is System.IO.DirectoryInfo. Which is a .Net Class. This can be seen in the Member Type column in the output.

 

Next is the PowerShell Variables. As you can image, they are like any other variable with some small differences.

Variables:

  • Variables can be strongly typed by using [String]$MyVariable or [Int] $MyNumberedVariable
  • When a variable contains a space, it has to be enclosed in curly braces ${Version Number}. Best practices is to avoid using variables with spaces.
  • New-Variable cmdlet can be used to create new variables. The –Name parameter of this cmdlet becomes the variable name of the argument you specify for the –Value parameters get assigned to the variable.
    • New-Variable –Name ShellVersion –Value 4.0
      • Note the Value does not prefix the $

SS1

 

  • Automatic Variables are created by PowerShell to store the state of PowerShell. For example the $PWD is an automatic variable that represents the current directory

ss2

Other automatic variables are: $PID and $PHome. $PSVersionTable examines the Windows PowerShell.

ss4

 

  • $VerbosePreference variable is used to define the behavior in PowerShell. By Default, the SilentlyContinue message logging is set. Which means that the cmdlet won’t produce a verbose logging, unless the –Verbose switch parameter is explicitly specified. With the $VerbosePreference set to Continue, the cmdlet with verbose logging produces the verbose message automatically.

ss5 ss2

 

  • Environment variables store information related to the operating system environment. The $Env:Path gives the value of path environment. $Env:Computername hostname

As you can see, PowerShell is a large topic with vast capabilities. As you become more comfortable with PowerShell, I know you will soon discover that is a critical tool for the toolbox.

Next week I will be covering the additional install for Azure and DSC. So check out next week’s Powerbit, when a byte is too much and bit is all you need.

Originally published at Northwest Cadence by ALM Consultant Bryon Root.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s