Private/Storage/ActiveDirectory/Import-PukActiveDirectoryModule.ps1
|
function Import-PukActiveDirectoryModule { [CmdletBinding()] param() if (Get-Module -Name ActiveDirectory) { return } $attemptErrors = [System.Collections.Generic.List[string]]::new() try { Import-Module ActiveDirectory -ErrorAction Stop return } catch { $attemptErrors.Add("native import: $($_.Exception.Message)") } # -UseWindowsPowerShell and WindowsCompatibility only make sense on PowerShell 7+ (Core); # on Windows PowerShell 5.1 the native import above is the only path, so retrying with # a Core-only parameter would just fail with a confusing "parameter not found" error. if ($PSVersionTable.PSEdition -eq 'Core') { try { Import-Module ActiveDirectory -UseWindowsPowerShell -ErrorAction Stop return } catch { $attemptErrors.Add("-UseWindowsPowerShell: $($_.Exception.Message)") } try { Import-Module WindowsCompatibility -ErrorAction Stop Import-WinModule ActiveDirectory -ErrorAction Stop return } catch { $attemptErrors.Add("WindowsCompatibility: $($_.Exception.Message)") } } throw "The ActiveDirectory module is unavailable. Install RSAT: Active Directory tools, or ensure -UseWindowsPowerShell / the WindowsCompatibility module can reach it. Details: $($attemptErrors -join ' | ')" } |