| 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 |
$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 |
$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 |
# Ensure WebAdministration module is loaded |
| 21 |
if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) { |
| 22 |
Import-Module WebAdministration |
| 23 |
} |
| 24 |
|
| 25 |
# Result |
| 26 |
$result = @{ |
| 27 |
directory = @{} |
| 28 |
changed = $false |
| 29 |
} |
| 30 |
|
| 31 |
# Construct path |
| 32 |
$directory_path = if ($application) { |
| 33 |
"IIS:\Sites\$($site)\$($application)\$($name)" |
| 34 |
} |
| 35 |
else { |
| 36 |
"IIS:\Sites\$($site)\$($name)" |
| 37 |
} |
| 38 |
|
| 39 |
# Directory info |
| 40 |
$directory = if ($application) { |
| 41 |
Get-WebVirtualDirectory -Site $site -Name $name -Application $application |
| 42 |
} |
| 43 |
else { |
| 44 |
Get-WebVirtualDirectory -Site $site -Name $name |
| 45 |
} |
| 46 |
|
| 47 |
try { |
| 48 |
# Add directory |
| 49 |
If (($state -eq 'present') -and (-not $directory)) { |
| 50 |
If (-not $physical_path) { |
| 51 |
Fail-Json -obj $result -message "missing required arguments: physical_path" |
| 52 |
} |
| 53 |
If (-not (Test-Path -LiteralPath $physical_path)) { |
| 54 |
Fail-Json -obj $result -message "specified folder must already exist: physical_path" |
| 55 |
} |
| 56 |
|
| 57 |
$directory_parameters = @{ |
| 58 |
Site = $site |
| 59 |
Name = $name |
| 60 |
PhysicalPath = $physical_path |
| 61 |
} |
| 62 |
|
| 63 |
If ($application) { |
| 64 |
$directory_parameters.Application = $application |
| 65 |
} |
| 66 |
|
| 67 |
$directory = New-WebVirtualDirectory @directory_parameters -Force |
| 68 |
$result.changed = $true |
| 69 |
} |
| 70 |
|
| 71 |
# Remove directory |
| 72 |
If ($state -eq 'absent' -and $directory) { |
| 73 |
Remove-Item -LiteralPath $directory_path -Recurse -Force |
| 74 |
$result.changed = $true |
| 75 |
} |
| 76 |
|
| 77 |
$directory = if ($application) { |
| 78 |
Get-WebVirtualDirectory -Site $site -Name $name -Application $application |
| 79 |
} |
| 80 |
else { |
| 81 |
Get-WebVirtualDirectory -Site $site -Name $name |
| 82 |
} |
| 83 |
|
| 84 |
If ($directory) { |
| 85 |
|
| 86 |
# Change Physical Path if needed |
| 87 |
if ($physical_path) { |
| 88 |
If (-not (Test-Path -LiteralPath $physical_path)) { |
| 89 |
Fail-Json -obj $result -message "specified folder must already exist: physical_path" |
| 90 |
} |
| 91 |
|
| 92 |
$vdir_folder = Get-Item -LiteralPath $directory.PhysicalPath |
| 93 |
$folder = Get-Item -LiteralPath $physical_path |
| 94 |
If ($folder.FullName -ne $vdir_folder.FullName) { |
| 95 |
Set-ItemProperty -LiteralPath $directory_path -name physicalPath -value $physical_path |
| 96 |
$result.changed = $true |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
# Change username or password if needed |
| 101 |
if ($connect_as -eq 'pass_through') { |
| 102 |
if ($directory.username -ne '') { |
| 103 |
Clear-ItemProperty -LiteralPath $directory_path -Name 'userName' |
| 104 |
$result.changed = $true |
| 105 |
} |
| 106 |
if ($directory.password -ne '') { |
| 107 |
Clear-ItemProperty -LiteralPath $directory_path -Name 'password' |
| 108 |
$result.changed = $true |
| 109 |
} |
| 110 |
} |
| 111 |
elseif ($connect_as -eq 'specific_user') { |
| 112 |
if ($directory.username -ne $username) { |
| 113 |
Set-ItemProperty -LiteralPath $directory_path -Name 'userName' -Value $username |
| 114 |
$result.changed = $true |
| 115 |
} |
| 116 |
if ($directory.password -ne $password) { |
| 117 |
Set-ItemProperty -LiteralPath $directory_path -Name 'password' -Value $password |
| 118 |
$result.changed = $true |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
catch { |
| 124 |
Fail-Json $result $_.Exception.Message |
| 125 |
} |
| 126 |
|
| 127 |
# Result |
| 128 |
$directory = if ($application) { |
| 129 |
Get-WebVirtualDirectory -Site $site -Name $name -Application $application |
| 130 |
} |
| 131 |
else { |
| 132 |
Get-WebVirtualDirectory -Site $site -Name $name |
| 133 |
} |
| 134 |
|
| 135 |
$result.directory = @{ |
| 136 |
PhysicalPath = $directory.PhysicalPath |
| 137 |
} |
| 138 |
|
| 139 |
Exit-Json -obj $result |