Manage Learn to apply best practices and optimize your operations.

Tap PowerShell DSC resources to automate web server construction

Administrators will create numerous IIS web servers during their IT careers. With Desired State Configuration, admins can automate this task with cleaner results.

IT departments use automation to complete repetitive tasks, looking to save time and reduce errors. Server provisioning...

is a perfect candidate for automation because the golden image, with the software and configuration settings for that server, remains the same at each new deployment.

Desired State Configuration (DSC), a server provisioning and configuration management technology, allows an administrator to define common settings in a configuration script, which is then applied as many times as needed, whether once or thousands of times, to the same end. There are dozens of PowerShell DSC resources to help build a DSC configuration and expose properties that the administrator can configure.

Building a web server is a common task for administrators, and Internet Information Services (IIS) is the typical web server used by many organizations to host websites. Here's how to apply the DSC automation technology to this common IT problem -- building an IIS web server multiple times -- that will save an organization time and money.

How to use DSC to install IIS

The steps described in this article enable a reader to use DSC to install IIS, set up a single website and create an app pool for the website.

The machine designated for the IIS web server must run Windows Management Framework (WMF) version 5. Technically, v4 will work, but v5 introduced several improvements to DSC and should be the version of choice for enterprise users.

With WMF 5.0 installed, define the machine's configuration. DSC can apply many configuration changes.

Next, see which PowerShell DSC resources are available to make the changes. Since IIS is a Windows Server feature, use the built-in WindowsFeature DSC resource. When IIS is installed, pull in a couple more DSC resources from a single DSC module called xWebAdministration from the PowerShell Gallery.

Find-Module xWebAdministration | Install-Module

See which DSC resources are available in the xWebAdministration module with the Get-DscResource cmdlet (Figure 1):

Get-DscResource -Module xWebAdministration

Available DSC resources in xWebAdministration module
Figure 1: Retrieve a list of the DSC resources available in the xWebAdministration module with the Get-DscResource command.

This article deals with the xWebSite and xWebAppPool DSC resources.

Use Get-DscResource again to see what attributes to pass to each of these DSC resources with the Syntax parameter:

Get-DscResource -Name xWebSite,xWebAppPool  -Syntax

Next, apply company-specific values to the attributes. Create a DSC configuration to import the required modules: WindowsFeature, xWebSite and xWebAppPool. With the modules imported, define each resource along with the necessary attributes that define the desired configuration.

The following script builds, executes and applies the configuration for the web server (Figure 2).

InstallWebServer.ps1

====================

Configuration CompanyXWebServer

{

    

     Import-DscResource -Module PSDesiredStateConfiguration, xWebAdministration

    

     Node 'localhost' {

        WindowsFeature 'InstallIIS' {

            Ensure = 'Present'

            Name = 'Web-Server'

        }

 

        xWebSite 'DefaultCompanyXSite' {

            Name = 'CompanyXSite'

            BindingInfo = @( MSFT_xWebBindingInformation

                {

                    Protocol = 'HTTP'

                    Port = 81

                }

            )

            PhysicalPath = 'C:\inetpub\wwwroot'

            ApplicationPool = 'CompanyXAppPool'

            DependsOn = '[xWebAppPool]DefaultCompanyXAppPool'

        }

 

        xWebAppPool 'DefaultCompanyXAppPool' {

            Name = 'CompanyXAppPool'

        }

    }

}

 

## Execute the configuration to create the MOF file

CompanyXWebServer

 

## Invoke the LCM to start applying this configuration

Start-DscConfiguration -Wait -Force -Path ".\CompanyXWebServer" -Verbose

Installed IIS
Figure 2: The script builds, installs and configures the IIS web server.

In this example, the Local Configuration Manager settings are at default, which means that this configuration will run every 30 minutes, and report via the event log whether or not it's still in compliance.

Next Steps

Manage Windows Server with Desired State Configuration

Learn the primary functions of PowerShell DSC

The fundamentals of PowerShell DSC admins should know

This was last published in March 2017

Dig Deeper on Windows PowerShell Scripting

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What Desired State Configuration resources have you used in an IT environment?
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close