如何使用PowerShell 2.0解锁其他域上的Active Directory帐户?

时间:2015-01-02 09:28:37

标签: powershell active-directory powershell-v2.0

我发现了LazyWinAdmin的一个令人惊奇的PowerShell脚本,它可以满足我的需求 - 但它仅限于当前域。我们的网络设置方式我们为某些类型的帐户设置了不同的域名。

我正在尝试编写一个脚本,只需解锁特定域上的指定用户帐户。我们的系统使用PowerShell 2.0,这使得这非常困难,因为我知道更高版本具有Active Directory管理cmdlet。相信我,我已经要求我们在我们的系统上安装了更新版本的PowerShell,但该公司拒绝让步。

我觉得有点愚蠢,因为我过去几乎只使用过新版本,所以我习惯了各种cmdlet,而不是手动草拟我想做的每一件事。

1 个答案:

答案 0 :(得分:0)

您需要指定搜索根以从其他域搜索。

$ buttonUnlock_Click中的原始代码:

# Search for this account in the current domain
$Searcher = [ADSISearcher]"(sAMAccountName=$Name)"
$Results = $Searcher.FindOne()

同样在$ buttonCheck_Click(它没有搜索代码,只是评论):

# Search for this account in the current domain

将两者更改为:

$searcher = New-Object DirectoryServices.DirectorySearcher
$searcher.Filter = "(sAMAccountName=$name)"
$searcher.SearchRoot = New-Object DirectoryServices.DirectoryEntry('LDAP://other.domain', 'user', 'pwd')
$results = $searcher.FindOne()

如果当前用户已经拥有访问其他域的权限,您可以简单地将[adsi]'LDAP://other.domain'作为搜索根。