1) 以管理员身份打开 Windows PowerShell ISE,执行“Set-ExecutionPolicy remotesigned“命令来将powershell策略设置为可远程执行。

2)在本地Active Directory环境中的Windows PowerShell ISE中输入下定义好的Powershell的脚本来初始化Windows网络配置,其中各项参数配置如表1-1所示

#Declare variables
$ipaddress = "172.16.67.171" 
$ipprefix = "20" 
$ipgw = "172.16.79.253" 
$ipdns = "100.100.2.136"
$ipdns2 = "100.100.2.138" 
$ipif = (Get-NetAdapter).ifIndex 
$featureLogPath = "c:\poshlog\featurelog.txt" 
$newname = "Dang"
$addsTools = "RSAT-AD-Tools" 

表1-1 网络配置参数

变量名

用途

$ipaddress

172.16.67.171

要分配给网络接口的IP地址

$ipprefix

20

子网掩码的前缀长度(CIDR表示法)

$ipgw

172.16.79.253

默认网关的IP地址

$ipdns

100.100.2.136

网络接口的首选DNS服务器地址

$ipdns2

100.100.2.138

网络接口的备用DNS服务器地址

$ipif

(Get-NetAdapter).ifIndex

网络适配器的接口索引

$featureLogPath

c:\poshlog\featurelog.txt

用于记录安装的功能的日志文件路径

$newname

Dang

计算机新名称

$addsTools

RSAT-AD-Tools

要安装的Windows功能名称(Active Directory域服务工具)

3)在Windows PowerShell ISE中输入下定义好的Powershell的脚本来初始化Windows Server AD 必备组件

#Set a static IP address
New-NetIPAddress -IPAddress $ipaddress -PrefixLength $ipprefix -InterfaceIndex $ipif -DefaultGateway $ipgw 

# Set the DNS servers
Set-DnsClientServerAddress -InterfaceIndex $ipif -ServerAddresses ($ipdns, $ipdns2)

#Rename the computer 
Rename-Computer -NewName $newname -force 

#Install features 
New-Item $featureLogPath -ItemType file -Force 
Add-WindowsFeature $addsTools 
Get-WindowsFeature | Where installed >>$featureLogPath 

#Restart the computer 
Restart-Computer

4)随后使用如下脚本通过异步作业来安装所需的服务器角色和功能,然后创建一个新的Active Directory森林。

Declare variables
$DatabasePath = "c:\windows\NTDS"
$DomainMode = "WinThreshold"
$DomainName = "dang.gxsec.net.cn"
$DomainNetBIOSName = "dangad"
$ForestMode = "WinThreshold"
$LogPath = "c:\windows\NTDS"
$SysVolPath = "c:\windows\SYSVOL"
$featureLogPath = "c:\poshlog\featurelog.txt" 
$Password = "DangJason123"
$SecureString = ConvertTo-SecureString $Password -AsPlainText -Force

#Install Active Directory Domain Services, DNS, and Group Policy Management Console 
start-job -Name addFeature -ScriptBlock { 
Add-WindowsFeature -Name "ad-domain-services" -IncludeAllSubFeature -IncludeManagementTools 
Add-WindowsFeature -Name "dns" -IncludeAllSubFeature -IncludeManagementTools 
Add-WindowsFeature -Name "gpmc" -IncludeAllSubFeature -IncludeManagementTools } 
Wait-Job -Name addFeature 
Get-WindowsFeature | Where installed >>$featureLogPath

#Create a new Windows Server AD forest
Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath $DatabasePath -DomainMode $DomainMode -DomainName $DomainName -SafeModeAdministratorPassword $SecureString -DomainNetbiosName $DomainNetBIOSName -ForestMode $ForestMode -InstallDns:$true -LogPath $LogPath -NoRebootOnCompletion:$false -SysvolPath $SysVolPath -Force:$true

PowerShell脚本中使用的变量及其描述,如下表所示:

表1-2 配置参数详解

变量名

描述

$DatabasePath

"c:\windows\NTDS"

Active Directory数据库文件存储的路径

$DomainMode

"WinThreshold"

域的功能级别

$DomainName

"adtest.gxsec.net.cn"

新创建的域的名称

$DomainNetBIOSName

"adtest"

域的NetBIOS名称

$ForestMode

"WinThreshold"

森林的功能级别

$LogPath

"c:\windows\NTDS"

Active Directory日志文件存储的路径

$SysVolPath

"c:\windows\SYSVOL"

系统卷(SYSVOL)文件夹的路径

$featureLogPath

"c:\poshlog\featurelog.txt"

安装功能日志的文件路径

$Password

"DangJason123"

安全模式管理员账户的密码

$SecureString

ConvertTo-SecureString

安全字符串形式的密码,用于安装过程中

5)安装完毕后,服务器会自动进行重启,当重启完毕后,打开“服务器管理器—本地服务器—Active Directory用户和计算机”,如下图所示,证明我们已经成功安装好了Active Directory。