簡體   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