Use the DSC Script resource to change the application pool identity

The Desired State Configuration (DSC) module xWebAdministration does not yet support changing the identity of an application pool. There is a community fork cWebAdministration that seems to support it –  but I have not tested it yet.

What I did was to use the script resource to change the identity using the module WebAdministration. This is pretty straight forward so I just share the code here. Note that after changing the identity the state of the application pool is stopped. You can use the xWebAppPool resource to start it again.

Configuration WebServerConfiguration
{
	Node $AllNodes.NodeName
	{
        Script ChangeAppPoolIdentity
        {
            GetScript = { return @{ AppPoolName = "$($using:Node.WebAppPoolName)" }}
            TestScript = 
            {
                import-module webadministration -verbose:$false
                $pool = get-item("IIS:\AppPools\$($using:Node.WebAppPoolName)")
                return $pool.processModel.userName -eq $using:AppPoolUserName
            }
            SetScript = 
            {
                import-module webadministration -verbose:$false

                $pool = get-item("IIS:\AppPools\LSK.Services");

                $pool.processModel.userName = [String]($using:AppPoolUserName)
                $pool.processModel.password = [String]($using:AppPoolPassword)
                $pool.processModel.identityType = [String]("SpecificUser");

                $pool | Set-Item
            }
            DependsOn = "[xWebsite]WebSite"
        }

        xWebAppPool NewWebAppPool 
        { 
            Name            = $Node.WebAppPoolName 
            Ensure          = "Present" 
            State           = "Started" 
			DependsOn       = "[Script]ChangeAppPoolIdentity"
        } 	
	}
}

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