使用Powershell从csv批量导入AD用户,而无需用户干预 [英] Bulk import AD users from csv using powershell without user intervention

查看:383
本文介绍了使用Powershell从csv批量导入AD用户,而无需用户干预的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ADUser powershell cmdlet从csv文件导入用户,这是脚本

I'm trying to import users from a csv file using ADUser powershell cmdlet and here's the script

Import-Module ActiveDirectory
$csvcontent = Import-CSV -Path "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\import_create_ad_users_2a.csv"

foreach ($user in $csvcontent) {
$samAccountName = $user.GivenName.substring(0,1).ToLower()+$user.LastName.ToLower()
$userPrincinpal = $samAccountName+"@mmc.local"
New-ADUser
-AccountPassword (ConvertTo-SecureString -AsPlainText $user.Password -force)`
-ChangePasswordAtLogon $false`
-Company "mmc LLP."`
-DisplayName ($user.GivenName+""+$user.Lastname)`
-userprincipalname $userPrincinpal`
-SamAccountName $samAccountName`    -Name ($user.GivenName+""+$user.Lastname)`
-Path "CN=Users,DC=mmc,DC=local"`
-state $user.County`
-givenname $user.GivenName`
-surname $user.Lastname`
-description ($user.Description)`
-Enabled $true`

Add-ADGroupMember "mmc_Users" $samAccountName;
}

但是当我在Powershell中运行命令时,出现如下提示并且我想导入csv文件中列出的所有用户,而无需任何用户干预。

But when I run the command in powershell, I get a prompt as listed below and I would like to import all the users listed in the csv file without any user intervention.

cmdlet New-ADUser at command pipeline position 1
Supply values for the following parameters:
Name:

请查看脚本,让我知道如何解决此问题。

Please review the script and let me know how to fix this.

FYI-Powershell初学者

FYI - Powershell beginner

谢谢

Karthik

推荐答案

反引号通常值得避免。它们通过转义下一个字符来工作,该字符在行的末尾是换行符,因此它使命令得以继续。但是,它很容易在反引号后留下一个您看不到的空格,最终导致转义而不是换行符。上面的情况似乎并非如此,但正如TessellatingHeckler指出的那样,您在 New-ADUser 之后错过了一个。

Backticks are generally worth avoiding. They work by escaping the next character, which on the end of a line is the newline character so it allows the command to continue. However its too easy to end up with a space after the backtick that you can't see, which then ends up getting escaped and not the newline. That doesn't seem to be the case above, but as TessellatingHeckler pointed out you were missing one after New-ADUser.

更好的解决方案(以防止代码过于水平)将使用像这样:

A better solution (to keep the code from going too far horizontal) would be to use splatting like this:

Import-Module ActiveDirectory
$csvcontent = Import-CSV -Path "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\import_create_ad_users_2a.csv"

foreach ($user in $csvcontent) {

    $samAccountName = $user.GivenName.substring(0,1).ToLower()+$user.LastName.ToLower()
    $userPrincinpal = $samAccountName+"@mmc.local"

    $NewUserParams = @{
        AccountPassword = (ConvertTo-SecureString -AsPlainText $user.Password -Force)
        ChangePasswordAtLogon = $false
        Company = "mmc LLP."
        DisplayName = ($user.GivenName+""+$user.Lastname)
        userprincipalname = $userPrincinpal
        SamAccountName = $samAccountName  
        Name = ($user.GivenName+""+$user.Lastname)
        Path = "CN=Users,DC=mmc,DC=local"
        state = $user.County
        givenname = $user.GivenName
        surname = $user.Lastname
        description = ($user.Description)
        Enabled = $true
    }

    New-ADUser @NewUserParams
    Add-ADGroupMember "mmc_Users" $samAccountName
}

此操作通过创建哈希表 @ {} ,其中包含要设置的每个参数,然后使用特殊的 @ 字符将该哈希表发送到cmdlet。

This works by creating a hashtable @{ } with each of the parameters in it that you want to set and then sending that hashtable to the cmdlet with the special @ character.

这篇关于使用Powershell从csv批量导入AD用户,而无需用户干预的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆