Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether an application pool exists or not in IIS using powershell and web administration module?

I am using powershell to automate configuring 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 whether pool exists or not.How do I go about doing this?

Please note : There is no IIS Drive on my system. And hence commands which have IIS mentioned in the path like the following fail :

$IISPath = "IIS:\AppPools"
cd $IISPath
if (Test-Path ".\NewAppPool") { Write-Host "NewAppPool exists." }
like image 623
Shruti Agarwal Avatar asked Apr 06 '17 05:04

Shruti Agarwal


People also ask

How do I check my application pool status?

The Get-IISAppPool cmdlet gets information about application pools and their current status and other key information. If a specific application pool or a comma delimited list of application pools are requested, only those whose names are passed as an argument are returned.


1 Answers

Use this:

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;
}

Note: You need the WebAdministration module for Powershell. After importing you can use it. See the other ANSWER where I have mentioned about IIS drive

like image 197
Ranadip Dutta Avatar answered Oct 02 '22 10:10

Ranadip Dutta