| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2015, Henrik Wallström <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 7 |
|
| 8 |
$ErrorActionPreference = "Stop" |
| 9 |
|
| 10 |
$params = Parse-Args $args |
| 11 |
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true |
| 12 |
$application_pool = Get-AnsibleParam -obj $params -name "application_pool" -type "str" |
| 13 |
$physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" |
| 14 |
$site_id = Get-AnsibleParam -obj $params -name "site_id" -type "str" |
| 15 |
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent", "restarted", "started", "stopped" |
| 16 |
|
| 17 |
# Binding Parameters |
| 18 |
$bind_port = Get-AnsibleParam -obj $params -name "port" -type "int" |
| 19 |
$bind_ip = Get-AnsibleParam -obj $params -name "ip" -type "str" |
| 20 |
$bind_hostname = Get-AnsibleParam -obj $params -name "hostname" -type "str" |
| 21 |
|
| 22 |
# Custom site Parameters from string where properties |
| 23 |
# are separated by a pipe and property name/values by colon. |
| 24 |
# Ex. "foo:1|bar:2" |
| 25 |
$parameters = Get-AnsibleParam -obj $params -name "parameters" -type "str" |
| 26 |
if ($null -ne $parameters) { |
| 27 |
$parameters = @($parameters -split '\|' | ForEach-Object { |
| 28 |
return , ($_ -split "\:", 2) |
| 29 |
}) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
# Ensure WebAdministration module is loaded |
| 34 |
if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) { |
| 35 |
Import-Module WebAdministration |
| 36 |
} |
| 37 |
|
| 38 |
# Result |
| 39 |
$result = @{ |
| 40 |
site = @{} |
| 41 |
changed = $false |
| 42 |
} |
| 43 |
|
| 44 |
# Site info |
| 45 |
$site = Get-Website | Where-Object { $_.Name -eq $name } |
| 46 |
|
| 47 |
Try { |
| 48 |
# Add site |
| 49 |
If (($state -ne 'absent') -and (-not $site)) { |
| 50 |
If (-not $physical_path) { |
| 51 |
Fail-Json -obj $result -message "missing required arguments: physical_path" |
| 52 |
} |
| 53 |
ElseIf (-not (Test-Path -LiteralPath $physical_path)) { |
| 54 |
Fail-Json -obj $result -message "specified folder must already exist: physical_path" |
| 55 |
} |
| 56 |
|
| 57 |
$site_parameters = @{ |
| 58 |
Name = $name |
| 59 |
PhysicalPath = $physical_path |
| 60 |
} |
| 61 |
|
| 62 |
If ($application_pool) { |
| 63 |
$site_parameters.ApplicationPool = $application_pool |
| 64 |
} |
| 65 |
|
| 66 |
If ($site_id) { |
| 67 |
$site_parameters.ID = $site_id |
| 68 |
} |
| 69 |
|
| 70 |
If ($bind_port) { |
| 71 |
$site_parameters.Port = $bind_port |
| 72 |
} |
| 73 |
|
| 74 |
If ($bind_ip) { |
| 75 |
$site_parameters.IPAddress = $bind_ip |
| 76 |
} |
| 77 |
|
| 78 |
If ($bind_hostname) { |
| 79 |
$site_parameters.HostHeader = $bind_hostname |
| 80 |
} |
| 81 |
|
| 82 |
# Fix for error "New-Item : Index was outside the bounds of the array." |
| 83 |
# This is a bug in the New-WebSite commandlet. Apparently there must be at least one site configured in IIS otherwise New-WebSite crashes. |
| 84 |
# For more details, see http://stackoverflow.com/questions/3573889/ps-c-new-website-blah-throws-index-was-outside-the-bounds-of-the-array |
| 85 |
$sites_list = Get-ChildItem -LiteralPath IIS:\sites |
| 86 |
if ($null -eq $sites_list) { |
| 87 |
if ($site_id) { |
| 88 |
$site_parameters.ID = $site_id |
| 89 |
} |
| 90 |
else { |
| 91 |
$site_parameters.ID = 1 |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$site = New-Website @site_parameters -Force |
| 96 |
$result.changed = $true |
| 97 |
} |
| 98 |
|
| 99 |
# Remove site |
| 100 |
If ($state -eq 'absent' -and $site) { |
| 101 |
$site = Remove-Website -Name $name |
| 102 |
$result.changed = $true |
| 103 |
} |
| 104 |
|
| 105 |
$site = Get-Website | Where-Object { $_.Name -eq $name } |
| 106 |
If ($site) { |
| 107 |
# Change Physical Path if needed |
| 108 |
if ($physical_path) { |
| 109 |
If (-not (Test-Path -LiteralPath $physical_path)) { |
| 110 |
Fail-Json -obj $result -message "specified folder must already exist: physical_path" |
| 111 |
} |
| 112 |
|
| 113 |
$folder = Get-Item -LiteralPath $physical_path |
| 114 |
If ($folder.FullName -ne $site.PhysicalPath) { |
| 115 |
Set-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" -name physicalPath -value $folder.FullName |
| 116 |
$result.changed = $true |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
# Change Application Pool if needed |
| 121 |
if ($application_pool) { |
| 122 |
If ($application_pool -ne $site.applicationPool) { |
| 123 |
Set-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" -name applicationPool -value $application_pool |
| 124 |
$result.changed = $true |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
# Set properties |
| 129 |
if ($parameters) { |
| 130 |
$parameters | ForEach-Object { |
| 131 |
$property_value = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" $_[0] |
| 132 |
|
| 133 |
switch ($property_value.GetType().Name) { |
| 134 |
"ConfigurationAttribute" { $parameter_value = $property_value.value } |
| 135 |
"String" { $parameter_value = $property_value } |
| 136 |
} |
| 137 |
|
| 138 |
if ((-not $parameter_value) -or ($parameter_value) -ne $_[1]) { |
| 139 |
Set-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" $_[0] $_[1] |
| 140 |
$result.changed = $true |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
# Set run state |
| 146 |
if ((($state -eq 'stopped') -or ($state -eq 'restarted')) -and ($site.State -eq 'Started')) { |
| 147 |
Stop-Website -Name $name -ErrorAction Stop |
| 148 |
$result.changed = $true |
| 149 |
} |
| 150 |
if ((($state -eq 'started') -and ($site.State -eq 'Stopped')) -or ($state -eq 'restarted')) { |
| 151 |
Start-Website -Name $name -ErrorAction Stop |
| 152 |
$result.changed = $true |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
Catch { |
| 157 |
Fail-Json -obj $result -message $_.Exception.Message |
| 158 |
} |
| 159 |
|
| 160 |
if ($state -ne 'absent') { |
| 161 |
$site = Get-Website | Where-Object { $_.Name -eq $name } |
| 162 |
} |
| 163 |
|
| 164 |
if ($site) { |
| 165 |
$result.site = @{ |
| 166 |
Name = $site.Name |
| 167 |
ID = $site.ID |
| 168 |
State = $site.State |
| 169 |
PhysicalPath = $site.PhysicalPath |
| 170 |
ApplicationPool = $site.applicationPool |
| 171 |
Bindings = @($site.Bindings.Collection | ForEach-Object { $_.BindingInformation }) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
Exit-Json -obj $result |