windows-powershell-docs
windows-powershell-docs copied to clipboard
Using IISAdministration and WebAdministration Module together can lead to Script breaking errors
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:
- Get-*Cmdlet from IISAdministration Module
- New-*Cmdlet from WebAdministration Module
- New-IISSiteBinding from IISAdministration Module
Prevention of the issue
To workaround this issue I found two solutions.
- Remove the IISAdministration Module before executing
New-IISSite - Wrap
Get-IISAppPoolandNew-WebAppPoolinStart-IISCommitDelay/Stop-IISCommitDelay - 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-IISServerManagercmdlet 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