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 $params = Parse-Args $args -supports_check_mode $true
9 $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
10 
11 $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
12 $site = Get-AnsibleParam -obj $params -name "site" -type "str" -failifempty $true
13 $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present"
14 $physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" -aliases "path"
15 $application_pool = Get-AnsibleParam -obj $params -name "application_pool" -type "str"
16 $connect_as = Get-AnsibleParam -obj $params -name 'connect_as' -type 'str' -validateset 'specific_user', 'pass_through'
17 $username = Get-AnsibleParam -obj $params -name "username" -type "str" -failifempty ($connect_as -eq 'specific_user')
18 $password = Get-AnsibleParam -obj $params -name "password" -type "str" -failifempty ($connect_as -eq 'specific_user')
19 
20 $result = @{
21   application_pool = $application_pool
22   changed = $false
23   physical_path = $physical_path
24 }
25 
26 # Ensure WebAdministration module is loaded
27 if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) {
28   Import-Module WebAdministration
29 }
30 
31 # Application info
32 $application = Get-WebApplication -Site $site -Name $name
33 $website = Get-Website -Name $site
34 
35 # Set ApplicationPool to current if not specified
36 if (!$application_pool) {
37   $application_pool = $website.applicationPool
38 }
39 
40 try {
41   # Add application
42   if (($state -eq 'present') -and (-not $application)) {
43     if (-not $physical_path) {
44       Fail-Json $result "missing required arguments: path"
45     }
46     if (-not (Test-Path -LiteralPath $physical_path)) {
47       Fail-Json $result "specified folder must already exist: path"
48     }
49 
50     $application_parameters = @{
51       Name = $name
52       PhysicalPath = $physical_path
53       Site = $site
54     }
55 
56     if ($application_pool) {
57       $application_parameters.ApplicationPool = $application_pool
58     }
59 
60     if (-not $check_mode) {
61         $application = New-WebApplication @application_parameters -Force
62     }
63     $result.changed = $true
64   }
65 
66   # Remove application
67   if ($state -eq 'absent' -and $application) {
68     $application = Remove-WebApplication -Site $site -Name $name -WhatIf:$check_mode
69     $result.changed = $true
70   }
71 
72   $application = Get-WebApplication -Site $site -Name $name
73   if ($application) {
74 
75     # Change Physical Path if needed
76     if ($physical_path) {
77       if (-not (Test-Path -LiteralPath $physical_path)) {
78         Fail-Json $result "specified folder must already exist: path"
79       }
80 
81       $app_folder = Get-Item -LiteralPath $application.PhysicalPath
82       $folder = Get-Item -LiteralPath $physical_path
83       if ($folder.FullName -ne $app_folder.FullName) {
84         Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -name physicalPath -value $physical_path -WhatIf:$check_mode
85         $result.changed = $true
86       }
87     }
88 
89     # Change Application Pool if needed
90     if ($application_pool) {
91       if ($application_pool -ne $application.applicationPool) {
92         Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -name applicationPool -value $application_pool -WhatIf:$check_mode
93         $result.changed = $true
94       }
95     }
96 
97     # Change username and password if needed
98     $app_user = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName'
99     $app_pass = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password'
100     if ($connect_as -eq 'pass_through') {
101       if ($app_user -ne '') {
102         Clear-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' -WhatIf:$check_mode
103         $result.changed = $true
104       }
105       if ($app_pass -ne '') {
106         Clear-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' -WhatIf:$check_mode
107         $result.changed = $true
108       }
109     } elseif ($connect_as -eq 'specific_user') {
110       if ($app_user -ne $username) {
111         Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' -Value $username -WhatIf:$check_mode
112         $result.changed = $true
113       }
114       if ($app_pass -ne $password) {
115         Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' -Value $password -WhatIf:$check_mode
116         $result.changed = $true
117       }
118     }
119   }
120 } catch {
121   Fail-Json $result $_.Exception.Message
122 }
123 
124 # When in check-mode or on removal, this may fail
125 $application = Get-WebApplication -Site $site -Name $name
126 if ($application) {
127   $app_user = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName'
128   if ($app_user -eq '') {
129     $result.connect_as = 'pass_through'
130   } else {
131     $result.connect_as = 'specific_user'
132   }
133 
134   $result.physical_path = $application.PhysicalPath
135   $result.application_pool = $application.ApplicationPool
136 }
137 
138 Exit-Json $result
139