简体   繁体   English

将多个用户添加到 AD powershell 脚本

[英]Add multiple users to AD powershell script

I have a powershell script to remove a user stored under the variable $User which is taken from a user input in the command line.我有一个 powershell 脚本来删除存储在变量 $User 下的用户,该变量取自命令行中的用户输入。 How do I specify multiple users and remove all of them?如何指定多个用户并删除所有用户?

Script is below脚本如下

$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")

Agree with @Theo but if you know what you are doing there is a simple solution:同意@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")

All you need to know is delimiter which you have to use.您只需要知道必须使用的分隔符。 In that case it is ',', so you need to pass logins in pattern: user1,user2在这种情况下它是',',所以你需要以模式传递登录:user1,user2

perhaps this helps little with getting a more save result.也许这对获得更多的保存结果帮助不大。 it took me a few minutes to write it.我花了几分钟来写它。 maybe it helps.也许它有帮助。


######################################
# 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM