0

Windows Server 2012R2/2016: HOWTO Check for the Active Directory PowerShell Module using Get-WindowsFeature

I need a means to ensure the Windows 2012R2/2016 server I run an AD script on has Imported Active-Directory, and install it if not

#Check if AD is still installed
if (Import-Module ActiveDirectory -Proxy proxy.verizon.com:80 -ErrorAction Continue -Verbose)
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Import-Module ActiveDirectory -Proxy proxy.verizon.com:80
}
else {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Get-WindowsFeature -Name RSAT-AD-PowerShell|Install-Windowsfeature -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
Import-Module ActiveDirectory -Proxy proxy.verizon.com:80
}

Blockquote Will this work? My testing does not indicate a "true" response at '(Import-Module ActiveDirectory -Proxy proxy.verizon.com:80 -ErrorAction Continue -Verbose)'

Patrick Burwell
  • 129
  • 1
  • 12

1 Answers1

0

Windows Server 2012R2/2016: HOWTO Check for the Active Directory PowerShell Module using Get-WindowsFeature

#Check if AD is still installed
$installed = Get-WindowsFeature -LogPath $log -InformationAction Continue -Name RSAT-AD-PowerShell
if ($installed.Installed)
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Import-Module ActiveDirectory -Verbose -NoClobber -WarningAction Continue -InformationAction Continue -ErrorAction Continue
}
else {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Get-WindowsFeature $log -Name RSAT-AD-PowerShell|Install-Windowsfeature -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
Import-Module ActiveDirectory -Verbose -NoClobber -WarningAction Continue -InformationAction Continue -ErrorAction Continue
}

#The $log is set in your script header, like this:
"$date = get-date -uformat "%m%d%y-%H"
$day = Get-Date -Format yyyyMMdd
$Service = "ServiceNameHere"
$log = ".\logs\start-$service-$date.log"'
Patrick Burwell
  • 129
  • 1
  • 12