Link to home
Start Free TrialLog in
Avatar of IT Guy
IT GuyFlag for United States of America

asked on

Error while running Server 2016 Power Shell command

I am trying to follow the instructions within the the https://social.technet.microsoft.com/wiki/contents/articles/24541.powershell-bulk-create-ad-users-from-csv-file.aspx#Code_Used webpage to import username information from a .CSV file to create usernames within Server 2016 Active Directory but every time I try to do this I receive the following error messages:

When I run the following command within PowerShell (running as an administrator) .\un.ps1 I get the error message

 “ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.
At C:\Support\UN\UN.ps1:12 char:232
+ ... scription" -AccountPassword (ConvertTo-SecureString $Password -AsPlai ...
+                                                         ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.
At C:\Support\UN\UN.ps1:12 char:232
+ ... scription" -AccountPassword (ConvertTo-SecureString $Password -AsPlai ...
+                                                         ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand” which displays for every username that I attempt to create (see the screenshot).

I have included the CSV spreadsheet I am using with this command.

What needs to be done to fix this so I will be able to successfully import the information to create these usernames within Server 2016 Active Directory?

Here is a screenshot of these error messages:

User generated imageI receive these errors when running the .\un.ps1  command from an elevated PowerShell

Here is a screenshot of the CSV file:

User generated image
UN.csv

In this case the CSV file is saved in the "File format is CSV UTF-8 Comma delimited" format. Is this the correct format to save the file in?

User generated image
The context of the UN.ps1 PowerShell command is:

$Users = Import-Csv -Path "C:\support\un\CompanyUN.csv"            
foreach ($User in $Users)            
{            
    $Displayname = $User.Firstname + " " + $User.Lastname            
    $UserFirstname = $User.Firstname            
    $UserLastname = $User.Lastname            
    $OU = "$User.OU"            
    $SAM = $User.SAM            
    $UPN = $User.Firstname + "." + $User.Lastname + "@" + $User.Maildomain            
    $Description = $User.Description            
    $Password = $User.Password            
    New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true -server domain.loc            
}

Open in new window


What needs to be done to fix this so I will be able to successfully import & create these Server 2016 Active Directory users using this power shell command and the CSV file?

Please let me know if any further information is needed.
Avatar of IT Guy
IT Guy
Flag of United States of America image

ASKER

I have already Changed SamAccountName to sAMAccountName but still continue to receive the exact same error message.
Avatar of Nagendra Pratap Singh
Please check with the second version of the code from the same page.
Avatar of IT Guy

ASKER

Nagendra Pratap Singh,

I have used the second version of the code on the same page and now I receive the following errors:

PS C:\Support\UN> .\un.ps1
ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.
At C:\Support\UN\UN.ps1:13 char:232
+ ... scription" -AccountPassword (ConvertTo-SecureString $Password -AsPlai ...
+                                                         ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSe
   cureStringCommand

ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null.
At C:\Support\UN\UN.ps1:13 char:232
+ ... scription" -AccountPassword (ConvertTo-SecureString $Password -AsPlai ...
+                                                         ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSe
   cureStringCommand

Here is the screenshot:
User generated image
SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT Guy

ASKER

footech,

I have added these headers to the .CSV file.

Now whenI run the .PS1 powershell command I get the following error message:

PS C:\Support\UN> .\un.ps1
New-ADUser : Unable to contact the server. This may be because this server does not exist, it is currently down, or it
does not have the Active Directory Web Services running.
At C:\Support\UN\UN.ps1:13 char:5
+     New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamA ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [New-ADUser], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.NewADUser

User generated image
And here is the spreadsheet with these headers at the top of the spreadsheet
User generated image
Here is a screenshot of Active Directory Users and Computers for this Server 2016 AD domain
User generated image
What else needs to be done to fix this latest error?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The parameter is "-asPlain" check that

Here's a similar question I think this question will clarify your doubt.

https://www.experts-exchange.com/questions/29048293/I-need-to-add-user-by-csv-file-to-AD-with-PowerShell.html
Avatar of IT Guy

ASKER

J0rt3g4,

Are you saying that the -asPlain should be removed? Or should it be modified to something else?

Here are screenshots of the PowerShell script

User generated imageUser generated imageContinuation of PowerShell script

P.S. I have renamed the .CSV file name to CompanyUN.csv

Also now when I run the Power Shell script I get these errors:

PS C:\Support\UN> .\un.ps1
New-ADUser : Directory object not found
At C:\Support\UN\UN.ps1:13 char:5
+     New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamA ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (CN=Accounts Pay...=a______,DC=com:String) [New-ADUser], ADIdentityNotFo
   undException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
   icrosoft.ActiveDirectory.Management.Commands.NewADUser

New-ADUser : Directory object not found
At C:\Support\UN\UN.ps1:13 char:5
+     New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamA ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (CN=Al________...=a_____,DC=com:String) [New-ADUser], ADIdentityNotFo
   undException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
   icrosoft.ActiveDirectory.Management.Commands.NewADUser

[PLEASE NOTE: Usernames and Domain names have been blanked out]

User generated image[PLEASE NOTE: Usernames and Domain names have been blanked out]

User generated imageHere is a screenshot of the CSV file. Am I using the correct header names? Also should the OU simply be OU=users,DC=companyname,DC=com

User generated imageHere is a screenshot of our Active Directory users and computers. Is the OU context of "OU=users,DC=companyname,DC=com" correct?

Please let me know if any further information is needed.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT Guy

ASKER

footech,

Thank you very much for your help!

I followed the instructions in your last comment, ran the PowerShell script again, and now all of the AD accounts have been created.