Debian Codegraph @

ansible_collections/microsoft/iis/plugins/modules/web_app_pool_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 #AnsibleRequires -CSharpUtil Ansible.Basic
7
8 $spec = @{
9 options = @{
10 name = @{ type = "str" }
11 }
12 supports_check_mode = $true
13 }
14
15 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
16
17 $name = $module.Params.name
18
19 $module.Result.exists = $false
20 $module.Result.app_pools = @()
21
22
23 # Ensure WebAdministration module is loaded
24 if ($null -eq (Get-Module -Name "WebAdministration" -ErrorAction SilentlyContinue)) {
25 Import-Module WebAdministration
26 $web_admin_dll_path = Join-Path $env:SystemRoot system32\inetsrv\Microsoft.Web.Administration.dll
27 Add-Type -LiteralPath $web_admin_dll_path
28 }
29
30 function Get-AppPoolInfo ($name) {
31
32 # Get all the current attributes for the pool
33 $pool = Get-Item -LiteralPath IIS:\AppPools\$name
34 if ($null -ne $pool) {
35 $module.Result.exists = $true
36 }
37 $appPoolInfoDict = @{
38 name = $name
39 attributes = @{}
40 }
41
42 $elements = @("attributes", "cpu", "failure", "processModel", "recycling")
43 foreach ($element in $elements) {
44 if ($element -eq "attributes") {
45 $attribute_collection = $pool.Attributes
46 $attribute_parent = $pool
47 }
48 else {
49 $attribute_collection = $pool.$element.Attributes
50 $attribute_parent = $pool.$element
51 }
52
53 foreach ($attribute in $attribute_collection) {
54 $attribute_name = $attribute.Name
55 if ($attribute_name -notlike "*password*") {
56 $attribute_value = $attribute_parent.$attribute_name
57 # Convert TimeSpan to string if applicable
58 if ($attribute_value -is [TimeSpan]) {
59 $attribute_value = $attribute_value.ToString("c") # Converts to hh:mm:ss or d.hh:mm:ss format
60 }
61 if ($element -eq "attributes") {
62 $appPoolInfoDict.attributes.Add($attribute_name, $attribute_value)
63 }
64 else {
65 if (-not $appPoolInfoDict.attributes[$element]) {
66 $appPoolInfoDict.attributes[$element] = @{}
67 }
68 $appPoolInfoDict.attributes[$element].Add($attribute_name, $attribute_value)
69 }
70 }
71 }
72 }
73 # Ensure periodicRestart is initialized
74 if (-not $appPoolInfoDict.attributes.recycling.ContainsKey("periodicRestart")) {
75 $appPoolInfoDict.attributes.recycling["periodicRestart"] = @{}
76 }
77 # Manually get the periodicRestart attributes in recycling
78 foreach ($attribute in $pool.recycling.periodicRestart.Attributes) {
79 $attribute_name = $attribute.Name
80 $attribute_value = $pool.recycling.periodicRestart.$attribute_name
81 # Convert TimeSpan to string if applicable
82 if ($attribute_value -is [TimeSpan]) {
83 $attribute_value = $attribute_value.ToString("c") # Converts to hh:mm:ss or d.hh:mm:ss format
84 }
85 $appPoolInfoDict.attributes.recycling.periodicRestart.Add($attribute_name, $attribute_value)
86 }
87 return $appPoolInfoDict
88 }
89 try {
90 # In case a user specified app pool name return information only for this app pool
91 if ($null -ne $name) {
92 [array]$module.Result.app_pools = Get-AppPoolInfo -name $name
93 }
94 # Return information of all the app pools available on the system
95 else {
96 $appPoolList = (Get-ChildItem -LiteralPath IIS:\AppPools).Name
97 [array]$module.Result.app_pools = $appPoolList | ForEach-Object { Get-AppPoolInfo -name $_ }
98 }
99 }
100 catch {
101 $msg = -join @(
102 "Failed to fetch the info of the required application pool "
103 "Exception: $($_.Exception.Message)"
104 )
105 $module.FailJson($msg, $_)
106 }
107
108 $module.ExitJson()