Uninstall-SecureMFA_SSPR_Portal.ps1

#Requires -RunAsAdministrator
#Requires -Version 5.0

<#
     .SYNOPSIS
        Uninstalls SecureMFA SSPR Portalr.
    .DESCRIPTION
        Uninstalls SecureMFA SSPR Portal from Windows Server.
  
    .NOTES
        Version: 2.0.0.1
        Author: SecureMfa.com
        Creation Date: 06/01/2021
        Purpose/Change: Release
   
    .EXAMPLE
        C:\PS> Uninstall-SecureMFA_SSPR_Portal
 
        This command will Uninstalls SecureMFA SSPR Portal from Windows Server.
    
#>


Function Uninstall-SecureMFA_SSPR_Portal {
Param
(
    [Parameter(Mandatory=$false)][string]$siteName = "SecureMFASSPR",
    [Parameter(Mandatory=$false)][string]$WebSSPRPortalPath = "C:\inetpub\SecureMFASSPR\",
    [Parameter(Mandatory=$false)][string]$IISAppPoolName = "SecureMFASSPR",
    [Parameter(Mandatory=$false)][Switch]$Force
)
    
    #Validate uninsatall action
    if (!$Force) {
    $message  = "Please confirm if you want to uninstall SSPR Portal [Web site $siteName will be deleted from $WebSSPRPortalPath ]"            
    $question = 'Please confirm?'
    $choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
    $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
    $choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
    $decision_Validation = $Host.UI.PromptForChoice($message, $question, $choices, 0)
    if ($decision_Validation -eq 1 ) {Write-Host "Uninstall has been cancelled, exiting!" -ForegroundColor Yellow ; break} 
    }
            
    try
    {
        $Error.Clear()
             
        #Start Uninstall

        #Site Configuration
        Import-Module WebAdministration

        #Stop IIS website and app pool if it was deployed before.
        if ((Get-IISAppPool -Name $IISAppPoolName).state -eq "Started") {Stop-WebAppPool -Name $IISAppPoolName ; write-host "$IISAppPoolName app pool has been stoped."} else {write-host "$IISAppPoolName app pool is not running."}
        if ((Get-Website -Name $siteName).state -eq "Started") {Stop-Website $siteName ; write-host "$siteName website has been stoped."} else {write-host "$siteName website is not running."}
        
        write-host "Waiting for $siteName to unlaod ..."
        Start-Sleep -s 10

        #Remove web site
        $site = Get-Website -Name $siteName
        if(!$site) {Write-Host "Site $siteName could not be found, Skipping ..."} else {Write-Host "Removing Web Site $siteName" -ForegroundColor Yellow ; Remove-WebSite -Name $siteName}

        #Remove IIS app pool
        if (Test-Path ("IIS:\AppPools\" + $IISAppPoolName)) { Write-Host "Removing App pool $IISAppPoolName" -ForegroundColor Yellow ; Remove-WebAppPool -Name $IISAppPoolName } else { Write-Host "$IISAppPoolName IIS AppPool does not exists. Skipping ..." }

        #Delete WEB API portal
        if(!(Test-Path ($WebSSPRPortalPath))){Write-Host "Folder $WebSSPRPortalPath could not be found, Skipping ..."} else {write-host "Removing $WebSSPRPortalPath" -ForegroundColor Yellow ; Remove-Item $WebSSPRPortalPath -Recurse -Force }
          
        # Uninstall completed:
        write-host "Uninstall of Site: $siteName ($WebSSPRPortalPath) complete." -ForegroundColor Green
        
    }
    catch
    {
        Write-Host "$($MyInvocation.InvocationName): $_" -ForegroundColor red
    }    


}