Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save uzbekdev1/6c552dfd9204a13c68121dfc85b5dc35 to your computer and use it in GitHub Desktop.
Save uzbekdev1/6c552dfd9204a13c68121dfc85b5dc35 to your computer and use it in GitHub Desktop.
Check if the an application pool exists on a remote server.
<#
.SYNOPSIS
Tests if a web application exists (this can be a site or application).
This was only written to keep naming conventions consistent. This is the same as
Test-Path IIS:\Sites\$SiteName;
.PARAMETER Url
The url of the match on
.PARAMETER Environment
The environment to apply this to
.EXAMPLE
Test-WebAppExists -Environment Dev -Url "http://unittest.{env}.place.something.com/services"
#>
Function Test-WebAppExists {
Param (
[string] $ServerName = $env:COMPUTERNAME,
[Parameter(Mandatory = $true)]
[string] $Url
)
# setup variables
$appPoolName = ConvertTo-UrlBasedAppPoolName -Url $Url
$scriptBlock = {
if($appPoolName -eq $null) {
$appPoolName = $args[0]
}
Import-Module WebAdministration
$pathToTest = "IIS:\AppPools\{0}" -f $appPoolName
if((Test-Path $pathToTest) -eq $false) { return $false }
$app = Get-Item $pathToTest
$exists = $true
if($app -eq $null) { $exists = $false }
if($app.GetType().Fullname -ne "Microsoft.IIs.PowerShell.Framework.ConfigurationElement") { $exists = $false }
return $exists;
} # end scriptblock
$parameters = @($appPoolName)
if(Test-IsLocalComputerName -ComputerName $ServerName) {
$exists = . $scriptBlock
} else {
$exists = Invoke-Command -ComputerName $ServerName -ScriptBlock $scriptBlock -ArgumentList $parameters
}
return $exists;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment