简体   繁体   中英

Powershell - Create Mass AD-USERS

I am building up a script to create Mass AD-USers with a csv file.

I am using Powershell for this.

Here is the error message that I get for this command:

$UserLogin = ($UserFirstName).Substring(0,1).ToLower + "." + $UserName.ToLower()

Error message Cannot call a method inside a null expression. To Character Line:10:1

$UserLogin = ($UserFirstName).Substring(0,1).ToLower + "." ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
    CategoryInfo: InvalidOperation: (:) [], RuntimeException
    FullyQualifiedErrorId: InvokeMethodOnNull

Also when I run my script, it does not put the datas of my csv in Active Directory

Could anyone help me and explain to me how to fix the message error and how to have my csv datas transferred in my Active Directory?

Good day Emmanuel

It seems like you are getting a Null exception what means your variables are empty.

You can try to get the info using the cmdlet Import-csv

For example: You have a Csv with few columns --> Name, LastName, Username and Description.

Then you need to import these information to a variable.

$users = Import-Csv [Path].users.csv

Then you will be able to get the info of these columns with $users.name and $users.username

Now to fix you problem you should do this:

$UserLogin = $users.name.Substring(0,1).ToLower() + "." + $users.username.ToLower()

You forgot to add "()" in the first.tolower -->.tolower()

Using me as example:

$users
Name Username
---- --------
Juan Quintero

$users.name.Substring(0,1).ToLower() + "." + $Users.username.ToLower()
j.quintero

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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