[英]Powershell issue with using variable as a parameter in argument
I am trying to create a script, that will unlock a user on all organization domain controllers.我正在尝试创建一个脚本,它将解锁所有组织域控制器上的用户。
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
}
I am running into this issue:我遇到了这个问题:
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
outputs the server if. $index
输出服务器如果。 I comment out the unlock line and just output the value.我注释掉解锁行,只注释掉 output 的值。 I am not sure why it is
null
when used in the argument?我不确定为什么在参数中使用它是
null
?
Your problem is a simple typo , but the resulting symptom is subtle:您的问题是一个简单的错字,但产生的症状很微妙:
In 'BXDC01.DOMAIN.org'.'NJDC01.DOMAIN.org'
, replace .
在
'BXDC01.DOMAIN.org'.'NJDC01.DOMAIN.org'
中,将.
with ,
given that you mean these two hostnames to be separate array elements .与
,
假设您的意思是这两个主机名是单独的数组元素。
As for the symptom:至于症状:
'BXDC01.DOMAIN.org'.'NJDC01.DOMAIN.org'
is interpreted as wanting to access a property literally named NJDC01.DOMAIN.org
on the (literal) [string]
instance 'BXDC01.DOMAIN.org'
'BXDC01.DOMAIN.org'.'NJDC01.DOMAIN.org'
被解释为想要访问(文字) [string]
实例'BXDC01.DOMAIN.org'
上字面上名为NJDC01.DOMAIN.org
的属性
Given that a [string]
instance has no such property, PowerShell quietly defaults to returning $null
鉴于
[string]
实例没有这样的属性,PowerShell 悄悄地默认返回$null
Set-StrictMode
-Version 2
or higher were in effect, it would instead report a statement-terminating error .Set-StrictMode
-Version 2
或更高版本生效,它将改为报告语句终止错误。 The $null
value therefore becomes part of the array and is enumerated by the foreach
statement. $null
值因此成为数组的一部分并由foreach
语句枚举。 Thus, in the first iteration $index
is $null
, causing the error you saw.因此,在第一次迭代中
$index
是$null
,导致您看到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.