Skip to content
This repository was archived by the owner on Mar 11, 2020. It is now read-only.

Commit c660cf3

Browse files
author
Mano Marks
authored
Merge pull request #35 from sixeyed/master
Support for IIS & ASP.NET on Windows Server 2003
2 parents f61839f + af69a49 commit c660cf3

File tree

6 files changed

+276
-102
lines changed

6 files changed

+276
-102
lines changed

Functions/Private/Artifacts/IIS/Discover_IIS.ps1

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -37,84 +37,25 @@ $ManifestResult = @{
3737
Status = ''
3838
}
3939

40-
$WindowsFeatures = Get-WindowsOptionalFeature -Path $MountPath
41-
$IIS = $WindowsFeatures.Where{$_.FeatureName -eq 'IIS-WebServer'}
42-
$EnabledFeatures = $WindowsFeatures.Where{$_.State -eq 'Enabled'}
43-
$FeaturesToExport = $EnabledFeatures.Where{$_.FeatureName -match 'IIS'-or
44-
$_.FeatureName -match 'ASPNET' -or
45-
$_.FeatureName -match 'Asp-Net' -and
46-
$_.FeatureName -NotMatch 'Management'} | Sort-Object FeatureName | Select-Object -ExpandProperty FeatureName
47-
48-
if ($IIS.State -eq 'Enabled') {
49-
50-
$IISConfig = [xml](Get-Content -Path $MountPath\Windows\System32\inetsrv\config\applicationHost.config)
51-
52-
$AllSites = $IISConfig | Select-Xml -XPath "//sites" | Select-Object -ExpandProperty Node
53-
$siteDefaults = $AllSites.siteDefaults
54-
$applicationDefaults = $AllSites.applicationDefaults
55-
$virtualDirectoryDefaults = $AllSites.virtualDirectoryDefaults
56-
$sites = $AllSites.site
57-
if ($ArtifactParam) {
58-
$sites = $sites.where{$_.name -in $ArtifactParam }
59-
}
60-
$Websites = New-Object System.Collections.ArrayList
61-
ForEach ($site in $sites) {
62-
$Websites.add([PSCustomObject]@{
63-
Name = $site.name;
64-
ID = $site.id;
65-
ApplicationPool = $site.application.ApplicationPool
66-
PhysicalPath = $site.application.virtualDirectory.physicalPath.replace('%SystemDrive%\','\').replace('C:\','\').Replace('c:\','\');
67-
Binding = [PSCustomObject]@{ Protocol = $site.bindings.binding.Protocol;
68-
BindingInformation = $site.bindings.binding.bindingInformation } }) | Out-Null
69-
}
70-
71-
$AllApplicationPools = $IISConfig | Select-Xml -XPath "//applicationPools" | Select-Object -ExpandProperty Node
72-
$ApplicationPools = $AllApplicationPools.add.name
73-
$ApplicationPoolDefaults = $allApplicationPools.applicationPoolDefaults
74-
$appPools = [PSCustomObject]@{
75-
applicationPools = $ApplicationPools
76-
applicationPoolDefaults = [PSCustomObject]@{managedRuntimeVersion = $ApplicationPoolDefaults.managedRuntimeVersion;
77-
processModel = [PSCustomObject]@{ identityType = $ApplicationPoolDefaults.processModel.identityType }
78-
}
79-
}
80-
$HandlerList = $IISConfig | Select-Xml -XPath "//handlers" | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty add
81-
82-
$DefaultHandlers = [xml](Get-Content $PSScriptRoot\DefaultHandlers.xml) | Select-Xml -XPath "//handlers" | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty add
83-
$handlers = New-object System.Collections.ArrayList
84-
85-
foreach ($Handler in $HandlerList) {
86-
if (-not $DefaultHandlers.name -match $handler.Name) {
87-
88-
$handlers.Add([PSCustomObject]@{
89-
Name = $Handler.name
90-
Path = $Handler.path
91-
Verb = $Handler.verb
92-
}) | Out-Null
93-
}
40+
try {
41+
$WindowsFeatures = Get-WindowsOptionalFeature -Path $MountPath
42+
$ManifestResult = GetManifestFromApplicationHost -OutputPath $OutputPath -MountPath $Mount.Path -ArtifactParam $ArtifactParam
43+
}
44+
catch {
45+
$ManifestResult = GetManifestFromMetabase -OutputPath $OutputPath -MountPath $Mount.Path -ArtifactParam $ArtifactParam
9446
}
95-
Write-Verbose -Message 'IIS service is present on the system'
96-
$ManifestResult.FeatureName = $FeaturesToExport -join ';'
97-
$ManifestResult.Status = 'Present'
98-
$ManifestResult.Websites = $Websites
99-
$ManifestResult.ApplicationPools = $appPools
100-
$ManifestResult.HttpHandlers = $handlers
101-
$ManifestResult.SiteDefaults = $siteDefaults
102-
$ManifestResult.ApplicationDefaults = $applicationDefaults
103-
$ManifestResult.VirtualDirectoryDefaults = $virtualDirectoryDefaults
10447

105-
if ($ManifestResult.FeatureName -like '*ASPNET*' -or $ManifestResult.FeatureName -like '*Asp-Net*'){
106-
Write-Verbose -Message 'ASP.NET is present on the system'
107-
$ManifestResult.AspNetStatus = 'Present'
48+
if ($ManifestResult.Status -eq 'Present'){
49+
Write-Verbose -Message 'IIS service is present on the system'
50+
if ($ManifestResult.AspNetStatus -eq 'Present'){
51+
Write-Verbose -Message 'ASP.NET is present on the system'
10852
}
10953
else {
11054
Write-Verbose -Message 'ASP.NET is NOT present on the system'
111-
$ManifestResult.AspNetStatus = 'Absent'
11255
}
11356
}
11457
else {
115-
Write-Verbose -Message 'IIS service is NOT present on the system'
116-
$ManifestResult.Status = 'Absent'
117-
$ManifestResult.AspNetStatus = 'Absent'
58+
Write-Verbose -Message 'IIS service is NOT present on the system'
11859
}
11960

12061
### Write the result to the manifest file

Functions/Private/Artifacts/IIS/Generate_IIS.ps1

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ $ArtifactName = Split-Path -Path $PSScriptRoot -Leaf
2424

2525
Write-Verbose -Message ('Generating result for {0} component' -f (Split-Path -Path $PSScriptRoot -Leaf))
2626
$Manifest = '{0}\{1}.json' -f $ManifestPath, $ArtifactName
27+
$ResultBuilder = New-Object System.Text.StringBuilder
2728

2829
$Artifact = Get-Content -Path $Manifest -Raw | ConvertFrom-Json
2930

3031
if ($Artifact.Status -eq 'Present') {
3132
Write-Verbose ('Copying {0} configuration files' -f $ArtifactName)
3233
$ConfigPath = $Mount.Path + "\" + "Windows\System32\inetsrv\config"
33-
Copy-Item $ConfigPath $ManifestPath -Recurse
34-
35-
$ResultBuilder = New-Object System.Text.StringBuilder
34+
if (Test-Path -Path $ConfigPath) {
35+
Copy-Item $ConfigPath $ManifestPath -Recurse
36+
}
3637

3738
Write-Verbose -Message ('Writing instruction to install IIS')
3839
$null = $ResultBuilder.AppendLine('# Install Windows features for IIS')
@@ -42,17 +43,25 @@ if ($Artifact.Status -eq 'Present') {
4243
$null = $ResultBuilder.Append(', NET-Framework-45-ASPNET, Web-Asp-Net45')
4344
}
4445
$null = $ResultBuilder.AppendLine('')
45-
$null = $ResultBuilder.AppendLine("RUN Enable-WindowsOptionalFeature -Online -FeatureName $($Artifact.FeatureName.Replace(';',','))")
46+
47+
if ($Artifact.FeatureName.length -gt 0) {
48+
$null = $ResultBuilder.AppendLine("RUN Enable-WindowsOptionalFeature -Online -FeatureName $($Artifact.FeatureName.Replace(';',','))")
49+
$null = $ResultBuilder.AppendLine('')
50+
}
4651

4752
if ($Artifact.HttpHandlers.Count > 0) {
4853
Write-Verbose -Message ('Writing instruction to add HTTP handlers')
4954
$null = $ResultBuilder.Append('RUN ')
5055
foreach ($HttpHandler in $Artifact.HttpHandlers) {
5156
$null = $ResultBuilder.AppendLine('New-WebHandler -Name "{0}" -Path "{1}" -Verb "{2}" `' -f $HttpHandler.Name, $HttpHandler.Path, $HttpHandler.Verb)
5257
}
53-
}
58+
$null = $ResultBuilder.AppendLine('')
59+
}
60+
5461
$null = $ResultBuilder.AppendLine('')
62+
$null = $ResultBuilder.AppendLine("RUN Remove-Website 'Default Web Site'")
5563

64+
$null = $ResultBuilder.AppendLine('')
5665
for ($i=0;$i -lt $Artifact.Websites.Count;$i++) {
5766
$Site = $Artifact.Websites[$i]
5867
$SitePath = $Mount.Path + $Site.PhysicalPath
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
function GetManifestFromApplicationHost {
2+
<#
3+
.SYNOPSIS
4+
Scans for presence of the Internet Information Services (IIS) Web Server
5+
6+
.PARAMETER MountPath
7+
The path where the Windows image was mounted to.
8+
9+
.PARAMETER OutputPath
10+
The filesystem path where the discovery manifest will be emitted.
11+
12+
.PARAMETER ArtifactParam
13+
Optional - one or more Website names to include in the output.
14+
#>
15+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess",'')]
16+
[CmdletBinding()]
17+
param (
18+
[Parameter(Mandatory = $true)]
19+
[string] $MountPath,
20+
21+
[Parameter(Mandatory = $true)]
22+
[string] $OutputPath,
23+
24+
[Parameter(Mandatory = $false)]
25+
[string[]] $ArtifactParam
26+
)
27+
28+
$ManifestResult = @{
29+
FeatureName = ''
30+
Status = ''
31+
}
32+
33+
$WindowsFeatures = Get-WindowsOptionalFeature -Path $MountPath
34+
$IIS = $WindowsFeatures.Where{$_.FeatureName -eq 'IIS-WebServer'}
35+
$EnabledFeatures = $WindowsFeatures.Where{$_.State -eq 'Enabled'}
36+
$FeaturesToExport = $EnabledFeatures.Where{$_.FeatureName -match 'IIS'-or
37+
$_.FeatureName -match 'ASPNET' -or
38+
$_.FeatureName -match 'Asp-Net' -and
39+
$_.FeatureName -NotMatch 'Management'} | Sort-Object FeatureName | Select-Object -ExpandProperty FeatureName
40+
41+
if ($IIS.State -eq 'Enabled') {
42+
43+
$IISConfig = [xml](Get-Content -Path $MountPath\Windows\System32\inetsrv\config\applicationHost.config)
44+
45+
$AllSites = $IISConfig | Select-Xml -XPath "//sites" | Select-Object -ExpandProperty Node
46+
$siteDefaults = $AllSites.siteDefaults
47+
$applicationDefaults = $AllSites.applicationDefaults
48+
$virtualDirectoryDefaults = $AllSites.virtualDirectoryDefaults
49+
$sites = $AllSites.site
50+
if ($ArtifactParam) {
51+
$sites = $sites.where{$_.name -in $ArtifactParam }
52+
}
53+
$Websites = New-Object System.Collections.ArrayList
54+
ForEach ($site in $sites) {
55+
$Websites.add([PSCustomObject]@{
56+
Name = $site.name;
57+
ID = $site.id;
58+
ApplicationPool = $site.application.ApplicationPool
59+
PhysicalPath = $site.application.virtualDirectory.physicalPath.replace('%SystemDrive%\','\').replace('C:\','\').Replace('c:\','\');
60+
Binding = [PSCustomObject]@{ Protocol = $site.bindings.binding.Protocol;
61+
BindingInformation = $site.bindings.binding.bindingInformation } }) | Out-Null
62+
}
63+
64+
$AllApplicationPools = $IISConfig | Select-Xml -XPath "//applicationPools" | Select-Object -ExpandProperty Node
65+
$ApplicationPools = $AllApplicationPools.add.name
66+
$ApplicationPoolDefaults = $allApplicationPools.applicationPoolDefaults
67+
$appPools = [PSCustomObject]@{
68+
applicationPools = $ApplicationPools
69+
applicationPoolDefaults = [PSCustomObject]@{managedRuntimeVersion = $ApplicationPoolDefaults.managedRuntimeVersion;
70+
processModel = [PSCustomObject]@{ identityType = $ApplicationPoolDefaults.processModel.identityType }
71+
}
72+
}
73+
$HandlerList = $IISConfig | Select-Xml -XPath "//handlers" | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty add
74+
75+
$DefaultHandlers = [xml](Get-Content $PSScriptRoot\DefaultHandlers.xml) | Select-Xml -XPath "//handlers" | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty add
76+
$handlers = New-object System.Collections.ArrayList
77+
78+
foreach ($Handler in $HandlerList) {
79+
if (-not $DefaultHandlers.name -match $handler.Name) {
80+
81+
$handlers.Add([PSCustomObject]@{
82+
Name = $Handler.name
83+
Path = $Handler.path
84+
Verb = $Handler.verb
85+
}) | Out-Null
86+
}
87+
}
88+
Write-Verbose -Message 'IIS service is present on the system'
89+
$ManifestResult.FeatureName = $FeaturesToExport -join ';'
90+
$ManifestResult.Status = 'Present'
91+
$ManifestResult.Websites = $Websites
92+
$ManifestResult.ApplicationPools = $appPools
93+
$ManifestResult.HttpHandlers = $handlers
94+
$ManifestResult.SiteDefaults = $siteDefaults
95+
$ManifestResult.ApplicationDefaults = $applicationDefaults
96+
$ManifestResult.VirtualDirectoryDefaults = $virtualDirectoryDefaults
97+
98+
if ($ManifestResult.FeatureName -like '*ASPNET*' -or $ManifestResult.FeatureName -like '*Asp-Net*'){
99+
$ManifestResult.AspNetStatus = 'Present'
100+
}
101+
else {
102+
$ManifestResult.AspNetStatus = 'Absent'
103+
}
104+
}
105+
else {
106+
$ManifestResult.Status = 'Absent'
107+
$ManifestResult.AspNetStatus = 'Absent'
108+
}
109+
110+
return $ManifestResult
111+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
function GetManifestFromMetabase {
2+
<#
3+
.SYNOPSIS
4+
Scans for presence of the Internet Information Services (IIS) Web Server on Windows Server 2003 images.
5+
6+
.PARAMETER MountPath
7+
The path where the Windows image was mounted to.
8+
9+
.PARAMETER OutputPath
10+
The filesystem path where the discovery manifest will be emitted.
11+
12+
.PARAMETER ArtifactParam
13+
Optional - one or more Website names to include in the output.
14+
#>
15+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess",'')]
16+
[CmdletBinding()]
17+
param (
18+
[Parameter(Mandatory = $true)]
19+
[string] $MountPath,
20+
21+
[Parameter(Mandatory = $true)]
22+
[string] $OutputPath,
23+
24+
[Parameter(Mandatory = $false)]
25+
[string[]] $ArtifactParam
26+
)
27+
28+
$ManifestResult = @{
29+
FeatureName = ''
30+
Status = ''
31+
}
32+
33+
$MetabasePath = "$MountPath\Windows\System32\inetsrv\MetaBase.xml"
34+
35+
if (Test-Path -Path $MetabasePath) {
36+
37+
$IISConfig = [xml](Get-Content -Path $MetabasePath)
38+
39+
$AspNetInstalled = $false
40+
ForEach ($svc in $IISConfig.configuration.MBProperty.IIsWebService) {
41+
if ($svc.ApplicationDependencies.Contains('ASP.NET')) {
42+
$AspNetInstalled = $true
43+
break
44+
}
45+
}
46+
47+
$Sites = New-Object System.Collections.ArrayList
48+
ForEach ($site in $IISConfig.configuration.MBProperty.IIsWebServer) {
49+
if ($site.ServerBindings -ne $null){
50+
$Sites.add([PSCustomObject]@{
51+
Name = $site.ServerComment;
52+
ID = $site.Location;
53+
Bindings = $site.ServerBindings }) | Out-Null
54+
}
55+
}
56+
Write-Verbose -Message "Found: $($Sites.Count) sites"
57+
58+
$apps = $IISConfig.configuration.MBProperty.IIsWebVirtualDir
59+
if ($ArtifactParam) {
60+
$apps = $apps.where{$_.AppFriendlyName -in $ArtifactParam }
61+
}
62+
63+
$Websites = New-Object System.Collections.ArrayList
64+
ForEach ($app in $apps) {
65+
if ($app.Path -ne $null){
66+
$siteID = $app.Location.Substring(0, $app.Location.ToLower().IndexOf('/root'))
67+
$site = $Sites.where({$_.ID -eq $siteID})
68+
$Websites.add([PSCustomObject]@{
69+
Name = $app.AppFriendlyName;
70+
ID = $app.Location;
71+
ApplicationPool = $app.AppPoolId;
72+
PhysicalPath = $app.Path.replace('%SystemDrive%\','\').replace('C:\','\').Replace('c:\','\');
73+
Binding = [PSCustomObject]@{ Protocol = 'http'; #TODO - discover protocol from metabase
74+
BindingInformation = "*" + $site.Bindings } }) | Out-Null
75+
}
76+
}
77+
78+
$ManifestResult.FeatureName = ''
79+
$ManifestResult.Status = 'Present'
80+
$ManifestResult.Websites = $Websites
81+
$ManifestResult.ApplicationPools = New-object System.Collections.ArrayList
82+
$ManifestResult.HttpHandlers = New-object System.Collections.ArrayList
83+
$ManifestResult.SiteDefaults = New-object System.Collections.ArrayList
84+
$ManifestResult.ApplicationDefaults =New-object System.Collections.ArrayList
85+
$ManifestResult.VirtualDirectoryDefaults = New-object System.Collections.ArrayList
86+
87+
if ($AspNetInstalled -eq $true){
88+
$ManifestResult.AspNetStatus = 'Present'
89+
}
90+
else {
91+
$ManifestResult.AspNetStatus = 'Absent'
92+
}
93+
}
94+
else {
95+
$ManifestResult.Status = 'Absent'
96+
$ManifestResult.AspNetStatus = 'Absent'
97+
}
98+
99+
return $ManifestResult
100+
}

Image2Docker.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'Image2Docker.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '1.7.2'
15+
ModuleVersion = '1.7.3'
1616

1717
# Supported PSEditions
1818
### NOTE: This module will not work with PowerShell Core.

0 commit comments

Comments
 (0)