无法提交配置更改,因为磁盘上的文件已更改



我想要一个可以创建IIS网站的Powershell脚本。 但我收到错误

新 IISSite : 文件名: \?\C:\Windows\system32\inetsrv\config\applicationHost.config 错误: 无法提交配置更改,因为文件已更改 disk at C:\projects\salonsecretSrc\RegisterWebSite.ps1:38 char:9 + 新 IISSite -绑定信息 $strIssBindigFormat -名称 $st ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotDirect: (:) [New-IISSite], FileLoadException + FullQualifiedErrorId : System.IO.FileLoadException,Microsoft.IIS.Powershell.Command.NewIISSiteComman

这是我的脚本:

$strCurrentPath = $MyInvocation.MyCommand.Path
$strWebSiteFolder = Get-ChildItem (dir $strCurrentPath)
$strWebSiteBindingPath = $strWebSiteFolder.Directory.FullName+"buildWebSite"
$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"

如何解决可能是什么问题?

它可以第一次创建一个网站,但第二次如果我手动删除它,它会得到这个错误。

有许多不同的东西可以锁定 applicationHost.config 文件并导致 Powershell 命令(如New-IISSite)失败。

在这种情况下,您提到您手动删除了站点,因此您可能仍然打开了 IIS 管理器并且它正在锁定文件。我建议关闭 IIS 管理器。

正如这篇关于Powershell和IIS的博客文章所述,Octopus Deploy的制造商发现,文件锁定问题的唯一可靠解决方案是将任何文件更改命令(如New-WebAppPool)包装在try-catch块中,并多次重试这些操作。执行此操作的函数的源代码可在博客中找到。

New-IISSite 命令似乎发生了一些非常糟糕的事情。 我最终回到了新网站(通过WebAdmistration模块),所有的问题都消失了。

由于这是谷歌中关于此问题的第一个条目,因此我将在此处描述我的发现以说明错误消息的原因。这可能与 OP 问题无关。

失败代码的示例

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

解释

如果使用 IISAdministration PowerShell 模块中的Get-IISAppPool然后是来自 WebAdministration PowerShell 模块的New-WebAppPool, 命令Get-IISAppPool将以某种方式阻止New-WebAppPool关闭applicationHost.config文件,并可能使其处于打开状态。

如果我们现在运行New-IISSite,我们将得到错误:New-IISSite : Filename: \?C:Windowssystem32inetsrvconfigapplicationHost.config Error: Cannot commit configuration changes because the file has changed on disk

如果遵循上述顺序,我也能够使用其他命令重现它:

  1. Get-*Cmdlet fromIISAdministration Module
  2. 来自Web 管理模块的新 *Cmdlet
  3. New-IISSiteBinding fromIISAdministration Module

预防问题

为了解决这个问题,我找到了两个解决方案。

  1. 在执行New-IISSite之前删除IISA 管理模块
  2. Get-IISAppPool包裹并New-WebAppPoolStart-IISCommitDelay/Stop-IISCommitDelay
  3. 仅使用其中一个模块。我决定只使用 IISAdministration 模块。它不在Microsoft文档中,但您也可以使用Get-IISServerManagercmdlet 创建/删除应用程序池,如此处所述。

解决方法 1

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

解决方法 2

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

最新更新