[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2311
  • Last Modified:

powershell scrpit to populate user principal name from a list of users

I have  a list of users  in a CSV that is the SanAccountName. I need to run a powershell script to populate the LogOnName Portion of the UPN. here is what I have so far that If I use a specific username it works perfectly. I am just having issues calling on the list from the CSV file. any help would be great. thanks.

Import-Module ActiveDirectory

$UserList = Import-csv C:\temp\aduserUPNList.csv

foreach ($user in $userlist) {

get-aduser -SamAccountName $user | foreach {set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)} }


Error message:
Get-ADUser : A parameter cannot be found that matches parameter name 'SamAccountName'.
At line:7 char:12
+ get-aduser -SamAccountName $user | foreach {set-aduser -identity $_.SAMAccountNa ...
+            ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
0
TXRhino
Asked:
TXRhino
  • 4
  • 4
1 Solution
 
SubsunCommented:
Try..
Import-csv C:\temp\aduserUPNList.csv | %{
Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)
}

Open in new window

Input csv file should be like..
SAMAccountName 
UserA
UserB
UserC

Open in new window

0
 
TXRhinoAuthor Commented:
I tried that and ensured the csv file looked correct as well with no success.. here is the error I receive.

Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At line:2 char:22
+ Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “ ...
+                      ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
 
SubsunCommented:
The argument is null. Supply a non-null argument
The error says the value for $_.SAMAccountName is $null.. mostly the problem with input file.. Following code will ignore the empty values/lines in the input file..
Import-csv C:\temp\aduserUPNList.csv } ?{$_.SAMAccountName} | %{
Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)
}

Open in new window

If it is not working please post your sample input file..
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
TXRhinoAuthor Commented:
I double checked and the input file is just what was described in your original post. ran it again and this is what I get...

At line:3 char:38
+ Import-csv C:\temp\aduserUPNList.csv } ?{$_.SAMAccountName} | %{
+                                      ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
0
 
TXRhinoAuthor Commented:
Captureof-CSV.JPG
0
 
SubsunCommented:
There was a typo in last comment..
Import-csv C:\temp\aduserUPNList.csv | ?{$_.SAMAccountName} | %{
Set-aduser -identity $_.SAMAccountName -UserPrincipalName ($_.SAMAccountName + “@ci.fort-worth.tx.us”)
}

Open in new window

If it is not working just create a simple input file with two three users put the SAMAccountName's in a text file and try the following code..
GC C:\temp\aduserUPNList.txt | ?{$_} | %{
Set-aduser -identity $_ -UserPrincipalName ($_ + “@ci.fort-worth.tx.us”)
}

Open in new window

Text file..
UserA
UserB
UserC

Open in new window

0
 
SubsunCommented:
Check the header name in your csv.. there snould not be any trailing space after SAMAccountName.

Example "SAMAccountName " or "SAMAccountName   " which will give you error..
0
 
TXRhinoAuthor Commented:
The text file was the trick... Many Thanks.

Ryan
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now