【问题标题】:Unlocking account from powershell ISE pre made script从 powershell ISE 预制脚本解锁帐户
【发布时间】:2018-10-15 12:30:38
【问题描述】:

我正忙着编写一个脚本,其中声明了一个变量,其中包含您可以在提示中输入的用户信息。 当我看到无法使用电子邮件地址(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

【问题讨论】:

  • 您遇到什么错误?到目前为止,您尝试过什么来解决此问题?快速浏览一下,这可能是因为您在 $TestGet-ADUser 之间缺少一个 = 符号
  • 同样SamAccountNam 应该是SamAccountName
  • 我是草率复制并粘贴到这里,但我收到的错误消息是我在帖子中更新的内容

标签: powershell variables account


【解决方案1】:

你应该参考$TestSamAccountName属性:

Unlock-ADAccount -Identity $Test.SamAccountName

但为什么不直接将帐户传送到Unlock-ADAccount?像这样:

Get-ADUser -Filter { EmailAddress -eq $name } | Unlock-ADAccount

【讨论】:

  • 第二个选项就像一个魅力谢谢!我不知道这可能是我的第一个脚本
【解决方案2】:

这可能是一个更简单的解决方案。请注意,我切换了过滤器,因此不需要 @domain.com

Import-Module ActiveDirectory

$name = Read-Host -Prompt "Enter Username "

Get-ADUser -Filter {SamAccountName -eq $name} | 
    Unlock-ADAccount

【讨论】:

    猜你喜欢
    • 2022-10-24
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多