繁体   English   中英

Powershell 在参数中使用变量作为参数的问题

[英]Powershell issue with using variable as a parameter in argument

我正在尝试创建一个脚本,它将解锁所有组织域控制器上的用户。

Import-Module ActiveDirectory
Add-Type -AssemblyName PresentationFramework

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

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$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.AcceptButton = $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()

$name = $tb.Text

$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) {

    $index
    Unlock-ADAccount -Identity ($name) -Server $index
    
}

我遇到了这个问题:

Unlock-ADAccount : Cannot validate argument on parameter 'Server'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Users\jdewolf\Downloads\DCAccountUnlocker.ps1:60 char:45
     Unlock-ADAccount -Identity ($name) -Server $index
                                                ~~~~~~
     CategoryInfo          : InvalidData: (:) [Unlock-ADAccount], ParameterBindingValidationException
     FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.UnlockADAccount

$index输出服务器如果。 我注释掉解锁行,只注释掉 output 的值。 我不确定为什么在参数中使用它是null

您的问题是一个简单的错字,但产生的症状很微妙:

'BXDC01.DOMAIN.org'.'NJDC01.DOMAIN.org'中,. ,假设您的意思是这两个主机名是单独的数组元素

至于症状:

  • 'BXDC01.DOMAIN.org'.'NJDC01.DOMAIN.org'被解释为想要访问(文字) [string]实例'BXDC01.DOMAIN.org'上字面上名为NJDC01.DOMAIN.org属性

  • 鉴于[string]实例没有这样的属性,PowerShell 悄悄地默认返回$null

    • 请注意,如果Set-StrictMode -Version 2或更高版本生效,它将改为报告语句终止错误
  • $null值因此成为数组的一部分并由foreach语句枚举。 因此,在第一次迭代中$index$null ,导致您看到的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM