1

We're trying to bulk-create users on a Windows 2012 Server system; the system is only intended to allow us to validate an ALM solution and it will be extremely short lived, which means we do not care for now for security. When running the script below, the shell reports Exception calling "SetInfo" with "0" argument(s): "The specified username is invalid." for each line in the input file. However, if we manually execute the lines inside the loop, the user is properly created.

# Work on the local system
$Computer =[ADSI]"WinNT://$env:ComputerName,Computer"
$UsersFilePath = "C:\users\Administrator\Downloads\ActiveUsers.txt"
$Group=[ADSI]"WinNT://WORKGROUP/./Developers,Group"

Get-Content $UsersFilePath | % {

$_ # Debug only line, remove it when done
$LocalUser=$Computer.Create("User", $_ )
$LocalUser.SetPassword( $_ )
$LocalUser # Debug only line, remove it when done
$LocalUser.SetInfo()
} 

How should we write the loop?

[Here is the answer, as suggested by mjolinor; the main change is the trimming of the name read from the file]

# Work on the local system
$Computer =[ADSI]"WinNT://$env:ComputerName,Computer"
$UsersFilePath = "C:\users\Administrator\Downloads\ActiveUsers1.txt"
$Group=[ADSI]"WinNT://WORKGROUP/./Developers,Group"

Get-Content $UsersFilePath | % {

$_ # Debug only line, remove it when done
$UserName = $_.Trim()
$LocalUser=$Computer.Create("User", $UserName)
$LocalUser.SetPassword( $UserName )
#$LocalUser.put("userPrincipalName", "${name}@svr-tfs2013" )
$LocalUser # Debug only line, remove it when done
$LocalUser.SetInfo()
$Group.Add($LocalUser.path)
}
7
  • 2
    If it works with manual input, but not with your loop, I'd be suspicious of that input file. Are you sure the user names in that file are good (no trailing whitespace, etc.)? Commented Nov 14, 2013 at 13:20
  • 1
    Have you tried using the powershell activedirectory module, the new-aduser cmdlet? Commented Nov 14, 2013 at 14:03
  • 2
    I'm no ADSI expert but looking at the docs, are you supposed to use the Put method with SetInfo e.g. $localUser.Put("userPrincipalName", "${name}@$domain"); $localUser.SetInfo() or maybe you need to put the SAMAccountName as well? Commented Nov 14, 2013 at 14:34
  • @MdMoore313: the server is in a workgroup, not a Windows domain; we cannot use the AD cmdlets. Commented Nov 14, 2013 at 14:55
  • 1
    @mjolinor: you are correct. Please create an answer to the question, so I can accept it. The problem occurred because each name in the file was followed by a large number of spaces. Commented Nov 14, 2013 at 15:00

1 Answer 1

2

Per the comments, it appears your input file may have errors in the usernames (trailing whitespace).

One way to fix that is to use .trim() on the input to remove any leading or trailing whitespace that might be in the file:

# Work on the local system
$Computer =[ADSI]"WinNT://$env:ComputerName,Computer"
$UsersFilePath = "C:\users\Administrator\Downloads\ActiveUsers.txt"
$Group=[ADSI]"WinNT://WORKGROUP/./Developers,Group"

Get-Content $UsersFilePath | % {

$_ # Debug only line, remove it when done
$LocalUser=$Computer.Create("User", $_.trim() )
$LocalUser.SetPassword( $_.trim() )
$LocalUser # Debug only line, remove it when done
$LocalUser.SetInfo()
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.