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 17 $result = @{ 18 application_pool = $application_pool 19 changed = $false 20 physical_path = $physical_path 21 } 22 23 # Ensure WebAdministration module is loaded 24 if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) { 25 Import-Module WebAdministration 26 } 27 28 # Application info 29 $application = Get-WebApplication -Site $site -Name $name 30 31 try { 32 # Add application 33 if (($state -eq 'present') -and (-not $application)) { 34 if (-not $physical_path) { 35 Fail-Json $result "missing required arguments: path" 36 } 37 if (-not (Test-Path -Path $physical_path)) { 38 Fail-Json $result "specified folder must already exist: path" 39 } 40 41 $application_parameters = @{ 42 Name = $name 43 PhysicalPath = $physical_path 44 Site = $site 45 } 46 47 if ($application_pool) { 48 $application_parameters.ApplicationPool = $application_pool 49 } 50 51 if (-not $check_mode) { 52 $application = New-WebApplication @application_parameters -Force 53 } 54 $result.changed = $true 55 } 56 57 # Remove application 58 if ($state -eq 'absent' -and $application) { 59 $application = Remove-WebApplication -Site $site -Name $name -WhatIf:$check_mode 60 $result.changed = $true 61 } 62 63 $application = Get-WebApplication -Site $site -Name $name 64 if ($application) { 65 66 # Change Physical Path if needed 67 if ($physical_path) { 68 if (-not (Test-Path -Path $physical_path)) { 69 Fail-Json $result "specified folder must already exist: path" 70 } 71 72 $app_folder = Get-Item $application.PhysicalPath 73 $folder = Get-Item $physical_path 74 if ($folder.FullName -ne $app_folder.FullName) { 75 Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name physicalPath -value $physical_path -WhatIf:$check_mode 76 $result.changed = $true 77 } 78 } 79 80 # Change Application Pool if needed 81 if ($application_pool) { 82 if ($application_pool -ne $application.applicationPool) { 83 Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name applicationPool -value $application_pool -WhatIf:$check_mode 84 $result.changed = $true 85 } 86 } 87 } 88 } catch { 89 Fail-Json $result $_.Exception.Message 90 } 91 92 # When in check-mode or on removal, this may fail 93 $application = Get-WebApplication -Site $site -Name $name 94 if ($application) { 95 $result.physical_path = $application.PhysicalPath 96 $result.application_pool = $application.ApplicationPool 97 } 98 99 Exit-Json $result 100