PowerShell 7+使用IISAdministration模块创建IIS 10应用池失败
IIS 10+ 在 PowerShell 7.2.6 中创建应用池失败的解决方案
问题背景
在带GUI的Windows Server 2019环境中,已安装IIS 10,以管理员身份运行PowerShell 7.2.6,导入IISAdministration模块(非旧版WebAdministration),尝试使用示例代码创建应用池但完全失败,旧版PowerShell中有时可运行。
原测试代码:
# PowerShell 7.2.6 # Windows Server 2019 (With the GUI). # IIS 10 etc is already installed. # Executed as Administrator. # NOTE: This imports the IISAdministration module not the old WebAdministration module. Import-Module -Name IISAdministration -UseWindowsPowerShell -WarningAction SilentlyContinue Reset-IISServerManager -Confirm:$false Start-IISCommitDelay # Create the foo.co.uk application pool. Write-Host "Creating the foo.co.uk application pool.`n" -ForegroundColor Green $ServerManager = Get-IISServerManager $ServerManager.ApplicationPools.Add("foo.co.uk.apppool") # $ServerManager.CommitChanges() (this doesn't work though I keep seeing it in examples.) Stop-IISCommitDelay
问题原因
IISAdministration模块在PowerShell 7中通过Windows PowerShell兼容性层导入(-UseWindowsPowerShell参数),直接混用ServerManager对象方法与Start/Stop-IISCommitDelay会触发提交机制冲突。- 兼容性模式下,
CommitChanges()方法无法正确触发IIS配置写入,导致应用池创建不生效。
可行解决方案
方案一:使用模块原生cmdlet创建(推荐)
直接调用IISAdministration模块提供的New-IISAppPool cmdlet,无需手动处理提交逻辑,兼容性更强:
# 以管理员身份运行PowerShell 7.2.6 Import-Module -Name IISAdministration -UseWindowsPowerShell -WarningAction SilentlyContinue # 基础创建应用池 New-IISAppPool -Name "foo.co.uk.apppool"
如需配置额外属性(如.NET版本、托管管道模式),可扩展参数:
New-IISAppPool -Name "foo.co.uk.apppool" ` -ManagedRuntimeVersion "v4.0" ` -ManagedPipelineMode Integrated ` -StartMode AlwaysRunning
方案二:修正原代码的提交逻辑
若坚持使用ServerManager对象,需移除Start/Stop-IISCommitDelay,直接调用CommitChanges()完成配置提交:
Import-Module -Name IISAdministration -UseWindowsPowerShell -WarningAction SilentlyContinue Write-Host "Creating the foo.co.uk application pool.`n" -ForegroundColor Green $ServerManager = Get-IISServerManager $newAppPool = $ServerManager.ApplicationPools.Add("foo.co.uk.apppool") # 可选:配置应用池属性 $newAppPool.ManagedRuntimeVersion = "v4.0" $newAppPool.ManagedPipelineMode = [Microsoft.Web.Administration.ManagedPipelineMode]::Integrated # 提交更改 $ServerManager.CommitChanges()
验证方法
执行以下命令检查应用池是否创建成功:
Get-IISAppPool -Name "foo.co.uk.apppool"
若返回应用池对象信息,则创建成功。
内容的提问来源于stack exchange,提问作者William Humphreys
相关产品推荐
相关产品推荐

