Skip to content

Instantly share code, notes, and snippets.

@david-mclean
Last active August 29, 2015 14:23
Show Gist options
  • Save david-mclean/03fec70fd76552b1bc45 to your computer and use it in GitHub Desktop.
Save david-mclean/03fec70fd76552b1bc45 to your computer and use it in GitHub Desktop.
Powershell scripts used to easy deploying to IIS from a package created by build server. These scripts are already 18 months old, we actually call product specific deployment script from ReleaseManagment that uses these scripts.
[CmdletBinding(DefaultParametersetName="None")]
param(
[Parameter(Position=0,Mandatory=$true)][string]$name,
[Parameter(Position=1,Mandatory=$true)][string]$websiteName,
[Parameter(Position=2,Mandatory=$true)][string]$appPoolName,
[Parameter(Position=3,Mandatory=$true)][string]$physicalPath,
[Parameter(Mandatory=$true)][string]$username,
[Parameter(Mandatory=$true)][string]$password,
[string]$appPoolDotNetVersion = "v4.0",
[Parameter(ParameterSetName="sf",Mandatory=$false)][switch]$includeSharedFolder,
[Parameter(ParameterSetName="sf",Mandatory=$true)][string]$sharedFolderPath,
[Switch]$appPoolPipelineModeClassic,
[Switch]$ssl
)
Import-Module WebAdministration
#Create / Update apppool.
.\CreateAppPool @psboundparameters
cd IIS:\Sites\
## First check is that the site exists... Rather important really.
$siteName = if($websiteName -match '/') { $websiteName.split('/')[0]} else {$websiteName}
if(!(Test-Path $siteName -pathType container))
{
throw "Website: $siteName does not exist, cant create an application without a valid website"
}
# In case applications are in a folder
if( $websiteName -match '/' -AND !(Test-Path $websiteName -pathType container))
{
#There is a folder and it does not exist.
#Get site and find the physical path - create folder in said location.
$site = Get-Item $siteName
ni "$($site.PhysicalPath)\$($websiteName.split('/')[1])" -type directory
}
##Need to check the existance of application first.
if (!(Test-Path $websiteName/$name -pathType container))
{
try
{
New-WebApplication -Name $name -Site $websiteName -PhysicalPath $PhysicalPath -ApplicationPool $appPoolName
#Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/AnonymousAuthentication -name username -value "" -location $websiteName/$name
#Set-WebConfiguration -filter system.web/authentication -value @{mode='Forms'} -location $websiteName/$name
if($includeSharedFolder -eq $true)
{
if(!(Test-Path $websiteName/$name/SharedFolder))
{
$virtualDir = New-WebVirtualDirectory -Site $websiteName -Application $name -Name SharedFolder -PhysicalPath $sharedFolderPath
Set-ItemProperty $websiteName/$name/SharedFolder -Name username -Value $username
Set-ItemProperty $websiteName/$name/SharedFolder -Name password -Value $password
}
}
}
catch [System.Exception]
{
write-host $error[0] -ForegroundColor Red
}
} else {
#already exists so update values..
$application = Get-Item $websiteName/$name
Write-host "Application $websiteName/$name exists"
write-host "Updating physicalPath to $physicalPath"
Set-ItemProperty $websiteName/$name -name physicalPath -value $PhysicalPath
write-host "Updating AppPool Name to '$appPoolName'"
Set-ItemProperty $websiteName/$name -name applicationPool -value $appPoolName
#Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/AnonymousAuthentication -name username -value "" -location $websiteName/$name
#Set-WebConfiguration -filter system.web/authentication -value @{mode='Forms'} -location $websiteName/$name
if($includeSharedFolder -eq $true)
{
if(!(Test-Path $websiteName/$name/SharedFolder))
{
$virtualDir = New-WebVirtualDirectory -Site $websiteName -Application $name -Name SharedFolder -PhysicalPath $sharedFolderPath
Set-ItemProperty $websiteName/$name/SharedFolder -Name username -Value $username
Set-ItemProperty $websiteName/$name/SharedFolder -Name password -Value $password
}
$virtualDir = Get-Item $websiteName/$name/SharedFolder
Set-ItemProperty $websiteName/$name/SharedFolder -name physicalPath -value $sharedFolderPath
Set-ItemProperty $websiteName/$name/SharedFolder -Name username -Value $username
Set-ItemProperty $websiteName/$name/SharedFolder -Name password -Value $password
} else {
if(Test-Path $websiteName/$name/SharedFolder)
{
Remove-WebVirtualDirectory -Site $websiteName -Application $name -Name SharedFolder
}
}
}
param(
[string] $appPoolName = $(throw "::REQUIRED PARAMETER:: -appPoolName"),
[string] $username = $(throw "::REQUIRED PARAMETER:: -username"),
[string] $password = $(throw "::REQUIRED PARAMETER:: -password"),
[string] $appPoolDotNetVersion = "v4.0",
[switch] $classicMode
)
Import-Module WebAdministration
cd IIS:\AppPools\
$newAppPool = $false
#check if the app pool exists
if (!(Test-Path $appPoolName -pathType container))
{
#create the app pool
$appPool = New-Item $appPoolName
$newAppPool = $true
}
#AppPool exists - get it.
$appPool = Get-Item $appPoolName
try
{
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"
$appPool | Set-ItemProperty -Name "ManagedPipelineMode" -Value ([int]$classicMode.ToBool())
$appPool | Set-ItemProperty -Name processmodel.username -Value ([string]$username)
$appPool | Set-ItemProperty -Name processmodel.password -Value ([string]$password)
$appPool | Set-ItemProperty -Name processmodel.identityType -Value 3
}
catch [System.Exception]
{
if($newAppPool -eq $true){
Remove-WebAppPool -Name $appPoolName
write-host "Creation of AppPool $appPoolName failed" -ForegroundColor Red
} else {
write-host "Update of AppPool $appPoolName failed" -ForegroundColor Red
}
write-host $error[0] -ForegroundColor Red
return
}
[CmdletBinding(DefaultParametersetName="None")]
Param(
[Parameter(Position=1,Mandatory=$true)][string]$baseFolder,
[Parameter(Position=2,Mandatory=$true)][string[]]$folderNames,
[Parameter(ParameterSetName="setPerms",Mandatory=$false)][alias("p")][switch]$setPermissions,
[Parameter(ParameterSetName="setPerms",Mandatory=$true)][string]$user,
[Parameter(ParameterSetName="setPerms",Mandatory=$false)][alias("r")][switch]$Read,
[Parameter(ParameterSetName="setPerms",Mandatory=$false)][alias("w")][switch]$Write,
[Parameter(ParameterSetName="setPerms",Mandatory=$false)][alias("m")][switch]$Modify,
[Parameter(ParameterSetName="setPerms",Mandatory=$false)][alias("d")][switch]$Delete
)
if(!(Test-Path $baseFolder -pathType Container))
{
ni $baseFolder -type directory
}
cd $baseFolder
function ChangeFolderPerms( $folder, $perm, $user)
{
$acl = Get-Acl $folder
$permission = $user, $perm, $InheritanceFlag, $PropagationFlag, $accessControlType
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.AddAccessRule($accessRule)
Set-Acl $folder $acl
}
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$accessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$folderNames | % {
$folder = ni .\$_ -type directory -force
write-host "Created: $folder"
if($setPermissions -eq $true)
{
if($read -eq $true)
{
$perm = "Read"
ChangeFolderPerms $folder $perm $user
}
if($write -eq $true)
{
$perm = "Write"
ChangeFolderPerms $folder $perm $user
}
if($modify -eq $true)
{
$perm = "Modify"
ChangeFolderPerms $folder $perm $user
}
if($delete -eq $true)
{
$perm = "Delete"
ChangeFolderPerms $folder $perm $user
}
}
}
param(
[string]$name = $(throw "::REQUIRED PARAMETER:: -name"),
[string]$appPoolName = $(throw "::REQUIRED PARAMETER:: -appPoolName"),
[int]$port = 80,
[string]$physicalPath = $(throw "::REQUIRED PARAMETER:: -websitePhysicalPath"),
[string]$appPoolDotNetVersion = "v4.0",
[string]$hostHeader,
[string]$username,
[string]$password,
[Switch]$appPoolPipelineModeClassic,
[Switch]$ssl
)
#Managed Pipeline mode is a magic string 0=integrated 1=classic see http://msdn.microsoft.com/en-us/library/ms690608
Import-Module WebAdministration
if(!$hostHeader){$hostHeader = $name}
#Create / Update apppool.
.\CreateAppPool @psboundparameters
#navigate to the sites root
cd IIS:\Sites\
#check if the site exists
if (Test-Path $name -pathType container)
{
#Check the appPool against one passed in.
$website = Get-Item $name
Write-host "Site $($website.Name) exists"
if($website.ApplicationPool -ne $appPoolName)
{
write-host "Updating AppPool Name to '$appPoolName'"
$website | Set-ItemProperty -Name "ApplicationPool" -Value $appPoolName
}
if($website.PhysicalPath -ne $physicalPath)
{
write-host "Updating physicalPath to $physicalPath"
$website | set-itemproperty -Name "PhysicalPath" -Value $physicalPath
}
return
}
try{
#create the site
#Check param order..
$website = New-WebSite -Name $name -Port $port -HostHeader $hostHeader -PhysicalPath $physicalPath -ApplicationPool $appPoolName #-Ssl:$ssl
#Something is not exactly right with this.
if($Ssl -eq $true){
New-WebBinding -Name $name -Protocol "https" -IP "*" -Port 443
#Find and add the certificate.
cd IIS:\SslBindings
$cert = Get-ChildItem cert:\LocalMachine\MY | Where-Object {$_.Subject -match "CN=*$hostHeader*"} | Select-Object -First 1
get-item cert:\LocalMachine\My\$cert.thumbprint | New-Item 0.0.0.0!443
}
}
catch [System.Exception]
{
write-host "Creation of Site $iisAppName failed" -ForegroundColor Red
write-host $error[0] -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment