1 #!powershell
2 
3 # Copyright: (c) 2015, Henrik Wallström <henrik@wallstroms.nu>
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 $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 -Path IIS:\sites
86     if ($null -eq $sites_list) {
87       if ($site_id) {
88         $site_parameters.ID = $site_id
89       } else {
90         $site_parameters.ID = 1
91       }
92     }
93 
94     $site = New-Website @site_parameters -Force
95     $result.changed = $true
96   }
97 
98   # Remove site
99   If ($state -eq 'absent' -and $site) {
100     $site = Remove-Website -Name $name
101     $result.changed = $true
102   }
103 
104   $site = Get-Website | Where-Object { $_.Name -eq $name }
105   If($site) {
106     # Change Physical Path if needed
107     if($physical_path) {
108       If (-not (Test-Path $physical_path)) {
109         Fail-Json -obj $result -message "specified folder must already exist: physical_path"
110       }
111 
112       $folder = Get-Item $physical_path
113       If($folder.FullName -ne $site.PhysicalPath) {
114         Set-ItemProperty "IIS:\Sites\$($site.Name)" -name physicalPath -value $folder.FullName
115         $result.changed = $true
116       }
117     }
118 
119     # Change Application Pool if needed
120     if($application_pool) {
121       If($application_pool -ne $site.applicationPool) {
122         Set-ItemProperty "IIS:\Sites\$($site.Name)" -name applicationPool -value $application_pool
123         $result.changed = $true
124       }
125     }
126 
127     # Set properties
128     if($parameters) {
129       $parameters | ForEach-Object {
130         $property_value = Get-ItemProperty "IIS:\Sites\$($site.Name)" $_[0]
131 
132         switch ($property_value.GetType().Name)
133         {
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     {
148       Stop-Website -Name $name -ErrorAction Stop
149       $result.changed = $true
150     }
151     if ((($state -eq 'started') -and ($site.State -eq 'Stopped')) -or ($state -eq 'restarted'))
152     {
153       Start-Website -Name $name -ErrorAction Stop
154       $result.changed = $true
155     }
156   }
157 }
158 Catch
159 {
160   Fail-Json -obj $result -message $_.Exception.Message
161 }
162 
163 if ($state -ne 'absent')
164 {
165   $site = Get-Website | Where-Object { $_.Name -eq $name }
166 }
167 
168 if ($site)
169 {
170   $result.site = @{
171     Name = $site.Name
172     ID = $site.ID
173     State = $site.State
174     PhysicalPath = $site.PhysicalPath
175     ApplicationPool = $site.applicationPool
176     Bindings = @($site.Bindings.Collection | ForEach-Object { $_.BindingInformation })
177   }
178 }
179 
180 Exit-Json -obj $result
181