Skip to content

Commit 895719a

Browse files
author
Bob Pokorny
committed
Updated the Get-KeyfactorIISBoundCertificates.ps1 script to include support for JEA (Just Enough Administration) environments. Added a new documentation file for setting up an SSH development environment.
1 parent db4b068 commit 895719a

2 files changed

Lines changed: 356 additions & 34 deletions

File tree

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,130 @@
1-
function Get-KeyfactorIISBoundCertificates{
1+
function Get-KeyfactorIISBoundCertificates {
2+
23
$certificates = @()
34
$totalBoundCertificates = 0
45

6+
#
7+
# Verify the current process is running with an elevated token.
8+
#
9+
$currentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
10+
$principal = [Security.Principal.WindowsPrincipal]::new($currentIdentity)
11+
12+
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
13+
Write-Information `
14+
-Message "Get-KeyfactorIISBoundCertificates requires an elevated PowerShell session (Run as Administrator) or a JEA endpoint configured with administrative privileges." `
15+
-ErrorAction Stop
16+
}
17+
518
try {
6-
Add-Type -Path "$env:windir\System32\inetsrv\Microsoft.Web.Administration.dll" # -AssemblyName "Microsoft.Web.Administration"
7-
$serverManager = New-Object Microsoft.Web.Administration.ServerManager
8-
} catch {
9-
Write-Error "Failed to create ServerManager. IIS might not be installed."
10-
return
19+
Add-Type -Path "$env:windir\System32\inetsrv\Microsoft.Web.Administration.dll" -ErrorAction Stop
20+
$serverManager = [Microsoft.Web.Administration.ServerManager]::new()
21+
}
22+
catch {
23+
Write-Information `
24+
-Message "Failed to create the IIS ServerManager object. IIS may not be installed or the Microsoft.Web.Administration assembly could not be loaded.`n$($_.Exception.Message)" `
25+
-ErrorAction Stop
26+
}
27+
28+
#
29+
# Verify IIS sites can actually be enumerated.
30+
#
31+
try {
32+
$websites = $serverManager.Sites
33+
34+
if ($null -eq $websites) {
35+
Write-Information `
36+
-Message "Unable to enumerate IIS websites." `
37+
-ErrorAction Stop
38+
}
39+
40+
if ($websites.Count -eq 0) {
41+
42+
$siteCount = (Get-Service W3SVC -ErrorAction SilentlyContinue)
43+
44+
if ($siteCount) {
45+
Write-Information @"
46+
No IIS websites were returned.
47+
48+
Possible causes include:
49+
• The current user does not have permission to enumerate IIS configuration.
50+
• IIS contains no configured websites.
51+
• The IIS configuration is unavailable.
52+
"@
53+
}
54+
55+
return
56+
}
57+
}
58+
catch {
59+
Write-Error `
60+
-Message "Access denied while reading IIS configuration. Local Administrator privileges or an appropriately configured JEA endpoint are required.`n$($_.Exception.Message)" `
61+
-ErrorAction Stop
1162
}
1263

13-
$websites = $serverManager.Sites
1464
Write-Information "There were $($websites.Count) websites found."
1565

1666
foreach ($site in $websites) {
67+
1768
$siteName = $site.Name
1869
$siteBoundCertificateCount = 0
1970

2071
foreach ($binding in $site.Bindings) {
72+
2173
if ($binding.Protocol -eq 'https' -and $binding.CertificateHash) {
74+
2275
$certHash = ($binding.CertificateHash | ForEach-Object { $_.ToString("X2") }) -join ""
2376
$storeName = if ($binding.CertificateStoreName) { $binding.CertificateStoreName } else { "My" }
2477

2578
try {
26-
$cert = Get-ChildItem -Path "Cert:\LocalMachine\$storeName" | Where-Object {
27-
$_.Thumbprint -eq $certHash
28-
}
79+
80+
$cert = Get-ChildItem "Cert:\LocalMachine\$storeName" |
81+
Where-Object Thumbprint -eq $certHash
2982

3083
if (-not $cert) {
31-
Write-Warning "Certificate with thumbprint not found in Cert:\LocalMachine\$storeName"
84+
Write-Warning "Certificate with thumbprint '$certHash' was not found in Cert:\LocalMachine\$storeName."
3285
continue
3386
}
3487

3588
$certBase64 = [Convert]::ToBase64String($cert.RawData)
3689
$ip, $port, $hostname = $binding.BindingInformation -split ":", 3
3790

38-
$certInfo = [PSCustomObject]@{
39-
SiteName = $siteName
40-
Binding = $binding.BindingInformation
41-
IPAddress = $ip
42-
Port = $port
43-
Hostname = $hostname
44-
Protocol = $binding.Protocol
45-
SNI = $binding.SslFlags
46-
ProviderName = Get-CertificateCSP $cert
47-
SAN = Get-CertificateSAN $cert
48-
Certificate = $cert.Subject
49-
ExpiryDate = $cert.NotAfter
50-
Issuer = $cert.Issuer
51-
Thumbprint = $cert.Thumbprint
52-
HasPrivateKey = $cert.HasPrivateKey
53-
CertificateBase64 = $certBase64
91+
$certificates += [PSCustomObject]@{
92+
SiteName = $siteName
93+
Binding = $binding.BindingInformation
94+
IPAddress = $ip
95+
Port = $port
96+
Hostname = $hostname
97+
Protocol = $binding.Protocol
98+
SNI = $binding.SslFlags
99+
ProviderName = Get-CertificateCSP $cert
100+
SAN = Get-CertificateSAN $cert
101+
Certificate = $cert.Subject
102+
ExpiryDate = $cert.NotAfter
103+
Issuer = $cert.Issuer
104+
Thumbprint = $cert.Thumbprint
105+
HasPrivateKey = $cert.HasPrivateKey
106+
CertificateBase64 = $certBase64
54107
}
55108

56-
$certificates += $certInfo
57109
$siteBoundCertificateCount++
58110
$totalBoundCertificates++
59-
} catch {
60-
Write-Warning "Could not retrieve certificate details for hash $certHash in store $storeName."
111+
}
112+
catch {
113+
Write-Warning "Could not retrieve certificate details for thumbprint '$certHash' in store '$storeName'."
61114
Write-Warning $_
62115
}
63116
}
64117
}
65118

66-
Write-Information "Website: $siteName has $siteBoundCertificateCount bindings with certificates."
119+
Write-Information "Website '$siteName' has $siteBoundCertificateCount HTTPS binding(s) with certificates."
67120
}
68121

69-
Write-Information "A total of $totalBoundCertificates bindings with valid certificates were found."
122+
Write-Information "A total of $totalBoundCertificates HTTPS binding(s) with valid certificates were found."
70123

71124
if ($totalBoundCertificates -gt 0) {
72125
$certificates | ConvertTo-Json
73-
} else {
74-
Write-Information "No valid certificates were found bound to websites."
126+
}
127+
else {
128+
Write-Information "No HTTPS bindings with valid certificates were found."
75129
}
76130
}

0 commit comments

Comments
 (0)