0
votes

I did an Ad bulks script but doesn't seem to work for some reason. can someone help me ?

$ADUsers = Import-csv C:\Users\Deng\Desktop\newusers.csv

foreach ($User in $ADUsers)
{
$Username  = $Users.username
   $Firstname = $Users.firstname
   $Password  = $Users.password
   $Lastname  = $Users.lastname
   $OU        = $Users.ou

         New-ADUser `
        -SamAccountName $Username `
        -UserPrincipalName [email protected] `
        -Name $Firstname $Lastname `
        -GivenName $Firstname `
        -Surname $Lastname `
        -Enabled $True `
        -ChangePasswordAtLogon $false `
        -DisplayName $Lastname, $Firstname `
        -Path $OU `
        -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
}

I get this error below :

ConvertTo-SecureString: Cannot bind argument to parameter 'String' because it is null. At line:24 char:54 + ... -AccountPassword (convertto-securestring $Password -AsPlai ... + ~~~~~~~~~ + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

also see the content of the CSV

usersname firstname password lastname ou
efranklin Edward P@s$word Franklin "OU=PII Users,DC=PII,DC=net"
bjackson Bill P@s$word Jackson "OU=PII Users,DC=PII,DC=net"

I have made the change and this is what i get – DavidNG

ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null. At line:24 char:54 + ... -AccountPassword (convertto-securestring $Password -AsPlai ... –

still getting the same error

2
What is $Users?Caramiriel
@Caramiriel $users is the variable to define users in the csvDavidNG
I think @Caramiriel 's point is it should be $User, because that's what you specified in the ForEachSteven
I'm starting to think your input CSV file is not a CSV file at all, but a text file in the exact (list) format as you show us here. Please edit your question and add the first three or four lines of the inputfile to show what it truly looks like.Theo
I have edited your question to put that in as readable, formatted text. Am I correct in thinking the CSV uses a space character as field delimiter? In that case simply add this to the Import-Csv cmdlet as parameter -Delimiter ' ' and take the advice from @Steven to use Splatting to get rid of those horrible backticks.Theo

2 Answers

0
votes

Your mistake that in foreach loop you operating by object $user not $users, so you must write:

   $Username  = $User.username
   $Firstname = $User.firstname
   $Password  = $User.password
   $Lastname  = $User.lastname
   $OU        = $User.ou

Import-Csv read file row by row so first row must contains names of column like:

username;firstname;password;lastname;ou

Then might be your data:

efranklin;Edward;P@s$word;Franklin;"OU=PII Users,DC=PII,DC=net"

Additional You must have something like that(and don't forget in csv file must be ';' delimiter!!!!! separating parameters):

$ADUsers = Import-csv C:\Users\Deng\Desktop\newusers.csv -Delimiter ";"

foreach ($User in $ADUsers){
   $Username  = $User.username
   $Firstname = $User.firstname
   $Password  = $User.password
   $Lastname  = $User.lastname
   $OU        = $User.ou
New-ADUser -SamAccountName $Username -UserPrincipalName ($Username+'@aptushealth.com') -Name ($Firstname+' '+$Lastname) -GivenName $Firstname -Surname $Lastname -Enabled $True -ChangePasswordAtLogon $false -DisplayName ($Lastname+' '+$Firstname) -Path $OU -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force)            
}
0
votes

Adding some additional advice.

In your original code you were assigning the wrong values. As several participants have mentioned (see @Vad Answer) $Users would have been null in the code you submitted. It makes sense that would cause an error that specifically cites a null value. The error is very literal ConvertTo-SecureString will not accept a null value for the -String Parameter.

You used the iteration variable $User but made variable assignments like:

$Password = $Users.password

$Users (notice the "s") would be null in your sample and you should use $User.password instead!

Now, if you did all that and you are still getting the error. My advice would be to step back and check the data before executing New-ADUser . I'd first comment out New-ADUser . Then you can perform checks like:

$ADUsers = Import-Csv C:\Users\Deng\Desktop\newusers.csv
$ADUsers | Where-Object($_.Password -eq $null) | Format-Table

That would give you all the objects with a null password property. Those would need to be fixed before you try to use the content as input to New-ADUser .

Note: You can also use Excel for this sort of checking.

Password is just one of the properties. I would repeat the test citing other properties in the Where{} clause. You have to make sure the data is completely clean!


Original comments supplemental to other answers:

I think this has already been answered, but for readability I think this is a good use case for splatting the parameters.

Untested Example:

$ADUsers = Import-Csv C:\Users\Deng\Desktop\newusers.csv

foreach ($User in $ADUsers)
{
    $Params =
    @{
        samAccountName        = $User.username
        UserPrincipalName     = ($User.username + '@aptushealth.com')
        GivenName             = $User.FirstName
        SurName               = $User.LastName
        Name                  = ($User.FirstName + ' ' + $User.lastname)
        DisplayName           = ($User.lastname + ' ' + $User.FirstName)
        Enabled               = $true
        Path                  = $User.ou
        AccountPassword       = (ConvertTo-SecureString $User.password -AsPlainText -Force)
        ChangePasswordAtLogon = $false

    }

    New-ADUser @Params
}

This saves you from all the back tick escaping which is generally considered hazardous.