How to check if an application pool exists or not in IIS using PowerShell and the web administration module?

I use powershell to automate the configuration of websites in my IIS. I have the following code that creates a web application pool for me

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

But before creating the pool, I want to check if the pool exists or not. How should I do it?

Please note: there is no IIS disk on my system. And therefore, commands that have IIS are mentioned in the path, for example, the following fail :

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

Use this:

$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. . . ANSWER, IIS

+6

Source: https://habr.com/ru/post/1674167/


All Articles