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

1

15

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." }
Amorphous answered 6/4, 2017 at 5:59 Comment(0)
I
34

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

Intercom answered 6/4, 2017 at 6:7 Comment(3)
@ShrutiAgarwal: Good to help you.Intercom
In some cases this would return false for an existing application pool. i had to add import-module webadministration to the top of the file.Everick
@TonyT_32909023190: Like what ? I didnt get it.. Kindly edit your comments or post an answer.Intercom

© 2022 - 2024 — McMap. All rights reserved.