我在Octopus Deploy
中有一个脚本模块,它将在IIS中创建一个新的Web应用程序。作为其中的一部分,它还会创建一个 IIS 池(如果尚不存在(。
function Create-WebApplication($webSite, $alias, $physicalPath, $poolName)
{
$pool = "IIS:AppPools$poolName"
if (Test-Path $pool)
{
Write-Host "IIS pool already exists: $poolName"
}
else
{
#--Always gets into this else condition, no matter exists or not--
Write-Host "Creating IIS pool: $poolName"
New-WebAppPool -Name $poolName
Set-ItemProperty $pool -Name "managedRuntimeVersion" -Value "v4.0"
}
Write-Host "Creating website: $webSite$alias"
New-WebApplication -Name $alias -Site $webSite -PhysicalPath $physicalPath -ApplicationPool $poolName -Force
Write-Host "Setting the application pool: $poolName"
Set-ItemProperty IIS:Sites$webSite$alias -name applicationPool -value $poolName -Force
}
问题是,它总是会进入else
条件,即尝试创建应用程序池。相同的脚本在PowerShell ISE中工作正常。
我错过了一些明显的东西吗?
您需要导入 Web 管理模块才能针对 IIS 驱动器工作,否则Test-Path
在检查它时将始终失败。