Hi guys
I’ve been working on a little Powershell script that would help me install/enable the Active Directory module on Windows clients.
There is an easier method as to install this module on a Windows Server, so I haven’t included that in my script.
This is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# # Script that checks if the AD module is available and tries to import this module # If not available it checks if the required RSAT Windows feature is installed and tries to enable it # Clear-Host; Write-Host 'Checking if the Active Directory module is loaded' -ForegroundColor Cyan If (!(Get-Module ActiveDirectory)) { Clear-Host; Write-Host 'Checking if the Active Directory module is available' -ForegroundColor Cyan If (!(Get-Module ActiveDirectory -ListAvailable)) { Clear-Host; Write-Host "Checking if the required Remote Server Administration Toolkit (RSAT) package is installed" -ForegroundColor Cyan If (!(Get-HotFix -Id 'KB941314','KB958830','KB2693643' -ErrorAction SilentlyContinue)) { # KB941314 => Windows Vista # KB958830 => Windows 7 # KB2693643 => Windows 8, Windows 8.1, Windows 10 Clear-Host; Write-Host "The required Remote Server Administration Toolkit (RSAT) package isn't installed" -ForegroundColor Red; Start-Sleep -Seconds 3 $WinVer = [System.Environment]::OSVersion.Version Clear-Host; # Windows Vista If ($WinVer.Major -eq 6 -and $WinVer.Minor -eq 0) { Write-Host 'Installing RSAT package for Windows Vista' -ForegroundColor Cyan If ([System.Environment]::Is64BitOperatingSystem) { # 64-bit # http://www.microsoft.com/downloads/en/details.aspx?familyid=d647a60b-63fd-4ac5-9243-bd3c497d2bc5 } Else { # 32-bit # http://www.microsoft.com/downloads/en/details.aspx?familyid=9ff6e897-23ce-4a36-b7fc-d52065de9960&displaylang=en } } # Windows 7 If ($WinVer.Major -eq 6 -and $WinVer.Minor -eq 1) { Write-Host 'Installing RSAT package for Windows 7' -ForegroundColor Cyan # http://www.microsoft.com/downloads/en/details.aspx?familyid=7d2f6ad7-656b-4313-a005-4e344e43997d } # Windows 8/Windows 8.1/Windows 10 If (($WinVer.Major -eq 6 -and $WinVer.Minor -ge 2) -or ($WinVer.Major -eq 10 -and $WinVer.Minor -eq 0)) { Write-Host 'Installing RSAT package for Windows 8/Windows 8.1/Windows 10' -ForegroundColor Cyan # Win8 http://www.microsoft.com/download/en/details.aspx?id=28972 # Win8.1 http://www.microsoft.com/download/details.aspx?id=39296 # Win10 https://www.microsoft.com/en-us/download/details.aspx?id=45520 } } Else { # START SCRIPTBLOCK $InstallModule = { Write-Host 'Retrieving all Windows features' -ForegroundColor Cyan $AllWindowsFeatures = DISM /Online /Get-Features If ($?) { Clear-Host; Write-Host 'Processing all Windows features' -ForegroundColor Cyan Start-Sleep -Seconds 1 $AllWindowsFeatures | %{ $Feature = ($_ | Select-String -Pattern 'AD-Powershell') -replace '.+\s(.+)','$1' If ($Feature -ne $null -and $Feature -ne '') { Clear-Host; Write-Host "Enabling $Feature" -ForegroundColor Cyan DISM /Online /Enable-Feature /FeatureName:$Feature /Quiet Clear-Host If ($?) { Write-Host "Enabling $Feature succeeded" -ForegroundColor Green } Else { Write-Host "Enabling $Feature failed" -ForegroundColor Red } } } } Else { Write-Host "Error retrieving all windows features. Was the script executed with elevated rights?" -ForegroundColor Red } Start-Sleep -Seconds 3 } # END SCRIPTBLOCK Clear-Host; Write-Host 'Trying to enable the required Windows feature' -ForegroundColor Cyan $EnableWindowsFeature = Start-Process Powershell -ArgumentList "-NoLogo -NoProfile $InstallModule" -verb RunAs -Wait } } Else { Clear-Host; Write-Host 'Importing the Active Directory module' -ForegroundColor Cyan If (!(Import-Module ActiveDirectory)) { # ISSUES } Else { Clear-Host; Write-Host 'The Active Directory module is loaded' -ForegroundColor Green } } } Else { Clear-Host; Write-Host 'The Active Directory module is loaded' -ForegroundColor Green } |
I hope someone can use it.
Greetz
Colombeen
2 Comments
WileCau · July 31, 2017 at 6:08 am
Thanks, this helped me get the AD module installed and working.
Is there a reason you commented out the URLs for installing the RSAT packages instead of auto-browsing to the appropriate one?
E.g. to browse to the Win10 RSAT download URL:
Start-Process https://www.microsoft.com/en-us/download/details.aspx?id=45520
colombeen · August 8, 2017 at 9:12 am
I am still looking for the best solution. I don’t want to leave the powershell window, so I’m trying to download and install by only using powershell.