1+ function Write-MSADPTLog {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory )]
5+ [string ]$Message ,
6+
7+ [Parameter ()]
8+ [ValidateSet (' INFO' , ' WARNING' , ' ERROR' , ' PROMPT' , ' TREASUREFOUND' , ' PASS' , ' FAIL' , ' REVIEW' )]
9+ [string ]$Level = ' INFO' ,
10+
11+ [Parameter ()]
12+ [string ]$LogFilePath
13+ )
14+
15+ $timestamp = Get-Date - Format " yyyy-MM-dd HH:mm:ss"
16+ $entry = " [$timestamp ][$Level ] $Message "
17+
18+ $foregroundColor = switch ($Level ) {
19+ ' INFO' { ' Gray' }
20+ ' WARNING' { ' Yellow' }
21+ ' ERROR' { ' Red' }
22+ ' PROMPT' { ' Cyan' }
23+ ' TREASUREFOUND' { ' Green' }
24+ ' PASS' { ' Green' }
25+ ' FAIL' { ' Red' }
26+ ' REVIEW' { ' Magenta' }
27+ default { ' White' }
28+ }
29+
30+ Write-Host $entry - ForegroundColor $foregroundColor
31+
32+ if (-not [string ]::IsNullOrWhiteSpace($LogFilePath )) {
33+ try {
34+ Add-Content - Path $LogFilePath - Value $entry - ErrorAction Stop
35+ }
36+ catch {
37+ Write-Host " [$timestamp ][WARNING] Failed to write to log file '$LogFilePath ': $ ( $_.Exception.Message ) " - ForegroundColor Yellow
38+ }
39+ }
40+ }
41+
42+
43+ function Prompt-User {
44+ [CmdletBinding ()]
45+ param (
46+ [Parameter (Mandatory )]
47+ [string ]$PromptText
48+ )
49+
50+ Write-MSADPTLog - Message " $PromptText [Y/N]:" - Level ' PROMPT'
51+ $response = Read-Host
52+
53+ return ($response -eq ' Y' -or $response -eq ' y' )
54+ }
55+
56+ function Test-MSADPTADConnectivity {
57+ [CmdletBinding ()]
58+ param (
59+ [Parameter (Mandatory )]
60+ [PSCredential ]$Credential ,
61+
62+ [Parameter (Mandatory )]
63+ [string ]$AdServer
64+ )
65+
66+ Write-MSADPTLog - Message " Pre-flight: testing Active Directory connectivity to '$AdServer '." - Level ' INFO'
67+
68+ try {
69+ $rootDSE = Get-ADRootDSE - Server $AdServer - Credential $Credential - ErrorAction Stop
70+
71+ Write-MSADPTLog - Message " Pre-flight successful. Connected to '$ ( $rootDSE.dnsHostName ) '. DefaultNamingContext='$ ( $rootDSE.defaultNamingContext ) '." - Level ' INFO'
72+ return $rootDSE
73+ }
74+ catch {
75+ Write-MSADPTLog - Message " Pre-flight failed for '$AdServer ': $ ( $_.Exception.Message ) " - Level ' ERROR'
76+ return $null
77+ }
78+ }
79+
80+ function New-MSADPTAdCommandSplat {
81+ [CmdletBinding ()]
82+ param (
83+ [Parameter ()]
84+ [string ]$Server ,
85+
86+ [Parameter ()]
87+ [PSCredential ]$Credential
88+ )
89+
90+ $splat = @ {}
91+
92+ if (-not [string ]::IsNullOrWhiteSpace($Server )) {
93+ $splat.Server = $Server
94+ }
95+
96+ if ($null -ne $Credential ) {
97+ $splat.Credential = $Credential
98+ }
99+
100+ return $splat
101+ }
102+
103+ function Get-MSADPTRemoteRegistryValue {
104+ [CmdletBinding ()]
105+ param (
106+ [Parameter (Mandatory )]
107+ [string ]$ComputerName ,
108+
109+ [Parameter (Mandatory )]
110+ [string ]$SubKey ,
111+
112+ [Parameter (Mandatory )]
113+ [string ]$ValueName
114+ )
115+
116+ try {
117+ $base = [Microsoft.Win32.RegistryKey ]::OpenRemoteBaseKey(
118+ [Microsoft.Win32.RegistryHive ]::LocalMachine,
119+ $ComputerName
120+ )
121+
122+ if (-not $base ) { return $null }
123+
124+ $key = $base.OpenSubKey ($SubKey )
125+ if (-not $key ) { return $null }
126+
127+ return $key.GetValue ($ValueName , $null )
128+ }
129+ catch {
130+ return $null
131+ }
132+ }
133+
134+ function Test-MSADPTWebEndpoint {
135+ [CmdletBinding ()]
136+ param (
137+ [Parameter (Mandatory )]
138+ [string ]$ComputerName ,
139+
140+ [Parameter (Mandatory )]
141+ [string ]$RelativePath
142+ )
143+
144+ $httpOpen = $false
145+ $httpsOpen = $false
146+
147+ foreach ($scheme in @ (' http' , ' https' )) {
148+ $uri = " ${scheme} ://$ComputerName /$RelativePath "
149+
150+ try {
151+ $null = Invoke-WebRequest - Uri $uri - Method Head - MaximumRedirection 0 - TimeoutSec 5 - ErrorAction Stop
152+ if ($scheme -eq ' http' ) { $httpOpen = $true }
153+ if ($scheme -eq ' https' ) { $httpsOpen = $true }
154+ }
155+ catch {
156+ if ($_.Exception.Response ) {
157+ try {
158+ $status = [int ]$_.Exception.Response.StatusCode
159+ if ($status -in 200 , 301 , 302 , 401 , 403 ) {
160+ if ($scheme -eq ' http' ) { $httpOpen = $true }
161+ if ($scheme -eq ' https' ) { $httpsOpen = $true }
162+ }
163+ }
164+ catch {
165+ # ignore nested parse issues
166+ }
167+ }
168+ }
169+ }
170+
171+ return [PSCustomObject ]@ {
172+ HttpOpen = $httpOpen
173+ HttpsOpen = $httpsOpen
174+ }
175+ }
176+
177+ # Export-ModuleMember -Function Write-MSADPTLog, Prompt-User, Test-MSADPTADConnectivity
178+ Export-ModuleMember - Function Write-MSADPTLog , Prompt- User, Test-MSADPTADConnectivity , New-MSADPTAdCommandSplat , Get-MSADPTRemoteRegistryValue , Test-MSADPTWebEndpoint
0 commit comments