Debian Codegraph @

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

raw
1 #!powershell
2
3 # Copyright: (c) 2025, 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" ; required = $false }
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.site = @()
21
22 # Ensure WebAdministration module is loaded
23 if ($null -eq (Get-Module -Name "WebAdministration" -ErrorAction SilentlyContinue)) {
24 Import-Module WebAdministration
25 $web_admin_dll_path = Join-Path $env:SystemRoot system32\inetsrv\Microsoft.Web.Administration.dll
26 Add-Type -LiteralPath $web_admin_dll_path
27 }
28
29 function Get-WebsiteInfo ($name) {
30 # Try to get all the current site details
31 try {
32 $site = Get-Item -LiteralPath IIS:\Sites\$name -ErrorAction Stop
33 }
34 catch {
35 return
36 }
37 if ($null -ne $site) {
38 $module.Result.exists = $true
39 }
40 $site_bindings = Get-WebBinding -Name $name
41 $bindings_list = @(
42 $site_bindings | ForEach-Object {
43 $ssl_flags = $_.sslFlags
44 $use_ccs = [math]::Floor($ssl_flags / 2)
45 $use_sni = $ssl_flags % 2
46 $psObject = [PSCustomObject]@{
47 ip = $($_.bindingInformation -split ":")[0]
48 port = [int]$($_.bindingInformation -split ":")[1]
49 hostname = $($_.bindingInformation -split ":")[2]
50 protocol = $_.protocol
51 use_ccs = [bool]$use_ccs
52 use_sni = [bool]$use_sni
53 certificate_hash = $_.certificateHash
54 certificate_store_name = $_.CertificateStoreName
55 }
56 $psObject
57 }
58 )
59 $WebsiteInfoDict = @{
60 name = $site.Name
61 site_id = $site.ID
62 state = $site.State
63 physical_path = $site.PhysicalPath
64 application_pool = $site.applicationPool
65 bindings = $bindings_list
66 }
67 return $WebsiteInfoDict
68 }
69
70 try {
71 # In case a user specified website name return information only for this website
72 if ($null -ne $name) {
73 $module.Result.site = @(Get-WebsiteInfo -name $name)
74 }
75 # Return information of all the websites available on the system
76 else {
77 $module.Result.site = @(Get-Website | ForEach-Object { Get-WebsiteInfo -name $_.Name })
78 }
79 }
80 catch {
81 $msg = -join @(
82 "Failed to fetch the info of the required Website "
83 "Exception: $($_.Exception.Message)"
84 )
85 $module.FailJson($msg, $_)
86 }
87
88 $module.ExitJson()