我正忙着编写一个脚本,其中声明了一个变量,其中包含可以在提示中输入的用户信息。 当我看到无法使用电子邮件地址(2000年之后的登录名)来完成解锁帐户时,我想到了这种方式。 但是我不知道自己在做什么错。
这是我现在正在运行的代码。
Import-Module ActiveDirectory
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "$env:username")
$Test = Get-ADUser -Filter { EmailAddress -eq $name } | Select SamAccountName
Unlock-ADAccount -Identity $Test
我遇到的错误是
Unlock-ADAccount : Object reference not set to an instance of an object.
At C:\Users\Admcbl\Documents\Powershell.ps1:5 char:1
+ Unlock-ADAccount -Identity $Test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Microsoft.Activ...ement.ADAccount:ADAccount) [Unlock-ADAccount], NullReferenceException
+ FullyQualifiedErrorId : Object reference not set to an instance of an object.,Microsoft.ActiveDirectory.Management.Commands.UnlockADAccount
答案 0 :(得分:0)
您应该参考SamAccountName
的{{1}}属性:
$Test
但是为什么不直接将帐户发送到Unlock-ADAccount -Identity $Test.SamAccountName
?像这样:
Unlock-ADAccount
答案 1 :(得分:0)
这可能是您更简单的解决方案。请注意,我已切换过滤器,因此不需要@domain.com
Import-Module ActiveDirectory
$name = Read-Host -Prompt "Enter Username "
Get-ADUser -Filter {SamAccountName -eq $name} |
Unlock-ADAccount