Skip to content

Instantly share code, notes, and snippets.

@lholman
Created May 29, 2013 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lholman/5669608 to your computer and use it in GitHub Desktop.
Save lholman/5669608 to your computer and use it in GitHub Desktop.
A powershell module to remove a specific IIS website and app pool from a specified server
function Remove-IISSite{
<#
.SYNOPSIS
A powershell module to remove a specific IIS website and app pool from a specified server
.DESCRIPTION
A powershell module to remove a specific IIS website and app pool from a specified server
.NOTES
Author: Lloyd Holman
DateCreated: 17/01/2013
Requirements: Copy this module to any location found in $env:PSModulePath
.PARAMETER serverName
Required. The name of the remote server to execute the command against
.PARAMETER platformName
Required. The name of the platform to remove the IIS web site and App Pool for
.PARAMETER user
Optional. The FQDN of the administrator to run the commands as
.PARAMETER psSession
Optional. A valid PSSession object for the supplied serverName, saves authenticating again
.EXAMPLE
Import-Module Remove-IISSiteSpecifcServer
Import the module
.EXAMPLE
Get-Command -Module Remove-IISSiteSpecifcServer
List available functions
.EXAMPLE
Remove-IISSiteSpecifcServer -serverName -platformName -user
Execute the module
#>
[cmdletbinding()]
Param(
[Parameter(
Position = 0,
Mandatory = $True )]
[string]$serverName,
[Parameter(
Position = 1,
Mandatory = $True )]
[string]$platformName,
[Parameter(
Position = 2,
Mandatory = $False )]
[string]$user,
[Parameter(
Position = 3,
Mandatory = $False )]
[system.management.automation.runspaces.PSSession]$psSession
)
Begin {
$DebugPreference = "Continue"
}
Process {
Try
{
if ($serverName -eq "")
{
Write-Host "Please provide a serverName to execute the command against"
}
if ($platformName -eq "")
{
Write-Host "Please provide a platformName to execute the command for"
}
if ($user -eq "" -and $psSession -eq $null)
{
Write-Host "Please provide a Windows user name via -user or a PSSession object via -psSession"
}
if ($psSession -ne $null)
{
#Lets check the supplied serverName against the PSSession ComputerName
if ($psSession.ComputerName -ne $serverName)
{
Write-Host "The supplied serverName ($serverName) doesn't match the ComputerName ($psSession.ComputerName) of the supplied PSSession object."
break
}
}
$s = $null
if ($psSession -ne $null)
{
#Weve been passed a PSSession object so lets use that
$s = $psSession
}
else
{
#Create a Credential object only if a $user has been supplied
if ($user -ne "") { $c = Get-Credential -Credential $user; }
if ($c -ne $null)
{
#Create a new PSSession with the specific user credential
$s = New-PSSession -ComputerName $serverName -Credential $c;
}
}
$appPoolName = "$platformName"
Write-Host "Removing IIS app pool: $appPoolName on $serverName";
Invoke-Command -Session $s -ScriptBlock { param($InnerServerName, $InnerAppPoolName) & c:\windows\system32\inetsrv\APPCMD delete apppool "$InnerAppPoolName"} -ArgumentList $serverName, $appPoolName | Format-List;
$siteName = "$platformName"
Write-Host "Removing IIS site: $siteName on $serverName";
Invoke-Command -Session $s -ScriptBlock { param($InnerServerName, $InnerSiteName) & c:\windows\system32\inetsrv\APPCMD delete site "$InnerSiteName"} -ArgumentList $serverName, $siteName | Format-List;
if ($psSession -eq $null)
{
Remove-PSSession -Session $s;
}
}
catch [Exception] {
$result = "Woh!, wasn't expecting to get this exception. `r`n $_.Exception.ToString()"
}
}
End {
return $result | Format-Table
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment