【问题标题】:Add multiple users to AD powershell script将多个用户添加到 AD PowerShell 脚本
【发布时间】:2020-11-09 12:24:39
【问题描述】:

我有一个 powershell 脚本来删除存储在变量 $User 下的用户,该变量取自命令行中的用户输入。如何指定多个用户并删除所有用户?

脚本在下面

$User = Read-Host - Prompt 'Enter user name'
Remove-ADUser $User
Write-Host "'$user' account has been removed press any key to close..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

【问题讨论】:

  • 哇,这是等待意外的代码。你根本没有检查用户的输入,你没有解释他们需要输入登录名,你只是假设他们输入的是正确的用户删除(这​​是一种破坏性操作)。真的,你应该认真考虑一下!
  • 这不是一般用途的脚本,我将是唯一使用它的人,并且只有我知道需要删除的用户名
  • @Theo 如果我要检查他们是否正确输入了用户名,添加数据验证以确保以正确格式输入用户名的最佳方法是什么? Powershell 脚本的新手。
  • @MacHooper 找到正确帐户的最佳方法是首先检索帐户并验证它是否正确。有多种方法可以编写验证脚本,但这完全取决于 AD 的结构以及您构建信息的方式。在下面的 4 步脚本中,我在实际删除之前首先获得了测试结果(它找到了哪个帐户?)。一旦您确定代码只会找到并删除目标帐户,就有很多方法可以改进脚本(例如用于自动化目的)

标签: powershell scripting active-directory windows-scripting


【解决方案1】:

同意@Theo,但如果您知道自己在做什么,那么有一个简单的解决方案:

$User = Read-Host - Prompt 'Enter user name'
foreach($u in $User.Split(',')) 
{
   Remove-ADUser $u
   Write-Host "'$u' account has been removed"
}

$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

您只需要知道必须使用的分隔符。在那种情况下它是',',所以你需要传递登录模式:user1,user2

【讨论】:

  • 工作得非常好,谢谢你,因为我说过加快工作速度是为了我自己。我知道用户名格式和需要删除的用户
【解决方案2】:

也许这对获得更多的保存结果帮助不大。 我花了几分钟来写它。也许有帮助。


######################################
# first make sure we know what is happing..
######################################

$name = 'bob'
$AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}

######################################
# then go a step further
######################################

$name = 'bob'
$AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}

#show results of filter
$AccountToDelete.name

if ($AccountToDelete.count -gt 1)
    {
        write-warning 'more then one user:'
        $AccountToDelete.name
        BREAK
    } 
    ELSE 
    {
    
    'delete {0}' -f $AccountToDelete.name
    Remove-ADUser $AccountToDelete -WhatIf
}


######################################
# improvement 1
######################################

$names = 'bob','don' 

foreach ($name in $names){

    $AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}

    #show results of filter
    $AccountToDelete.name

    if ($AccountToDelete.count -gt 1)
        {
            write-warning 'more then one user:'
            $AccountToDelete.name
            BREAK
        } 
     ELSE 
    {
    
        'delete {0}' -f $AccountToDelete.name
        Remove-ADUser $AccountToDelete -WhatIf    

    }
}


######################################
# improvement 2
######################################

#now add names to delete in a notepad textfile, one name per line
<#
you can use this to create a file
PS c:\users\administator> notepad users.txt 
#>

#replace the string arrary $names = 'bob','don' 
$names = (get-content .\users.txt).split('^t')
$names 
'processing {0} names...' -f $names.count

foreach ($name in $names){

    $AccountToDelete = Get-ADuser -filter {enabled -eq $true} -properties "displayname" | where {$_.displayname -match $name}

    #show results of filter
    $AccountToDelete.name

    if ($AccountToDelete.count -gt 1)
        {
            write-warning 'more then one user:'
            $AccountToDelete.name
            BREAK
        } 
     ELSE 
    {
    
        'delete {0}' -f $AccountToDelete.name
        Remove-ADUser $AccountToDelete -WhatIf    

    }
}

#finally if the script is showing you the results you need you can remove the -WhatIf 

【讨论】:

  • 没有答案那么短。正如 Theo 和 Simoff 已经提到的那样,您处理此问题的方式很容易出错。我总是在虚拟机中测试我的脚本(我做了一个来回答这个问题)。当它与广告相关时,我总是需要确保它确实在做我想要的。 (@Simoff:如果有人确切地知道他在做什么,他就不会在这里问这样的问题 ;-) ...)我的脚本是 4 个步骤来获得更可控的结果。如果它没有帮助你,它可能会帮助别人。一如既往……小心。测试 AD 恢复技术或恢复没有乐趣。 (如果生产)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
相关资源
最近更新 更多