-
-
Save BryanWilhite/e54408801bc9bef3fc83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# shared functions for IIS | |
# | |
# based on “Basic Power-Shell cmdlets to work with IIS 7+” https://gist.github.com/sergejusb/5304386 | |
# | |
Import-Module WebAdministration | |
function Add-PhysicalPath($PhysicalPath) | |
{ | |
if(-not(Test-Path $PhysicalPath)) | |
{ | |
Write-Output "Path $PhysicalPath does not exist. Generating..." | |
New-Item -Force -ItemType directory -Path $PhysicalPath | |
} | |
} | |
function Add-WebApplication($Site, $AppName, $PhysicalPath, $ApplicationPool = "DefaultAppPool") | |
{ | |
if ((Get-WebApplication -Name $AppName -Site $Site) -ne $null) | |
{ | |
Write-Warning "Web application $AppName already exists. Removing..." | |
Remove-WebApplication -Site $Site -Name $AppName | |
if (Test-Path "$PhysicalPath\Web.config") { Remove-Item "$PhysicalPath\Web.config" } | |
} | |
Add-PhysicalPath $PhysicalPath | |
Write-Output "Adding web application $AppName..." | |
$webApp = New-WebApplication -Site $Site -Name $AppName -PhysicalPath $PhysicalPath -ApplicationPool $ApplicationPool | |
return Get-Item "IIS:\Sites\$Site\$AppName" | |
} | |
function Add-WebAppPool($PoolName, $Identity = "NetworkService", $RuntimeVersion = "v4.0", $PipelineMode = "Integrated") | |
{ | |
if (Test-Path "IIS:\AppPools\$PoolName") | |
{ | |
Write-Warning "Application pool $PoolName already exists. Removing..." | |
Remove-WebAppPool -Name $PoolName | |
} | |
Write-Output "Adding application pool $PoolName..." | |
$appPool = New-WebAppPool -Name $PoolName | |
$appPool.ManagedRuntimeVersion = $RuntimeVersion | |
$appPool.ManagedPipelineMode = $PipelineMode | |
$appPool.ProcessModel.IdentityType = $Identity | |
$appPool | Set-Item | |
return Get-Item "IIS:\AppPools\$PoolName" | |
} | |
function Add-Website($Site, $PhysicalPath, $ApplicationPool = "DefaultAppPool", $Port = 80) | |
{ | |
if (Test-Path "IIS:\Sites\$Site") | |
{ | |
Write-Warning "Web site $Site already exists. Removing..." | |
Remove-Website -Name $Site | |
if (Test-Path "$PhysicalPath\Web.config") { Remove-Item "$PhysicalPath\Web.config" } | |
} | |
Add-PhysicalPath $PhysicalPath | |
Write-Output "Generating web site $Site..." | |
$webSite = New-Website -Name $Site -PhysicalPath $PhysicalPath -ApplicationPool $ApplicationPool -Port $Port | |
$webSite.ServerAutoStart = $true | |
$webSite | Set-Item | |
return Get-Item "IIS:\Sites\$Site" | |
} | |
function Add-WebsiteDefaultDocument($Site, $Document) | |
{ | |
$path = "IIS:\Sites\$Site" | |
$addFilter = "system.webServer/defaultDocument/files" | |
$getFilter = "$addFilter/*" | |
Write-Output "Adding default document, $Document, to $path..." | |
if(Get-WebConfiguration -Filter $getFilter -PSPath $path | Where-Object -Property value -EQ $Document) | |
{ | |
Write-Warning "Default document, $Document, is already specified." | |
return | |
} | |
Add-WebConfiguration -PSPath $path -Filter $addFilter -Value $Document | |
} | |
function Add-WebsiteDefaultDocuments($Site, $Documents) | |
{ | |
$a = $Documents.Split(',') | |
[System.Array]::Reverse($a) | |
foreach ($i in $a) { Add-WebsiteDefaultDocument $Site $i.ToLower() } | |
} | |
function Add-WebVirtualDirectory($Site, $DirName, $PhysicalPath, $Application = $null) | |
{ | |
if (-not(Test-Path "IIS:\Sites\$Site")) | |
{ | |
Write-Warning "Web site $Site does not exist. Cannot add Virtual Directory." | |
return $null | |
} | |
if ((Get-WebVirtualDirectory -Name $DirName -Site $Site) -ne $null) | |
{ | |
Write-Warning "Web virtual directory $DirName already exists. Removing..." | |
Remove-WebVirtualDirectory -Site $Site -Name $DirName | |
} | |
Add-PhysicalPath $PhysicalPath | |
Write-Output "Adding web virtual directory $DirName..." | |
$webDir = New-WebVirtualDirectory -Site $Site -Name $DirName -PhysicalPath $PhysicalPath -Application $Application | |
return Get-Item "IIS:\Sites\$Site\$DirName" | |
} | |
function Get-AspNetRegistrations() | |
{ | |
$path = "HKLM:\SOFTWARE\Microsoft\ASP.NET" | |
if(-not(Test-Path $path)) | |
{ | |
Write-Warning "ASP.NET does not appear to be installed on this machine." | |
return $null | |
} | |
return Get-ChildItem $path | Get-ItemProperty | |
} | |
function Get-WebApplicationPowerShellPathMeta($PowerShellPath) | |
{ | |
$a = $PowerShellPath.Split("\") | |
if($a.Length -gt 3) | |
{ | |
$hash= @{ PSDrive = $a[0]; Root = $a[1]; Site = $a[2]; AppName = $a[3] } | |
return New-Object -Property $hash -TypeName psobject | |
} | |
else | |
{ | |
return $null; | |
} | |
} | |
function Get-WebApplicationPowerShellPaths() | |
{ | |
$sites = Get-Website | |
$apps = Get-WebApplication | |
$paths = @() | |
foreach($i in $apps) | |
{ | |
$site = $sites | where { $i.PhysicalPath.StartsWith($_.PhysicalPath) } | |
$paths += "IIS:\Sites\$($site.Name)\$($i.path.Substring(1))" | |
} | |
return $paths | |
} | |
function Get-WebApplicationSummary($PowerShellPath, $IncludeAppPoolData = $false) | |
{ | |
$meta = Get-WebApplicationPowerShellPathMeta($PowerShellPath) | |
if($meta -eq $null) | |
{ | |
Write-Warning "PSPath for $PowerShellPath was parsed incorrectly. Cannot generate summary." | |
return | |
} | |
$element = Get-WebApplication -Site $meta.Site -Name $meta.AppName | |
$hash = @{} | |
$hash.Add("PowerShellPath", $PowerShellPath) | |
$hash.Add("PSPath", $element.PSPath) | |
$hash.Add("PhysicalPath", $element.PhysicalPath) | |
foreach($i in $element.Attributes) | |
{ | |
$name = [string]::Concat($i.Name.Substring(0, 1).ToUpperInvariant(), $i.Name.Substring(1)) | |
$hash.Add($name, $i.Value) | |
} | |
$name = "ConfigCompilationDebug" | |
$value = Get-WebConfigurationValue $PowerShellPath /system.web/compilation/@debug | |
$hash.Add($name, $value) | |
$name = "ConfigCompilationTargetFramework" | |
$value = Get-WebConfigurationValue $PowerShellPath /system.web/compilation/@targetFramework | |
$hash.Add($name, $value) | |
$name = "ConfigHttpRuntimeTargetFramework" | |
$value = Get-WebConfigurationValue $PowerShellPath /system.web/httpRuntime/@targetFramework | |
$hash.Add($name, $value) | |
if($IncludeAppPoolData) | |
{ | |
$key = "ApplicationPool"; | |
if(-not($hash.ContainsKey($key))) | |
{ | |
Write-Warning "Web Configuration Element attribute $key does not exist. Cannot read Application Pool." | |
} | |
else | |
{ | |
$pool = Get-Item "IIS:\AppPools\$($hash[$key])" | |
#discover new properties: | |
#$pool.cpu.RawAttributes | oh | |
$hash.Add("AppPoolAutoStart", $pool.autoStart) | |
$hash.Add("AppPoolEnable32BitAppOnWin64", $pool.enable32BitAppOnWin64) | |
$hash.Add("AppPoolManagedManagedPipelineMode", $pool.managedPipelineMode) | |
$hash.Add("AppPoolManagedRuntimeVersion", $pool.managedRuntimeVersion) | |
} | |
} | |
$orderedHash = [ordered]@{} | |
foreach($i in $hash.Keys.GetEnumerator() | sort) { $orderedHash.Add($i, $hash[$i]) } | |
return New-Object -Property $orderedHash -TypeName psobject | |
} | |
function Get-WebConfigurationValue($PowerShellPath, $XPath) | |
{ | |
$value = (Get-WebConfigurationProperty -PSPath $PowerShellPath -Filter $XPath -Name Value).Value | |
return $value | |
} | |
function Get-WebSiteById($Id) | |
{ | |
return Get-Website | Where-Object -Property ID -Value $Id -EQ | |
} | |
function Restore-PermissionsForWebServerGroup($Path) | |
{ | |
if(-not(Test-Path $Path)) | |
{ | |
Write-Warning "Path $Path was not found. Unable to restore permissions." | |
return | |
} | |
& TAKEOWN /f $Path /a | |
& ICACLS $Path /reset /t | |
& ICACLS $Path ` | |
/grant IIS_IUSRS:(CI)(OI)(IO)(RX) ` | |
/t /l /q | |
} | |
function Write-WebConfigFile($PhysicalPath, $TargetFramework="default") | |
{ | |
if([string]::IsNullOrEmpty($TargetFramework)) { $TargetFramework = "default" } | |
if($TargetFramework.StartsWith("v")) { $TargetFramework = $TargetFramework.Substring(1) } | |
if($TargetFramework.Equals("default") -or $TargetFramework.Equals("4.0")) { $TargetFramework = "4.5" } | |
$compilationTargetFramework = "" | |
if($TargetFramework.StartsWith("4")) | |
{ | |
$compilationTargetFramework = @" | |
targetFramework="$TargetFramework" | |
"@ | |
} | |
$xml = @" | |
<?xml version="1.0" encoding="utf-8" ?> | |
<!-- | |
For more information on how to configure your ASP.NET application, please visit | |
http://go.microsoft.com/fwlink/?LinkId=301880 and http://www.iis.net/configreference | |
--> | |
<configuration> | |
<appSettings></appSettings> | |
<system.web> | |
<compilation debug="false" $compilationTargetFramework /> | |
<httpRuntime targetFramework="$TargetFramework" /> | |
</system.web> | |
<system.webServer> | |
<defaultDocument enabled="true"> | |
<files> | |
<add value="default.html" /> | |
</files> | |
</defaultDocument> | |
</system.webServer> | |
<runtime></runtime> | |
</configuration> | |
"@ | |
$xml | Out-File -FilePath "$PhysicalPath\Web.config" -Encoding utf8 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment