PowerShell脚本优化:按DT/LT前缀拼接资产标签生成计算机名加域
PowerShell域加入脚本优化(支持设备类型前缀+资产标签命名)
核心优化说明
- 设备类型选择映射:台式机自动加
DT前缀、笔记本自动加LT前缀,支持直接回车选默认(台式机) - 计算机名自动拼接:前缀+输入的资产标签生成合规计算机名(如
DT01234) - 输入校验:拦截空资产标签、非法设备/OU选择、含特殊字符的计算机名
- 补全原脚本缺失的指定OU域加入逻辑(原脚本仅实现重命名,未实际执行OU绑定加域)
- 复用并修正原凭证验证函数(修正原拼写错误),提前校验AD凭证有效性
- 增加操作前确认环节,避免误操作
优化后完整脚本
# 需以管理员权限运行 # 请提前替换下方$domainName、$ouMap中的域/OU信息为实际值 $domainName = "yourdomain.com" # 修正原凭证验证函数拼写(原函数名Test-ADCrential拼写错误) function Test-ADCredential{ [CmdletBinding()] param( [pscredential]$Credential ) try { Add-Type -AssemblyName System.DirectoryServices.AccountManagement if(!$Credential) { $Credential = Get-Credential -Message "请输入域账号凭证" -ErrorAction Stop } # 校验凭证格式为DOMAIN\USER if($Credential.username.split("\").count -ne 2) { throw "凭证格式错误,请使用DOMAIN\USER格式,当前输入:$($Credential.Username)" } $DomainShortName = $Credential.username.Split("\")[0] $UserName = $Credential.username.Split("\")[1] $Password = $Credential.GetNetworkCredential().Password $PC = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Domain, $DomainShortName) if($PC.ValidateCredentials($UserName,$Password)) { Write-Verbose "凭证验证成功:$($Credential.Username)" return $True } else { throw "凭证验证失败:$($Credential.Username)" } } catch { Write-Verbose "凭证验证出错:$_" return $False } } # -------------------------- 1. 设备类型选择 -------------------------- Write-Host "`n请选择设备类型:" -ForegroundColor Cyan Write-Host "1. 台式机(前缀DT)[默认]" Write-Host "2. 笔记本电脑(前缀LT)" $typeChoice = Read-Host "请输入选项(1/2,直接回车选默认)" # 校验设备类型输入 while ($typeChoice -notin @("1","2","")) { Write-Host "无效输入,请输入1或2" -ForegroundColor Red $typeChoice = Read-Host "请重新输入(1/2,直接回车选默认)" } $devicePrefix = if ($typeChoice -eq "2") {"LT"} else {"DT"} # -------------------------- 2. 资产标签输入 -------------------------- $assetTag = Read-Host "`n请输入资产标签编号(如01234)" # 校验资产标签非空 while ([string]::IsNullOrWhiteSpace($assetTag)) { Write-Host "资产标签不能为空,请输入有效编号" -ForegroundColor Red $assetTag = Read-Host "请重新输入资产标签编号" } # 拼接生成正式计算机名 $newCompName = $devicePrefix + $assetTag # 校验计算机名合规(无Windows禁止的特殊字符) if ($newCompName -match '[\\/:*?"<>|]' -or $newCompName.Length -gt 15) { Write-Host "生成的计算机名[$newCompName]不合规:" -ForegroundColor Red Write-Host "- 禁止包含特殊字符:\ / : * ? " < > |" -ForegroundColor Red Write-Host "- 长度不能超过15位(NETBIOS限制)" -ForegroundColor Red exit } # -------------------------- 3. OU位置选择 -------------------------- Write-Host "`n请选择设备所属OU位置:" -ForegroundColor Cyan Write-Host "1. 测试组[默认]" Write-Host "2. 合规工作站" Write-Host "3. 控制工作站" Write-Host "4. 处理工作站" Write-Host "5. 质检工作站" Write-Host "6. 收货工作站" Write-Host "7. 发货工作站" $ouChoice = Read-Host "请输入选项(1-7,直接回车选默认)" # 校验OU选择输入 while ($ouChoice -notin @("1","2","3","4","5","6","7","")) { Write-Host "无效输入,请输入1-7之间的数字" -ForegroundColor Red $ouChoice = Read-Host "请重新输入(1-7,直接回车选默认)" } # 映射OU路径(请替换为实际AD结构!!!) $ouMap = @{ 1 = "OU=Test,DC=yourdomain,DC=com" 2 = "OU=Compliance,DC=yourdomain,DC=com" 3 = "OU=Controls,DC=yourdomain,DC=com" 4 = "OU=Processing,DC=yourdomain,DC=com" 5 = "OU=QC,DC=yourdomain,DC=com" 6 = "OU=Receiving,DC=yourdomain,DC=com" 7 = "OU=Shipping,DC=yourdomain,DC=com" } $ouChoiceInt = if ($ouChoice -eq "") {1} else {[int]$ouChoice} $targetOUPath = $ouMap[$ouChoiceInt] # -------------------------- 4. 凭证获取与验证 -------------------------- $cred = Get-Credential -Message "请输入有权限加域/重命名的域账号(格式:DOMAIN\USER)" # 验证凭证有效性 if (-not (Test-ADCredential -Credential $cred)) { Write-Host "凭证验证失败,请检查账号格式或权限" -ForegroundColor Red exit } # -------------------------- 5. 操作确认 -------------------------- Write-Host "`n=== 操作确认 ===" -ForegroundColor Yellow Write-Host "设备类型前缀:$devicePrefix" Write-Host "生成的计算机名:$newCompName" # 增加当前名对比提示 if ($newCompName -eq $env:COMPUTERNAME) { Write-Host "注意:生成的计算机名与当前系统名一致,将仅执行加域操作" -ForegroundColor Yellow } Write-Host "目标域:$domainName" Write-Host "目标OU路径:$targetOUPath" Write-Host "使用凭证:$($cred.UserName)" $confirm = Read-Host "`n确认执行请输入Y,取消请输入任意键" if ($confirm -notmatch "^[Yy]$") { Write-Host "操作已取消" -ForegroundColor Gray exit } # -------------------------- 6. 执行重命名+加域 -------------------------- try { Write-Host "`n正在执行重命名与域加入操作..." -ForegroundColor Cyan # 一步完成重命名+指定OU加域,无需单独执行Rename-Computer Add-Computer -NewName $newCompName ` -DomainName $domainName ` -OUPath $targetOUPath ` -Credential $cred ` -ErrorAction Stop Write-Host "操作成功,重启后生效" -ForegroundColor Green # 重启确认 $restartConfirm = Read-Host "是否立即重启?输入Y确认重启,其他键取消" if ($restartConfirm -match "^[Yy]$") { Restart-Computer -Force } } catch { Write-Host "操作失败:$_" -ForegroundColor Red exit }
使用注意事项
- 必须以管理员身份运行PowerShell脚本(重命名、域加入需要系统权限)
- 请将脚本中
$domainName、$ouMap里的域路径、OU路径替换为你公司实际的AD结构(可通过dsquery ou命令查询实际OU路径) - 资产标签请使用合规字符(仅数字、字母、连字符),长度建议控制在13位以内(加2位前缀后不超15位NETBIOS限制)
- 所用域账号需具备创建/修改域计算机对象、本地计算机重命名的权限
内容的提问来源于stack exchange,提问作者Justin Sanin
相关产品推荐
相关产品推荐

