### Create IIS website with WebAdministrationDsc (Sample_WebSite_NewWebsite) Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that creates a new IIS website on a single node. It installs the IIS and ASP.NET 4.5 features, stops the default site, copies content from a source path, and starts the new site with an optional SiteId. Requires the WebAdministrationDsc module and parameters for website name, source, and destination paths. ```powershell configuration Sample_WebSite_NewWebsite { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost', # Name of the website to create [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $WebSiteName, # Optional Site Id for the website [Parameter()] [UInt32] $SiteId, # Source Path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $SourcePath, # Destination path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Stop the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' ServerAutoStart = $false PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the website content File WebContent { Ensure = 'Present' SourcePath = $SourcePath DestinationPath = $DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]AspNet45' } # Create the new Website WebSite NewWebsite { Ensure = 'Present' Name = $WebSiteName SiteId = $SiteId State = 'Started' ServerAutoStart = $true PhysicalPath = $DestinationPath DependsOn = '[File]WebContent' } } } ``` -------------------------------- ### Create Web Virtual Directory with UNC Path and Credentials Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebVirtualDirectory Creates a new virtual directory on the Default Web Site using a UNC path that requires credentials. The configuration installs the IIS role, starts the default website, creates the physical directory, and then defines the WebVirtualDirectory resource with a Credential parameter. ```powershell configuration Sample_WebVirtualDirectory_NewVirtualDirectory_WithUncPath { param ( # Target nodes to apply the configuration [System.String[]] $NodeName = 'localhost', # Name of virtual directory to create [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [System.String] $VirtualDirectoryName, # Physical path of the virtual directory [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [System.String] $PhysicalPath, # Credential to use for the virtual directory [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [PSCredential] $Credential ) # Import the module that defines custom resources Import-DscResource -Module PSDesiredStateConfiguration Import-DscResource -Module WebAdministrationDsc Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Start the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Started' PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the virtual directory content File VirtualDirectoryContent { Ensure = 'Present' DestinationPath = $PhysicalPath Type = 'Directory' DependsOn = '[WindowsFeature]IIS' } # Create the new virtual directory WebVirtualDirectory NewVirtualDirectory { Ensure = 'Present' Website = "Default Web Site" WebApplication = '' Name = $VirtualDirectoryName PhysicalPath = $PhysicalPath DependsOn = '[File]VirtualDirectoryContent' Credential = $Credential } } } ``` -------------------------------- ### Create a new web application on the Default Web Site with Windows Authentication Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebApplication Creates a new web application on the Default Web Site. Requires Windows Server 2012 R2 or later. The configuration installs IIS and ASP.NET 4.5, starts the Default Web Site, creates an application pool, clones the wwwroot folder to the destination path, and creates a WebApplication with Windows Authentication enabled and other authentication methods disabled. ```powershell Configuration Sample_WebApplication_NewWebApplication { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost', # Destination path for Website content [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module PSDesiredStateConfiguration Import-DscResource -Module WebAdministrationDsc Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Start the Default Web Site WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Started' PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Create a new application pool for the application WebAppPool SampleAppPool { Ensure = 'Present' Name = 'SampleAppPool' } # Clone the wwwroot folder to the destination File WebContent { Ensure = 'Present' SourcePath = 'C:\inetpub\wwwroot' DestinationPath = $DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]IIS' } # Create a new web application with Windows Authentication WebApplication SampleApplication { Ensure = 'Present' Name = 'SampleApplication' WebAppPool = 'SampleAppPool' Website = 'Default Web Site' PreloadEnabled = $true ServiceAutoStartEnabled = $true AuthenticationInfo = DSC_WebApplicationAuthenticationInformation { Anonymous = $false Basic = $false Digest = $false Windows = $true } SslFlags = '' PhysicalPath = $DestinationPath DependsOn = '[WebSite]DefaultSite','[WebAppPool]SampleAppPool' } } } ``` -------------------------------- ### Create Web Virtual Directory without Credentials Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebVirtualDirectory Creates a new virtual directory on the Default Web Site without credentials. The configuration installs the IIS role, starts the default website, creates the physical directory, and then defines the WebVirtualDirectory resource with a local physical path. ```powershell configuration Sample_WebVirtualDirectory_NewVirtualDirectory { param ( # Target nodes to apply the configuration [System.String[]] $NodeName = 'localhost', # Name of virtual directory to create [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [System.String] $VirtualDirectoryName, # Physical path of the virtual directory [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [System.String] $PhysicalPath ) # Import the module that defines custom resources Import-DscResource -Module PSDesiredStateConfiguration Import-DscResource -Module WebAdministrationDsc Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Start the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Started' PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the virtual directory content File VirtualDirectoryContent { Ensure = 'Present' DestinationPath = $PhysicalPath Type = 'Directory' DependsOn = '[WindowsFeature]IIS' } # Create the new virtual directory WebVirtualDirectory NewVirtualDirectory { Ensure = 'Present' Website = "Default Web Site" WebApplication = '' Name = $VirtualDirectoryName PhysicalPath = $PhysicalPath DependsOn = '[File]VirtualDirectoryContent' } } } ``` -------------------------------- ### Install WebAdministrationDsc from PowerShell Gallery Source: https://github.com/dsccommunity/webadministrationdsc/wiki/Home Installs the WebAdministrationDsc module from the PSGallery repository using PowerShellGet. ```powershell Install-Module -Name WebAdministrationDsc -Repository PSGallery ``` -------------------------------- ### Composite configuration to install PHP on IIS Source: https://github.com/dsccommunity/webadministrationdsc/blob/main/README.md Defines a DSC composite configuration that installs PHP on IIS, including downloading and extracting PHP, installing Visual C++ Redistributable, configuring php.ini, and registering the PHP CGI module. Requires parameters for package folder, download URIs, destination path, and configuration path. Includes commented-out dependency for Windows Server 2012 R2 and below. ```powershell # Composite configuration to install PHP on IIS configuration xPhp { param ( [Parameter(Mandatory = $true)] [switch] $installMySqlExt, [Parameter(Mandatory = $true)] [string] $PackageFolder, [Parameter(Mandatory = $true)] [string] $DownloadUri, [Parameter(Mandatory = $true)] [string] $Vc2012RedistDownloadUri, [Parameter(Mandatory = $true)] [String] $DestinationPath, [Parameter(Mandatory = $true)] [string] $ConfigurationPath ) # Make sure the IIS Prerequisites for PHP are present IisPreReqs_php Iis { Ensure = "Present" # Removed because this dependency does not work in # Windows Server 2012 R2 and below # This should work in WMF v5 and above # DependsOn = "[File]PackagesFolder" } # Download and install Visual C Redist2012 from chocolatey.org Package vcRedist { Path = $Vc2012RedistDownloadUri ProductId = "{CF2BEA3C-26EA-32F8-AA9B-331F7E34BA97}" Name = "Microsoft Visual C++ 2012 x64 Minimum Runtime - 11.0.61030" Arguments = "/install /passive /norestart" } $phpZip = Join-Path $PackageFolder "php.zip" # Make sure the PHP archine is in the package folder xRemoteFile phpArchive { uri = $DownloadURI DestinationPath = $phpZip } # Make sure the content of the PHP archine are in the PHP path Archive php { Path = $phpZip Destination = $DestinationPath } if ($installMySqlExt ) { # Make sure the MySql extention for PHP is in the main PHP path File phpMySqlExt { SourcePath = "$($DestinationPath)\ext\php_mysql.dll" DestinationPath = "$($DestinationPath)\php_mysql.dll" Ensure = "Present" DependsOn = "@[Archive]PHP" MatchSource = $true } } # Make sure the php.ini is in the Php folder File PhpIni { SourcePath = $ConfigurationPath DestinationPath = "$($DestinationPath)\php.ini" DependsOn = "@[Archive]PHP" MatchSource = $true } # Make sure the php cgi module is registered with IIS IisModule phpHandler { Name = "phpFastCgi" Path = "$($DestinationPath)\php-cgi.exe" RequestPath = "*.php" Verb = "*" Ensure = "Present" DependsOn = "@[Package]vcRedist","[File]PhpIni" # Removed because this dependency does not work in # Windows Server 2012 R2 and below # This should work in WMF v5 and above # "[IisPreReqs_php]Iis" } # Make sure the php binary folder is in the path Environment PathPhp { Name = "Path" Value = ";$($DestinationPath)" Ensure = "Present" Path = $true DependsOn = "[Archive]PHP" } } ``` -------------------------------- ### Configure all possible settings for the WebApplication resource Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebApplication Shows an example for all possible settings of the WebApplication resource. Requires a mandatory PhysicalPath parameter. Demonstrates setting Website, Ensure, Name, PhysicalPath, WebAppPool, ApplicationType, AuthenticationInfo, PreloadEnabled, ServiceAutoStartEnabled, ServiceAutoStartProvider, SslFlags, and EnabledProtocols. ```powershell configuration Sample_WebApplication { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost', [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [String] $PhysicalPath ) Import-DscResource -ModuleName WebAdministrationDsc node $NodeName { WebApplication WebApplication { Website = 'Website' Ensure = 'Present' Name = 'WebApplication' PhysicalPath = $PhysicalPath WebAppPool = 'DefaultAppPool' ApplicationType = 'ApplicationType' AuthenticationInfo = ` DSC_WebApplicationAuthenticationInformation { Anonymous = $true Basic = $false Digest = $false Windows = $false } PreloadEnabled = $true ServiceAutoStartEnabled = $true ServiceAutoStartProvider = 'ServiceAutoStartProvider' SslFlags = @('Ssl') EnabledProtocols = @('http', 'net.tcp') } } } ``` -------------------------------- ### Confirm WebAdministrationDsc installation Source: https://github.com/dsccommunity/webadministrationdsc/wiki/Home Lists the DSC resources available in the WebAdministrationDsc module to verify the installation. ```powershell Get-DscResource -Module WebAdministrationDsc ``` -------------------------------- ### Composite configuration to install IIS pre-requisites for PHP Source: https://github.com/dsccommunity/webadministrationdsc/blob/main/README.md Defines a DSC composite configuration that installs IIS features required for PHP. Takes an Ensure parameter to specify whether features should be present or absent. Uses the WindowsFeature resource for each required feature. ```powershell # Composite configuration to install the IIS pre-requisites for PHP Configuration IisPreReqs_php { param ( [Parameter(Mandatory = $true)] [Validateset("Present","Absent")] [String] $Ensure ) foreach ($Feature in @("Web-Server","Web-Mgmt-Tools","web-Default-Doc", ` "Web-Dir-Browsing","Web-Http-Errors","Web-Static-Content",` "Web-Http-Logging","web-Stat-Compression","web-Filtering",` "web-CGI","web-ISAPI-Ext","web-ISAPI-Filter")) { WindowsFeature "$Feature$Number" { Ensure = $Ensure Name = $Feature } } } ``` -------------------------------- ### Sample_WebSite_StopDefault DSC configuration Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that installs IIS and stops the default website for security. Sets State = 'Stopped' on the WebSite resource and depends on the WindowsFeature IIS resource. ```powershell Configuration Sample_WebSite_StopDefault { param ( # Target nodes to apply the configuration [string[]]$NodeName = 'localhost' ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = "Present" Name = "Web-Server" } # Stop the default website WebSite DefaultSite { Ensure = "Present" Name = "Default Web Site" State = "Stopped" PhysicalPath = "C:\inetpub\wwwroot" DependsOn = "[WindowsFeature]IIS" } } } ``` -------------------------------- ### Create a new website with HTTPS bindings using certificate thumbprints Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite Use this DSC configuration to set up a new website with custom HTTPS bindings. It installs the IIS and ASP.NET 4.5 roles, stops the default website, copies website content from a source path, and creates a new website with two HTTPS bindings specified by certificate thumbprints. The configuration requires parameters for the website name, source path, and destination path. ```powershell Configuration Sample_WebSite_NewWebsite_UsingCertificateThumbprint { param ( # Target nodes to apply the configuration [string[]] $NodeName = 'localhost', # Name of the website to create [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $WebSiteName, # Source Path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $SourcePath, # Destination path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Stop the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the website content File WebContent { Ensure = 'Present' SourcePath = $SourcePath DestinationPath = $DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]AspNet45' } # Create the new Website with HTTPS WebSite NewWebsite { Ensure = 'Present' Name = $WebSiteName State = 'Started' PhysicalPath = $DestinationPath BindingInfo = @( DSC_WebBindingInformation { Protocol = 'HTTPS' Port = 8443 CertificateThumbprint = '71AD93562316F21F74606F1096B85D66289ED60F' CertificateStoreName = 'WebHosting' } DSC_WebBindingInformation { Protocol = 'HTTPS' Port = 8444 CertificateThumbprint = 'DEDDD963B28095837F558FE14DA1FDEFB7FA9DA7' CertificateStoreName = 'My' } ) DependsOn = '[File]WebContent' } } } ``` -------------------------------- ### Sample_WebSite_RemoveDefault DSC configuration Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that installs the IIS Web-Server feature and stops the default website by setting State = 'Stopped' and ServerAutoStart = $false. Uses the WebAdministrationDsc and PSDesiredStateConfiguration modules. ```powershell configuration Sample_WebSite_RemoveDefault { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost' ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Stop the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' ServerAutoStart = $false PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } } } ``` -------------------------------- ### Create IIS website with SSLFlags using WebAdministrationDsc Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that installs IIS and ASP.NET 4.5, stops the default site, copies website content, and creates a new HTTPS website with a certificate thumbprint and SSLFlags set to '1' to require Server Name Indication (SNI). ```powershell configuration Sample_WebSite_WithSSLFlags { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost', # Name of the website to create [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [String] $WebSiteName, # Source Path for Website content [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [String] $SourcePath, # Destination path for Website content [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = "Present" Name = "Web-Server" } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = "Present" Name = "Web-Asp-Net45" } # Stop the default website WebSite DefaultSite { Ensure = "Present" Name = "Default Web Site" State = "Stopped" ServerAutoStart = $false PhysicalPath = "C:\inetpub\wwwroot" DependsOn = "[WindowsFeature]IIS" } # Copy the website content File WebContent { Ensure = "Present" SourcePath = $SourcePath DestinationPath = $DestinationPath Recurse = $true Type = "Directory" DependsOn = "[WindowsFeature]AspNet45" } # Create the new Website # Have it set to the CertificateThumbprint # and set that the Server Name Indication is required WebSite NewWebsite { Ensure = "Present" Name = $WebSiteName State = "Started" PhysicalPath = $DestinationPath DependsOn = "[File]WebContent" BindingInfo = DSC_WebBindingInformation { Protocol = 'https' Port = '443' CertificateStoreName = 'My' CertificateThumbprint = 'BB84DE3EC423DDDE90C08AB3C5A828692089493C' HostName = $WebSiteName IPAddress = '*' SSLFlags = '1' } } } } ``` -------------------------------- ### Invoke xPhp configuration with parameters Source: https://github.com/dsccommunity/webadministrationdsc/blob/main/README.md Invokes the xPhp DSC configuration with specific parameters for package folder, download URIs, destination path, and configuration path. Disables MySQL extension installation. Uses backticks for line continuation. ```powershell xPhp -PackageFolder "C:\packages" ` -DownloadUri -DownloadUri "http://windows.php.net/downloads/releases/php-5.5.13-Win32-VC11-x64.zip" ` -Vc2012RedistDownloadUri "http://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe" ` -DestinationPath "C:\php" ` -ConfigurationPath "C:\MyPhp.ini" ` -installMySqlExt $false ``` -------------------------------- ### Create website with custom log fields using DSC Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that explicitly ensures a website is present with custom log fields. It installs IIS and ASP.NET 4.5, stops the default site, copies content, and creates a new site with W3C logging and a custom field for the Accept-Encoding request header. Requires the WebAdministrationDsc module and parameters for site name, source, and destination paths. ```powershell configuration Sample_WebSite_WithCustomLogFields_EnsurePresentExplicitly { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost', # Name of the website to create [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $WebSiteName, # Optional Site Id for the website [Parameter()] [UInt32] $SiteId, # Source Path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $SourcePath, # Destination path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Stop the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' ServerAutoStart = $false PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the website content File WebContent { Ensure = 'Present' SourcePath = $SourcePath DestinationPath = $DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]AspNet45' } # Create the new Website WebSite NewWebsite { Ensure = 'Present' Name = $WebSiteName SiteId = $SiteId State = 'Started' ServerAutoStart = $true PhysicalPath = $DestinationPath DependsOn = '[File]WebContent' LogFlags = @('Date','Time','ClientIP','ServerIP','UserAgent') LogFormat = 'W3C' LogCustomFields = @( DSC_LogCustomFieldInformation { LogFieldName = 'ClientEncoding' SourceName = 'Accept-Encoding' SourceType = 'RequestHeader' Ensure = 'Present' } ) } } } ``` -------------------------------- ### Create website with custom log fields (WebAdministrationDsc) Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that creates a website with custom W3C log fields. It installs IIS and ASP.NET 4.5, stops the default site, copies content, and creates a new site with LogFlags and a custom field for the Accept-Encoding request header. ```powershell configuration Sample_WebSite_WithCustomLogFields_EnsurePresentDefault { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost', # Name of the website to create [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $WebSiteName, # Optional Site Id for the website [Parameter()] [UInt32] $SiteId, # Source Path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $SourcePath, # Destination path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Stop the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' ServerAutoStart = $false PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the website content File WebContent { Ensure = 'Present' SourcePath = $SourcePath DestinationPath = $DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]AspNet45' } # Create the new Website WebSite NewWebsite { Ensure = 'Present' Name = $WebSiteName SiteId = $SiteId State = 'Started' ServerAutoStart = $true PhysicalPath = $DestinationPath DependsOn = '[File]WebContent' LogFlags = @('Date','Time','ClientIP','ServerIP','UserAgent') LogFormat = 'W3C' LogCustomFields = @( DSC_LogCustomFieldInformation { LogFieldName = 'ClientEncoding' SourceName = 'Accept-Encoding' SourceType = 'RequestHeader' } ) } } } ``` -------------------------------- ### Create IIS website with HTTPS binding using CertificateSubject Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite Use this DSC configuration to create a new IIS website with an HTTPS binding that references a certificate by subject, which is useful when the certificate is generated by the same configuration (e.g., with xCertReq). It installs the IIS and ASP.NET 4.5 roles, stops the default website, copies content from a source to a destination path, and creates the new site on port 8444 with the certificate subject 'CN=CertificateSubject' stored in the 'My' store. Requires Windows Server 2012 R2 or later and the WebAdministrationDsc module. ```powershell Configuration Sample_WebSite_NewWebsite_UsingCertificateSubject { param ( # Target nodes to apply the configuration [string[]] $NodeName = 'localhost', # Name of the website to create [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $WebSiteName, # Source Path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $SourcePath, # Destination path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Stop the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the website content File WebContent { Ensure = 'Present' SourcePath = $SourcePath DestinationPath = $DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]AspNet45' } # Create the new Website with HTTPS WebSite NewWebsite { Ensure = 'Present' Name = $WebSiteName State = 'Started' PhysicalPath = $DestinationPath BindingInfo = @( DSC_WebBindingInformation { Protocol = 'HTTPS' Port = 8444 CertificateSubject = 'CN=CertificateSubject' CertificateStoreName = 'My' } ) DependsOn = '[File]WebContent' } } } ``` -------------------------------- ### Unlock IIS sections using IIS Configuration Path format Source: https://github.com/dsccommunity/webadministrationdsc/wiki/IisFeatureDelegation Installs the IIS Windows Feature and unlocks the specified IIS configuration sections using the IIS Configuration Path format (e.g., MACHINE/WEBROOT/APPHOST) for the Path setting. ```powershell configuration Example { param ( [Parameter()] [string[]] $NodeName = 'localhost' ) Import-DscResource -Module WebAdministrationDsc Import-DscResource -Module PSDesiredStateConfiguration Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Allow Write access to some section that normally don't have it. IisFeatureDelegation serverRuntime { Filter = '/system.webserver/serverRuntime' OverrideMode = 'Allow' Path = 'MACHINE/WEBROOT/APPHOST' } IisFeatureDelegation anonymousAuthentication { Filter = '/system.webserver/security/authentication/anonymousAuthentication' OverrideMode = 'Allow' Path = 'MACHINE/WEBROOT/APPHOST' } IisFeatureDelegation sessionState { Filter = '/system.web/sessionState' OverrideMode = 'Allow' Path = 'MACHINE/WEBROOT/APPHOST' } } } ``` -------------------------------- ### Sample_WebSite_WithCustomLogFields_EnsureAbsent DSC configuration Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that installs IIS and ASP.NET 4.5, stops the default website, copies website content, and creates a new website with custom log fields. The LogCustomFields block uses DSC_LogCustomFieldInformation with Ensure = 'Absent' to remove the ClientEncoding request header field. ```powershell configuration Sample_WebSite_WithCustomLogFields_EnsureAbsent { param ( # Target nodes to apply the configuration [String[]] $NodeName = 'localhost', # Name of the website to create [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $WebSiteName, # Optional Site Id for the website [Parameter()] [UInt32] $SiteId, # Source Path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $SourcePath, # Destination path for Website content [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $DestinationPath ) # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration Node $NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Stop the default website WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' ServerAutoStart = $false PhysicalPath = 'C:\inetpub\wwwroot' DependsOn = '[WindowsFeature]IIS' } # Copy the website content File WebContent { Ensure = 'Present' SourcePath = $SourcePath DestinationPath = $DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]AspNet45' } # Create the new Website WebSite NewWebsite { Ensure = 'Present' Name = $WebSiteName SiteId = $SiteId State = 'Started' ServerAutoStart = $true PhysicalPath = $DestinationPath DependsOn = '[File]WebContent' LogFlags = @('Date','Time','ClientIP','ServerIP','UserAgent') LogFormat = 'W3C' LogCustomFields = @( DSC_LogCustomFieldInformation { LogFieldName = 'ClientEncoding' SourceName = 'Accept-Encoding' SourceType = 'RequestHeader' Ensure = 'Absent' } ) } } } ``` -------------------------------- ### Configure WebApplicationHandler with WebAdministrationDsc Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebApplicationHandler PowerShell DSC configuration that creates a WebApplicationHandler named 'ATest-WebHandler' at the specified IIS path. It sets various handler properties including physical handler path, verbs, modules, access rights, script processor, resource type, and location. The example uses the WebAdministrationDsc module and targets 'localhost'. ```powershell Configuration Example { Import-DscResource -ModuleName WebAdministrationDsc Node 'localhost' { WebApplicationHandler WebHandlerTest { Path = 'MACHINE/WEBROOT/APPHOST' Name = 'ATest-WebHandler' PhysicalHandlerPath = '*' Verb = '*' Modules = 'IsapiModule' RequireAccess = 'None' ScriptProcessor = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" ResourceType = 'Unspecified' AllowPathInfo = $false ResponseBufferLimit = 0 Type = $null PreCondition = $null Location = 'Default Web Site/TestDir' } } } ``` -------------------------------- ### Create IIS websites with configuration data (Sample_WebSite_NewWebsiteFromConfigurationData) Source: https://github.com/dsccommunity/webadministrationdsc/wiki/WebSite DSC configuration that creates IIS websites on multiple nodes using configuration data. It dynamically selects nodes with Role 'Web' and uses node-specific values for paths and website name. The accompanying $ConfigurationData defines common settings for all nodes and identifies two web servers. ```powershell Configuration Sample_WebSite_NewWebsiteFromConfigurationData { # Import the module that defines custom resources Import-DscResource -Module WebAdministrationDsc, PSDesiredStateConfiguration # Dynamically find the applicable nodes from configuration data Node $AllNodes.where{ $_.Role -eq 'Web' }.NodeName { # Install the IIS role WindowsFeature IIS { Ensure = 'Present' Name = 'Web-Server' } # Install the ASP .NET 4.5 role WindowsFeature AspNet45 { Ensure = 'Present' Name = 'Web-Asp-Net45' } # Stop an existing website (set up in Sample_WebSite_Default) WebSite DefaultSite { Ensure = 'Present' Name = 'Default Web Site' State = 'Stopped' ServerAutoStart = $false PhysicalPath = $Node.DefaultWebSitePath DependsOn = '[WindowsFeature]IIS' } # Copy the website content File WebContent { Ensure = 'Present' SourcePath = $Node.SourcePath DestinationPath = $Node.DestinationPath Recurse = $true Type = 'Directory' DependsOn = '[WindowsFeature]AspNet45' } # Create a new website WebSite BakeryWebSite { Ensure = 'Present' Name = $Node.WebsiteName State = 'Started' ServerAutoStart = $true PhysicalPath = $Node.DestinationPath DependsOn = '[File]WebContent' } } } $ConfigurationData = @{ # Node specific data AllNodes = @( # All the WebServers have the following identical information @{ NodeName = '*' WebsiteName = 'FourthCoffee' SourcePath = 'C:\BakeryWebsite\' DestinationPath = 'C:\inetpub\FourthCoffee' DefaultWebSitePath = 'C:\inetpub\wwwroot' }, @{ NodeName = 'WebServer1.fourthcoffee.com' Role = 'Web' }, @{ NodeName = 'WebServer2.fourthcoffee.com' Role = 'Web' } ); } ```