Function Uninstall-WebApplication { <# .SYNOPSIS Uninstalls a Web Application from the appropriate place .DESCRIPTION Uninstalls a Web Application from the appropriate place .PARAMETER WebAppName [string] The name of the web application. .PARAMETER IsClient [switch] Is this package installed to client? .PARAMETER IsAdmin [switch] Is this package installed to admin? .PARAMETER IsLegacy [switch] Is this package installed to the legacy site? (typically Default Web Site) .INPUTS WebAppName is required. Requires one of IsClient or IsAdmin or IsLegacy .OUTPUTS Various diagnostic information about the install process .EXAMPLE Uninstall-WebApplication -WebAppName BankService -IsLegacy Various diagnostic information about the install process. #> [CmdletBinding(DefaultParameterSetName='IsClient')] Param( [Parameter(ParameterSetName='IsAdmin',Mandatory=$true, Position=0)] [Parameter(ParameterSetName='IsClient',Mandatory=$true, Position=0)] [Parameter(ParameterSetName='IsLegacy',Mandatory=$true, Position=0)] [string]$WebAppName, [Parameter(ParameterSetName='IsClient',Mandatory=$true)] [switch]$IsClient, [Parameter(ParameterSetName='IsAdmin',Mandatory=$true)] [switch]$IsAdmin, [Parameter(ParameterSetName='IsLegacy',Mandatory=$true)] [switch]$IsLegacy ) process { $loglead = (Get-LogLeadName) if (!(Test-IsAdmin)){ throw "You are not running as administrator. Can not continue." } ## If this is a legacy app, and we are on a web-server, we can't uninstall it ## If this is a client or admin app, and we are on not on a web-server, we can't uninstall it. ## If this is a developer machine we can install it if (Test-IsDeveloperMachine) { ## Do nothing } elseif ((Test-IsWebServer) -and $IsLegacy) { Write-Warning "$loglead : Can not install app-tier web applications on the web tier" return } elseif (!(Test-IsWebServer) -and ($IsClient -or $IsAdmin)) { Write-Warning "$loglead : Can not install web-tier web applications on the app tier" return } Write-Host "$loglead : $WebAppName being uninstalled by $($env:username) on $($env:computername) at $(Get-Date)" $oldAppPoolNames = @() $WebAppName = $WebAppName.Replace('\','/') ## By process of elimination, if you weren't in parameter set IsAdmin or IsLegacy, you must be in IsClient. $parentAppName = 'WebClient' if ($IsAdmin) { $parentAppName = 'WebClientAdmin' } ## Test to make sure this is the valid site name. We can do this by looking for the site, if it doesn't exist, look for the path ## C:\Orb\$parentAppName and then find any sites for that. $findSiteIfExists = Get-IISSite -Name $parentAppName $siteList = @($findSiteIfExists) if (Test-IsCollectionNullOrEmpty $siteList) { ## Specify the path we are looking for such as c:\orb\webclient $targetFolderPath = (Join-Path (Get-OrbPath) $parentAppName) $siteList = (Get-IISSitesByPath $targetFolderPath) } if ($IsLegacy) { ## Use of this function ensures that the Default Web Site exists $defaultWebsite = (Get-DefaultWebsite) if ($null -ne $defaultWebsite) { $siteList += $defaultWebsite.Name } } if (Test-IsCollectionNullOrEmpty $siteList) { Write-Host "Can not uninstall an application if no sites are found to touch. Exiting" return } else { foreach ($site in $siteList) { $sitePath = $site.Name try { $existingApplication = Get-WebApplication -Name $WebAppName -Site $sitePath if ($null -ne $existingApplication) { $oldAppPoolNames += $existingApplication.applicationPool Write-Host "Remove-WebApplication -Name $WebAppName -Site '$($sitePath)'" Remove-WebApplication -Name $WebAppName -Site $sitePath } } catch { Write-Verbose $_.Exception.Message Write-Verbose "$logLead : Could not delete [$WebAppName] under [$($site)] to remove the web application" } } } foreach($oldAppPoolName in $oldAppPoolNames) { ## The same "wrong" app pool could have been used on other apps so only remove them if there are no referenced apps for this pool $existingApp = (Get-Item (Join-Path "IIS:\AppPools" $oldAppPoolName)) if ($null -ne $existingApp) { $existingAppApplicationCount = (Get-IisAppPoolChildApplicationsCount $oldAppPoolName) ## The count of that outdated application pool is 0, indicating that apppool isn't in use. Delete it. if ($existingAppApplicationCount -eq 0) { Write-Verbose "$logLead : Removing identified and unused Web AppPool [$oldAppPoolName]" (Remove-WebAppPool -Name $oldAppPoolName) } else { Write-Verbose "$logLead : Web AppPool [$oldAppPoolName] still has things attached to it. Not deleting." } } } } }