Reputation: 14088
I would like to have a powershell script that could create an IIS web site. but I am getting error
New-IISSite : Filename: \?\C:\Windows\system32\inetsrv\config\applicationHost.config Error: Cannot commit configuration changes because the file has changed on disk At C:\projects\salonsecretSrc\RegisterWebSite.ps1:38 char:9 + New-IISSite -BindingInformation $strIssBindigFormat -Name $st ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [New-IISSite], FileLoadException + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.IIS.Powershell.Commands.NewIISSiteComman
this is my script:
$strCurrentPath = $MyInvocation.MyCommand.Path
$strWebSiteFolder = Get-ChildItem (dir $strCurrentPath)
$strWebSiteBindingPath = $strWebSiteFolder.Directory.FullName+"\build\WebSite"
$strCurrentFolderName = $strWebSiteFolder.Directory.Name
$strIssSiteName = "$strCurrentFolderName.local"
$strIssBindigFormat = ":80:$strIssSiteName"
Write-Host "Current Script path: $strCurrentPath"
Write-Host "IIS Web Site phycical path: $strWebSiteBindingPath"
Write-Host "IIS SiteName: $strIssSiteName"
Write-Host "IIS Bindindg Format: $strIssBindigFormat"
Write-Host "Creating App Pool - $strIssSiteName"
New-WebAppPool -Name $strIssSiteName -Force
Write-Host "Creating Web Site Pool - $strIssSiteName"
New-IISSite -BindingInformation $strIssBindigFormat -Name $strIssSiteName -PhysicalPath "$strWebSiteBindingPath" -Force
Write-Host "Mapping Pull and Web Site - $strIssSiteName"
Set-ItemProperty "IIS:\Sites\$strIssSiteName" -name applicationPool -value $strIssSiteName
Write-Host "$strIssSiteName WebSite Created"
what could be an issue how to solve ?
it could create an web site first time but second time if I remove it manually it will get this error.
Upvotes: 10
Views: 15603
Reputation: 6526
The fix for me was to call Reset-IISServerManager -Confirm:$false
after creating the app pool, before creating the website.
The reason is that the IISAdministration PowerShell module (at least as of now) does not support creating application pools, so that has to be done using the older WebAdministration module. Once that change is made, the IISAdministration module has an old view of the config data, so needs to be reset to include the changes made by the WebAdministration module.
According to the docs (https://learn.microsoft.com/en-us/powershell/module/iisadministration/reset-iisservermanager?view=windowsserver2022-ps), this command:
resets the IISAdministration view of the IIS ServerManager object and the view of .config file applicationHost.config to the current contents of the .config file
Upvotes: 3
Reputation: 51
Since this is the first entry in google about this issue, I will describe my findings to the cause of the error message here. It's maybe not related to OPs issue.
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:
Prevention of the issue
To workaround this issue I found two solutions.
New-IISSite
Get-IISAppPool
and New-WebAppPool
in Start-IISCommitDelay
/ Stop-IISCommitDelay
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
Upvotes: 4
Reputation: 4684
It seems something really screwy is going on with New-IISSite command. I ended up dropping back to New-Website (via WebAdmistration module), and all the problems went away.
Upvotes: 3
Reputation: 358
There are many different things that can lock the applicationHost.config file and cause powershell commands like New-IISSite
to fail.
In this case, you mentioned you deleted the site manually so perhaps you still have IIS Manager open and it is locking the file. I suggest closing IIS Manager.
As described in this blog post on Powershell and IIS, the makers of Octopus Deploy have found that the only reliable solution to the file-locking problem is to wrap any file-altering commands like New-WebAppPool
in try-catch blocks and retry these operations multiple times. The source code for a function to do this is found in the blog.
Upvotes: 13