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 $site = Get-AnsibleParam -obj $params -name "site" -type "str" -failifempty $true 13 $application = Get-AnsibleParam -obj $params -name "application" -type "str" 14 $physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" 15 $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present" 16 17 # Ensure WebAdministration module is loaded 18 if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) { 19 Import-Module WebAdministration 20 } 21 22 # Result 23 $result = @{ 24 directory = @{} 25 changed = $false 26 }; 27 28 # Construct path 29 $directory_path = if($application) { 30 "IIS:\Sites\$($site)\$($application)\$($name)" 31 } else { 32 "IIS:\Sites\$($site)\$($name)" 33 } 34 35 # Directory info 36 $directory = if($application) { 37 Get-WebVirtualDirectory -Site $site -Name $name -Application $application 38 } else { 39 Get-WebVirtualDirectory -Site $site -Name $name 40 } 41 42 try { 43 # Add directory 44 If(($state -eq 'present') -and (-not $directory)) { 45 If (-not $physical_path) { 46 Fail-Json -obj $result -message "missing required arguments: physical_path" 47 } 48 If (-not (Test-Path $physical_path)) { 49 Fail-Json -obj $result -message "specified folder must already exist: physical_path" 50 } 51 52 $directory_parameters = @{ 53 Site = $site 54 Name = $name 55 PhysicalPath = $physical_path 56 } 57 58 If ($application) { 59 $directory_parameters.Application = $application 60 } 61 62 $directory = New-WebVirtualDirectory @directory_parameters -Force 63 $result.changed = $true 64 } 65 66 # Remove directory 67 If ($state -eq 'absent' -and $directory) { 68 Remove-Item $directory_path -Recurse -Force 69 $result.changed = $true 70 } 71 72 $directory = Get-WebVirtualDirectory -Site $site -Name $name 73 If($directory) { 74 75 # Change Physical Path if needed 76 if($physical_path) { 77 If (-not (Test-Path $physical_path)) { 78 Fail-Json -obj $result -message "specified folder must already exist: physical_path" 79 } 80 81 $vdir_folder = Get-Item $directory.PhysicalPath 82 $folder = Get-Item $physical_path 83 If($folder.FullName -ne $vdir_folder.FullName) { 84 Set-ItemProperty $directory_path -name physicalPath -value $physical_path 85 $result.changed = $true 86 } 87 } 88 } 89 } catch { 90 Fail-Json $result $_.Exception.Message 91 } 92 93 # Result 94 $directory = Get-WebVirtualDirectory -Site $site -Name $name 95 $result.directory = @{ 96 PhysicalPath = $directory.PhysicalPath 97 } 98 99 Exit-Json -obj $result 100