Debian Codegraph @

ansible_collections/microsoft/iis/plugins/modules/web_application_info.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2024, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6
7 #AnsibleRequires -CSharpUtil Ansible.Basic
8
9 function Get-ConnectAsInfo {
10 param (
11 [string] $site,
12 [string] $appName
13 )
14
15 # Construct the IIS path
16 $appPath = "IIS:\Sites\$($site)\$($appName)"
17
18 # Get the properties of the web application or virtual directory
19 $appProperties = Get-ItemProperty -LiteralPath $appPath
20
21 # Determine the Connect-As mode
22 if ($appProperties.userName -and $appProperties.userName -ne "") {
23 $connect_as = "specific_user"
24 $username = $appProperties.userName
25 }
26 else {
27 $connect_as = "pass_through"
28 $username = ""
29 }
30 return @{
31 connect_as = $connect_as
32 username = $username
33 }
34 }
35 $spec = @{
36 options = @{
37 name = @{ type = "str" }
38 site = @{ type = "str" }
39 }
40 supports_check_mode = $true
41 }
42
43 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
44 $name = $module.Params.name
45 $site = $module.Params.site
46
47 $module.Result.exists = $false
48 $module.Result.applications = @()
49
50 try {
51 # Ensure WebAdministration module is loaded
52 if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) {
53 Import-Module WebAdministration
54 }
55 }
56 catch {
57 $module.FailJson("Failed to load WebAdministration module, Exception: $($_.Exception.Message)", $_)
58 }
59
60 try {
61 $getParams = @{}
62 if ($name) {
63 $getParams.Name = $name
64 }
65 if ($site) {
66 $getParams.Site = $site
67 }
68 $applications = Get-WebApplication @getParams
69 }
70 catch {
71 $module.FailJson("Failed to get web applications, Exception: $($_.Exception.Message)", $_)
72 }
73 if ($null -ne $applications) {
74 $module.Result.exists = $true
75 }
76
77 try {
78 $module.Result.applications = @(
79 foreach ($application in $applications) {
80 # Get site name from the application object
81 $site_name = $application.GetParentElement().Attributes["name"].Value
82 $app_name = $application.Path.TrimStart('/')
83
84 # Fetch Connect-As information once
85 $connectAsInfo = Get-ConnectAsInfo -site $site_name -appName $app_name
86 @{
87 name = $app_name
88 site = $site_name
89 connect_as = $connectAsInfo.connect_as
90 username = $connectAsInfo.username
91 application_pool = $application.ApplicationPool
92 physical_path = $application.PhysicalPath
93 enabled_protocols = $application.EnabledProtocols
94 }
95 }
96 )
97 }
98 catch {
99 $module.FailJson("Failed to get application details, Exception: $($_.Exception.Message)", $_)
100 }
101
102 $module.ExitJson()