windows-powershell-docs icon indicating copy to clipboard operation
windows-powershell-docs copied to clipboard

Using IISAdministration and WebAdministration Module together can lead to Script breaking errors

Open sKuhLight opened this issue 3 years ago • 0 comments

I was working on a script to automate the setup of a test-environment and encoutered following problem.

Example for failing code

Get-IISAppPool
New-WebAppPool -Name TestAppPool
New-IISSite -Name TestSite -PhysicalPath D:\ -BindingInformation *:80:test

Explaination

If using Get-IISAppPool from the IISAdministration PowerShell Module followed by New-WebAppPool from the WebAdministration PowerShell Module, the command Get-IISAppPool will somehow prevent New-WebAppPool from closing the applicationHost.config file and probably leaves it in an open state.

If we now run New-IISSite, we will get the Error: New-IISSite : Filename: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config Error: Cannot commit configuration changes because the file has changed on disk

I was able to reproduce this with other Commands too if followed the order described above:

  1. Get-*Cmdlet from IISAdministration Module
  2. New-*Cmdlet from WebAdministration Module
  3. New-IISSiteBinding from IISAdministration Module

Prevention of the issue

To workaround this issue I found two solutions.

  1. Remove the IISAdministration Module before executing New-IISSite
  2. Wrap Get-IISAppPool and New-WebAppPool in Start-IISCommitDelay / Stop-IISCommitDelay
  3. Only use one of the Modules. I decided to only use the IISAdministration Module. It's not in the Microsoft Docs but you can create/delete AppPools with it too using the Get-IISServerManager cmdlet as described here.

Workaround 1

Get-IISAppPool
New-WebAppPool -Name TestAppPool
Remove-Module IISAdministration
New-IISSite -Name TestSite -PhysicalPath D:\ -BindingInformation *:80:test

Workaround 2

Start-IISCommitDelay
Get-IISAppPool
New-WebAppPool -Name TestAppPool
Stop-IISCommitDelay
New-IISSite -Name TestSite -PhysicalPath D:\ -BindingInformation *:80:test

sKuhLight avatar Sep 21 '22 19:09 sKuhLight