1

I am currently working on an assignment for class, where we need to use the Dsadd command in order to import a list of CSV users into Active Directory.

This is my script:

$CSV = Import-Csv -Delimiter "," -Path "$PWD\TestCSV.csv"

foreach ($user in $CSV) 
{
    $Member = $User.Title + ","
    $Domain = $User.Domain + ", DC=Domains" + ", DC=com" + "`""
    $Name = $User.Firstname + " " + $User.Lastname + ","
    $Password = $User.Password
    write-host Dsadd user `"CN=$Name"" DC=$Domain -pwd $User.Password -memberof `"CN=$Member" CN=Users, DC=$Domain"
}

When I execute it, it prints the following to the powershell window:

Dsadd user "CN=Abruaba Rodriguez, DC=CoolDomain, DC=Domains, DC=Com" -pwd password
-memberof "CN=gk_students, CN=Users, DC=CoolDomain, DC=Domains, DC=com"

This is the correct syntax to add a user with that name, as if I invoke that line by itself, it works. But if I remove write-host the command fails saying "dsadd failed:'DC=Domains,' is an unknown parameter.

Any advice? Thanks in advance.

EDIT: I got my script to work through a workaround, however I dont think it is the best way this script could be run.

I changed the last line and added two more. Instead of writing to the host, I wrote-out to a variable and then put that output on a single line. Then I simply invoked the variable and success! it worked:

$Execute = write-output Dsadd user `"CN=$Name"" DC=$Domain -pwd $User.Password -memberof     `"CN=$Member" CN=Users, DC=$Domain"
$Fields="{0} {1} {2} {3} {4} {5} {6} {7}" -f $Execute
Invoke-Expression $Fields

Is there a better way to do this task with powershell?

1 Answer 1

2

I think your difficulties come from how Powershell parses arguments for legacy commands. Since you have not surrounded your arguments that contain spaces in quotes, Powershell wont assume they are part of the same string. It will pass it as a separate argument.

Try building the command line differently

$CSV = Import-Csv -Delimiter "," -Path "$PWD\TestCSV.csv"

foreach ($user in $CSV) 
{
    $Domain = "DC={0},DC=Domains,DC=com" -f $User.Domain
    $MemberOfDn = """CN={0}, CN=Users,{1}""" -f $user.Title,$domain
    $UserDn = """CN={0} {1},{2}""" -f $User.Firstname, $User.Lastname, $Domain
    Dsadd user $UserDn -pwd $User.Password -memberof $MemberOfDn
}

With this, you build all of the strings you need at the beginning, and piece them together as needed. Also, in double-quoted strings, a "" is the same as an escaped double-quote character, whichever you prefer for yourself. You can either use the -f format operator like I did, or you can interpolate variables like directly like in your original example, but I find that the format operator helps with overall readability, especially if other people have to maintain/change your scripts.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, That makes a lot more sense. I never knew the format operator existed.
If you are using -f to format the strings, why not use single quotes and avoid the extra "" escaping? E.g. '"CN={0}, CN=Users,{1}"' instead of """CN={0}, CN=Users,{1}""".

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.