本文目录导读:

我来为您提供一个Windows下加入域的PowerShell脚本:
基础版加入域脚本
# Join-Domain.ps1
# 用法: .\Join-Domain.ps1 -DomainName "contoso.com" -Username "admin" -Password "P@ssw0rd"
param(
[Parameter(Mandatory=$true)]
[string]$DomainName,
[Parameter(Mandatory=$true)]
[string]$Username,
[Parameter(Mandatory=$true)]
[string]$Password,
[string]$OUPath = "", # 可选:OU路径
[switch]$Restart = $false # 是否自动重启
)
# 创建域凭证
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential("$DomainName\$Username", $SecurePassword)
try {
Write-Host "正在尝试加入域: $DomainName" -ForegroundColor Yellow
if ($OUPath) {
# 指定OU加入域
Add-Computer -DomainName $DomainName -Credential $Credential -OUPath $OUPath -Force
} else {
# 默认加入域
Add-Computer -DomainName $DomainName -Credential $Credential -Force
}
Write-Host "成功加入域: $DomainName" -ForegroundColor Green
if ($Restart) {
Write-Host "系统将在10秒后重启..." -ForegroundColor Yellow
Restart-Computer -Force
} else {
Write-Host "请手动重启计算机以完成加入域操作" -ForegroundColor Cyan
}
} catch {
Write-Host "加入域失败: $_" -ForegroundColor Red
exit 1
}
增强版加入域脚本(带网络检查)
# Join-Domain-Enhanced.ps1
param(
[Parameter(Mandatory=$true)]
[string]$DomainName,
[Parameter(Mandatory=$true)]
[string]$Username,
[Parameter(Mandatory=$true)]
[string]$Password,
[string]$OUPath = "",
[string]$NewComputerName = "", # 可选:新计算机名
[switch]$Restart = $false
)
function Test-DomainController {
param([string]$Domain)
try {
$dc = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain(
[System.DirectoryServices.ActiveDirectory.DirectoryContext]::new("Domain", $Domain)
)
return $true
} catch {
return $false
}
}
# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "请以管理员身份运行此脚本!" -ForegroundColor Red
exit 1
}
# 网络连接检查
Write-Host "正在检查网络连接..." -ForegroundColor Yellow
if (-not (Test-Connection -ComputerName $DomainName -Count 1 -Quiet)) {
Write-Host "无法ping通域控制器,请检查网络连接" -ForegroundColor Red
exit 1
}
# DNS检查
try {
$dnsResult = Resolve-DnsName -Name $DomainName -Type A -ErrorAction Stop
Write-Host "DNS解析成功: $($dnsResult.IPAddress)" -ForegroundColor Green
} catch {
Write-Host "DNS解析失败,请检查DNS设置" -ForegroundColor Red
exit 1
}
# 设置新计算机名(如果需要)
if ($NewComputerName) {
try {
Rename-Computer -NewName $NewComputerName -Force
Write-Host "计算机名已更改为: $NewComputerName" -ForegroundColor Green
} catch {
Write-Host "更改计算机名失败: $_" -ForegroundColor Red
}
}
# 创建域凭证
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential("$DomainName\$Username", $SecurePassword)
# 加入域
try {
Write-Host "正在加入域: $DomainName" -ForegroundColor Yellow
if ($OUPath) {
Add-Computer -DomainName $DomainName -Credential $Credential -OUPath $OUPath -Force -ErrorAction Stop
} else {
Add-Computer -DomainName $DomainName -Credential $Credential -Force -ErrorAction Stop
}
Write-Host "=" * 50 -ForegroundColor Green
Write-Host "成功加入域: $DomainName" -ForegroundColor Green
Write-Host "计算机名: $((Get-CimInstance Win32_ComputerSystem).Name)" -ForegroundColor Green
Write-Host "=" * 50 -ForegroundColor Green
if ($Restart) {
Write-Host "系统将在5秒后重启..." -ForegroundColor Yellow
Start-Sleep -Seconds 5
Restart-Computer -Force
} else {
Write-Host "请尽快重启计算机以完成加入域操作" -ForegroundColor Cyan
}
} catch [System.UnauthorizedAccessException] {
Write-Host "访问被拒绝,请检查用户名和密码是否正确" -ForegroundColor Red
exit 1
} catch {
Write-Host "加入域失败: $_" -ForegroundColor Red
Write-Host "错误详情:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
}
批量加入域脚本(适用于多台计算机)
# Bulk-Join-Domain.ps1
param(
[Parameter(Mandatory=$true)]
[string]$DomainName,
[Parameter(Mandatory=$true)]
[string]$AdminUsername,
[Parameter(Mandatory=$true)]
[string]$AdminPassword,
[Parameter(Mandatory=$true)]
[string]$ComputerListFile # 计算机列表文件,每行一个计算机名
)
$Computers = Get-Content $ComputerListFile
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential("$DomainName\$AdminUsername", $SecurePassword)
foreach ($Computer in $Computers) {
Write-Host "正在处理计算机: $Computer" -ForegroundColor Yellow
try {
Invoke-Command -ComputerName $Computer -ScriptBlock {
param($Domain, $Cred)
Add-Computer -DomainName $Domain -Credential $Cred -Force
} -ArgumentList $DomainName, $Credential -ErrorAction Stop
Write-Host "计算机 $Computer 成功加入域" -ForegroundColor Green
} catch {
Write-Host "计算机 $Computer 加入域失败: $_" -ForegroundColor Red
# 继续处理下一台
}
}
使用说明
-
基本用法:
.\Join-Domain.ps1 -DomainName "contoso.com" -Username "admin" -Password "P@ssw0rd" -Restart
-
指定OU加入:
.\Join-Domain.ps1 -DomainName "contoso.com" -Username "admin" -Password "P@ssw0rd" -OUPath "OU=Computers,OU=Office,DC=contoso,DC=com"
-
需要以管理员权限运行
注意事项
- 必须具有域管理员权限
- 计算机需要能够解析域控制器的DNS
- 确保防火墙没有阻止相关端口(尤其是445、135、88等)
- 建议先测试网络连接和DNS解析
- 脚本可以保存为
.ps1文件执行 - 如果遇到执行策略问题,使用:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
这个脚本可以帮助您快速、自动化的完成计算机加入域的操作。