14

我正在使用 powershell 在我的 IIS 中自动配置网站。我有以下代码为我创建了一个 Web 应用程序池

#Creating a new Application Pool
New-WebAppPool "NewAppPool"

但是在创建池之前,我想检查池是否存在。我该怎么做呢?

请注意:我的系统上没有 IIS 驱动器。因此,在路径中提到 IIS 的命令如下所示失败

$IISPath = "IIS:\AppPools"
cd $IISPath
if (Test-Path ".\NewAppPool") { Write-Host "NewAppPool exists." }
4

1 回答 1

30

用这个:

import-module webadministration

$AppPoolName="NewAppPool"

if(Test-Path IIS:\AppPools\$AppPoolName)
{
"AppPool is already there"
return $true;
}
else
{
"AppPool is not present"
"Creating new AppPool"
New-WebAppPool "$AppPoolName" -Force
return $false;
}

注意:您需要 Powershell 的 WebAdministration 模块。导入后就可以使用了。请参阅我提到的有关 IIS 驱动器的其他答案

于 2017-04-06T06:07:44.737 回答