Skip to content
Kevin Callahan edited this page Aug 23, 2016 · 12 revisions

Desired State Generator

If you are interested in using PowerShell Desired State Configuration to manage your web server configuration, the DSC Generator can help you get started. You can run the DSC Generator on an existing server and it will generate ready-to-run DSC configuration code for your IIS App Pools, Sites, and Virtual Directories. For example:

configuration IIS_DSC  # Generated by DSC Generator v2.0.1
{
	 Import-DscResource -ModuleName xWebAdministration

	 node localhost
	 {
		  $logFilePath = 'D:\IISLogs'
		  $appPoolRecycleHour = '02'
		  
		  #================== 'Default Web Site' site definition ==================
		  xWebAppPool DefaultAppPool_Pool
		  {
			   Name = "DefaultAppPool"
			   AutoStart = $true
			   ManagedPipelineMode = "Integrated"
			   ManagedRuntimeVersion = "v2.0"
			   IdentityType = "ApplicationPoolIdentity"
			   Enable32BitAppOnWin64 = $false
			   RestartSchedule = @($appPoolRecycleHour+':00:00')  # overriding @('04:00:00')
			   IdleTimeout = "00:00:00"
			   RestartTimeLimit = "00:00:00"
		  }

		  xWebSite DefaultWebSite_Site
		  {
			   Name = "Default Web Site"
			   Ensure = "Present"
			   State = "Started"
			   ApplicationPool = "DefaultAppPool"
			   PhysicalPath = "%SystemDrive%\inetpub\wwwroot"  # This folder must already exist
			   LogPath = $logFilePath  # overriding "D:\IISLogs"
			   DependsOn = "[xWebAppPool]DefaultAppPool_Pool"
			   BindingInfo = 
						 @(
							  MSFT_xWebBindingInformation 
							  {
								   Protocol = "http"
								   Port = "80"
								   IPAddress = "*"
							  }
						 )
			   AuthenticationInfo = 
						 MSFT_xWebAuthenticationInformation 
						 {
							  Anonymous = $true
							  Basic = $false
							  Digest = $false
							  Windows = $false
						 }
		  }

		  xWebApplication DefaultWebSite_fooy_App
		  {
			   Name = "fooy"
			   Ensure = "Present"
			   Website = "Default Web Site"
			   PhysicalPath = "C:\Admin\TestingStuff\a2"
			   WebAppPool = "DefaultAppPool"
			   DependsOn = "[xWebAppPool]DefaultAppPool_Pool"
			   AuthenticationInfo = 
						 MSFT_xWebApplicationAuthenticationInformation 
						 {
							  Anonymous = $false
						 }
		  }

	 }
}

cls

# Compile this DSC down to an .MOF:  An .mof file will be placed in the specified directory:
IIS_DSC -OutputPath "c:\my_iis_dsc_dir"

# Apply the DSC.  ALL .mof's in a folder will be executed!:
Start-DscConfiguration -Path "c:\my_iis_dsc_dir" -Wait -Debug -ErrorAction Stop -Force -Verbose  # this will apply the DSC

And the DSC Generator will also generate Desired State Configuration for the Windows features your IIS site needs, e.g.:

      WindowsFeature IIS-HttpLogging_Feature
      {
           Name = 'IIS-HttpLogging'
           Ensure = 'Present'
      }

      WindowsFeature IIS-FTPServer_Feature
      {
           Name = 'IIS-FTPServer'
           Ensure = 'Absent'
      }

The DSC Generated focuses on generating commonly used parameters. But its easy to add additional parameters to the DSC Generator or just modify the generated DSC by hand if you only have a few sites.

Modules you'll need to run the generated DSC can be found here

example

Clone this wiki locally