[ADSI]::Exists 的问题
Issues with [ADSI]::Exists
我试图在尝试删除用户之前确定该用户是否存在。我找到了这个命令并试图将它实现到我的脚本中。但是我注意到,该命令将 return 为我输入的任何用户名,无论它是否存在。谁能帮我解释一下使用这个脚本的正确方法,或者谁能告诉我一个更好的方法来确定用户是否存在?
[ADSI]::Exists("WinNT://Lotzi")
以下代码应该会失败 b/c Lotzi 不是实际用户,但该命令会 return 为真。
这是检查 Active Directory 中是否存在特定帐户的一种快速方法:
$accountName = "testname"
$searcher = [ADSISearcher] "(sAMAccountName=$accountName)"
$accountExists = $searcher.FindOne() -ne $null
您不需要使用 ADSI,那是老办法。好吧,你可以,但只是说说而已。
使用 PowerShell AD cmdlet?
# Get parameters, examples, full and Online help for a cmdlet or function
(Get-Command -Name Get-ADUser).Parameters
Get-help -Name Get-ADUser -Examples
Get-help -Name Get-ADUser -Full
Get-help -Name Get-ADUser -Online
(Get-Command -Name Get-ADComputer).Parameters
Get-help -Name Get-ADComputer -Examples
Get-help -Name Get-ADComputer -Full
Get-help -Name Get-ADComputer -Online
这就是它们存在的原因。现在您需要下载并安装或仅在您的工作站上安装 Windows RSAT 工具...
...或远程访问域控制器以使用 AD cmdlet。
How To Use The 2012 Active Directory PowerShell Cmdlets From Windows 7
https://blogs.technet.microsoft.com/ashleymcglone/2013/06/27/how-to-use-the-2012-active-directory-powershell-cmdlets-from-windows-7
然后就做这样的事情...
$Users = 'TestUser001','TestUser001','TestUser001'
ForEach($User in $Users)
{
$User = $(try {Get-ADUser 'TestUser001'} catch {$null})
if ($User -ne $null) {
# Exists
} else {
# Doesn't Exist
Write-Warning -Message "User $User not found"
}
}
$Computers = 'Computer001','Computer001','Computer001'
ForEach ($Computer in $Computers)
{
$Computer = $(try {Get-ADUser 'TestUser001'} catch {$null})
if ($Computer -ne $null) {
# Exists
} else {
# Doesn't Exist
Write-Warning -Message "Computer $Computer not found"
}
}
我试图在尝试删除用户之前确定该用户是否存在。我找到了这个命令并试图将它实现到我的脚本中。但是我注意到,该命令将 return 为我输入的任何用户名,无论它是否存在。谁能帮我解释一下使用这个脚本的正确方法,或者谁能告诉我一个更好的方法来确定用户是否存在?
[ADSI]::Exists("WinNT://Lotzi")
以下代码应该会失败 b/c Lotzi 不是实际用户,但该命令会 return 为真。
这是检查 Active Directory 中是否存在特定帐户的一种快速方法:
$accountName = "testname"
$searcher = [ADSISearcher] "(sAMAccountName=$accountName)"
$accountExists = $searcher.FindOne() -ne $null
您不需要使用 ADSI,那是老办法。好吧,你可以,但只是说说而已。
使用 PowerShell AD cmdlet?
# Get parameters, examples, full and Online help for a cmdlet or function
(Get-Command -Name Get-ADUser).Parameters
Get-help -Name Get-ADUser -Examples
Get-help -Name Get-ADUser -Full
Get-help -Name Get-ADUser -Online
(Get-Command -Name Get-ADComputer).Parameters
Get-help -Name Get-ADComputer -Examples
Get-help -Name Get-ADComputer -Full
Get-help -Name Get-ADComputer -Online
这就是它们存在的原因。现在您需要下载并安装或仅在您的工作站上安装 Windows RSAT 工具...
...或远程访问域控制器以使用 AD cmdlet。
How To Use The 2012 Active Directory PowerShell Cmdlets From Windows 7 https://blogs.technet.microsoft.com/ashleymcglone/2013/06/27/how-to-use-the-2012-active-directory-powershell-cmdlets-from-windows-7
然后就做这样的事情...
$Users = 'TestUser001','TestUser001','TestUser001'
ForEach($User in $Users)
{
$User = $(try {Get-ADUser 'TestUser001'} catch {$null})
if ($User -ne $null) {
# Exists
} else {
# Doesn't Exist
Write-Warning -Message "User $User not found"
}
}
$Computers = 'Computer001','Computer001','Computer001'
ForEach ($Computer in $Computers)
{
$Computer = $(try {Get-ADUser 'TestUser001'} catch {$null})
if ($Computer -ne $null) {
# Exists
} else {
# Doesn't Exist
Write-Warning -Message "Computer $Computer not found"
}
}