【问题标题】:Windows Server 2012R2/2016: HOWTO Check for the Active Directory PowerShell Module using Get-WindowsFeatureWindows Server 2012R2/2016:如何使用 Get-WindowsFeature 检查 Active Directory PowerShell 模块
【发布时间】:2021-10-31 01:53:48
【问题描述】:
Windows Server 2012R2/2016:如何使用 Get-WindowsFeature 检查 Active Directory PowerShell 模块
我需要一种方法来确保我运行 AD 脚本的 Windows 2012R2/2016 服务器已导入 Active-Directory,如果没有则安装它
#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
}
块引用
这行得通吗?我的测试在“(Import-Module ActiveDirectory -Proxy proxy.verizon.com:80 -ErrorAction Continue -Verbose)”处没有表明“真实”响应
【问题讨论】:
标签:
active-directory
windows-server-2012-r2
windows-server-2016
powershell-5.1
【解决方案1】:
Windows Server 2012R2/2016:如何使用 Get-WindowsFeature 检查 Active Directory PowerShell 模块
#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"'