You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell批量解锁域用户脚本报错:变量作为Server参数时提示参数为空

解决Unlock-ADAccount的Server参数为空错误

首先,你的脚本里有一个直接导致问题的语法错误:在定义$Sites数组时,第一个域控制器BXDC01.DOMAIN.org和第二个域控制器NJDC01.DOMAIN.org之间用了点号.代替了数组元素分隔需要的逗号,。这个错误会让PowerShell解析时出现异常,最终生成的数组里可能包含无效的空元素,当循环到这个空值时,Unlock-ADAccount的Server参数就会为空,触发你看到的验证错误。

除此之外,我还建议对脚本做几个优化,提升稳定性和用户体验:

1. 修复表单按钮的逻辑问题

你同时把解锁按钮和关闭按钮都设为了AcceptButton,这会导致只有最后设置的按钮生效。正确的做法是把关闭按钮设为CancelButton:

$testform.CancelButton = $test

2. 添加用户输入验证

在执行解锁操作前,先检查用户是否输入了有效的用户名,避免后续命令因为空值报错:

$rs = $testform.ShowDialog()
if ($rs -eq [System.Windows.Forms.DialogResult]::OK) {
    $name = $tb.Text.Trim()
    if ([string]::IsNullOrEmpty($name)) {
        [System.Windows.Forms.MessageBox]::Show("请输入用户名!", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
        exit
    }
    # 后续解锁逻辑
}

3. 加入错误处理机制

在循环处理域控制器时,用try/catch块捕获每个操作的错误,这样某个域控制器操作失败不会终止整个脚本,还能明确提示错误信息:

foreach ($index in $Sites) {
    Write-Host "正在处理域控制器: $index" -ForegroundColor Cyan
    try {
        Unlock-ADAccount -Identity $name -Server $index -ErrorAction Stop
        Write-Host "用户 $name 在 $index 上解锁成功!" -ForegroundColor Green
    }
    catch {
        Write-Host "用户 $name 在 $index 上解锁失败: $_" -ForegroundColor Red
    }
}

修复后的完整脚本

Import-Module ActiveDirectory
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

Write-Host "Tool for unlocking user on all Domain Controllers" -ForegroundColor Green

$testform = New-Object System.Windows.Forms.Form
$testform.Text = 'Account Unlock Tool'
$testform.Size = New-Object System.Drawing.Size(250,200)
$testform.StartPosition = 'CenterScreen'

# 解锁按钮
$okb = New-Object System.Windows.Forms.Button
$okb.Location = New-Object System.Drawing.Point(25,130)
$okb.Size = New-Object System.Drawing.Size(75,25)
$okb.Text = 'Unlock'
$okb.DialogResult = [System.Windows.Forms.DialogResult]::OK
$testform.AcceptButton = $okb
$testform.Controls.Add($okb)

# 关闭按钮
$test = New-Object System.Windows.Forms.Button
$test.Location = New-Object System.Drawing.Point(125,130)
$test.Size = New-Object System.Drawing.Size(75,25)
$test.Text = 'close'
$test.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$testform.CancelButton = $test
$testform.Controls.Add($test)

# 提示标签
$lb = New-Object System.Windows.Forms.Label
$lb.Location = New-Object System.Drawing.Point(20,40)
$lb.Size = New-Object System.Drawing.Size(240,20)
$lb.Text = 'Please enter the username in text box:'
$testform.Controls.Add($lb)

# 用户名输入框
$tb = New-Object System.Windows.Forms.TextBox
$tb.Location = New-Object System.Drawing.Point(40,80)
$tb.Size = New-Object System.Drawing.Size(150,20)
$testform.Controls.Add($tb)

$testform.Topmost = $true
$testform.Add_Shown({$tb.Select()})
$rs = $testform.ShowDialog()

if ($rs -eq [System.Windows.Forms.DialogResult]::OK) {
    $name = $tb.Text.Trim()
    if ([string]::IsNullOrEmpty($name)) {
        [System.Windows.Forms.MessageBox]::Show("请输入用户名!", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
        exit
    }

    # 修复后的域控制器数组
    $Sites = @('BXDC01.DOMAIN.org','NJDC01.DOMAIN.org','BXDC02.DOMAIN.org','BXDC03.DOMAIN.org','NJDC02.DOMAIN.org','NJAD01.DOMAIN.org','LMBAD02.DOMAIN.org','BXAD01.DOMAIN.org','HQDC01.DOMAIN.org')
    
    foreach ($index in $Sites) {
        Write-Host "正在处理域控制器: $index" -ForegroundColor Cyan
        try {
            Unlock-ADAccount -Identity $name -Server $index -ErrorAction Stop
            Write-Host "用户 $name 在 $index 上解锁成功!" -ForegroundColor Green
        }
        catch {
            Write-Host "用户 $name 在 $index 上解锁失败: $_" -ForegroundColor Red
        }
    }
}

内容的提问来源于stack exchange,提问作者JuneAtalanta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 16:04:10