Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from   Instant Windows PowerShell Guide Enhance your knowledge of Windows PowerShell and get to grips with its latest features

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Length 86 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Harshul Patel Harshul Patel
Author Profile Icon Harshul Patel
Harshul Patel
Arrow right icon
View More author details
Toc

Executing Desired State Configuration (Advanced)


DSC approaches declarative syntax that describes what needs to be done rather than covering imperative syntax that specifies how a task can be performed.

Getting ready

To use DSC, first define a desired configuration. Like functions, configurations in DSC can be defined in the Windows PowerShell language by using the Configuration keyword and stored in script (.ps1) or module (.psm1) files. Also, similar to functions, configurations need to be defined and then run.

How to do it...

  1. To use a configuration, invoke the Configuration block the same way you would invoke a Windows PowerShell function, passing in any expected parameters you have defined (two in the preceding sample). For example, in this case, the MyWebConfig configuration can be invoked as follows:

    PS C :\>MyWebConf -MachineName $env:COMPUTERNAME –WebsitePath \\PSShare\MyWebSites
    

    This will create a folder with the same name as your configuration name and will contain our MOF output file.

  2. The following command creates an MOF file known as the configuration instance document. Path represents the target directory where your MOF files are located. Wait causes the execution of the DSC resources to run in the background, that is, an interactive process.

    PS C :\>Start-DscConfiguration -Path .\ MyWebConf –Wait –Verbose
    

How it works...

Each Configuration block must have at least one Node block. Each Node block can have one or more resource provider blocks. You can use the same role provider more than once in the same Node block.

In addition to new language keywords, DSC includes the following set of CMDLETs for managing configurations:

  • Start-DscConfiguration: This CMDLET deploys a configuration to one or more target nodes and applies the configuration on those nodes by using the local configuration manager

  • Get-DscConfiguration: This CMDLET returns the current configuration from one or more target machines:

    PS C :\>$Sess = New-CimSession -ComputerName localhost
    PS C :\>Get-DscConfiguration –CimSession $Sess
    
  • Restore-DscConfiguration: This CMDLET restores the current configuration from one or more target machines:

    PS C :\>$Sess = New-CimSession -ComputerName localhost
    PS C :\>Restore-DscConfiguration –CimSession $Sess
    

There's more…

There is one more CMDLET that helps to detect the configuration drift:

  • Test-DscConfiguration: This CMDLET checks for one or more target nodes and returns a Boolean value indicating whether the current desired state matches the actual state. Have a look at the following command:

    PS C :\>$session = New-CimSession -ComputerName localhost
    PS C :\>Test-DscConfiguration –CimSession $session
    

    This will either return True when the current and actual configuration matches or False if there's a mismatch

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image