Loop, Split, $ Create Accounts?

by Shaun_Ketterman at 2013-04-29 00:44:40

Greetings Again,

With that out of the way, as you may guess, I am stumped yet again on another assignment. I’ve gotten 95% of it done, but the final section is too much for me to wrap my brain around. Here is the last section of my code, and the #notes show what I need to do with the script:

[code2=powershell]# Create users.txt file (in C:\Scripts)
New-Item C:\users.txt -ItemType File

# Add content to text file (this information was provided via instruction lol)
add-content C:\users.txt "ckent,Clark Kent,1Password3"
add-content C:\users.txt "bwayne,Bruce Wayne,2Password!"

#Open users.txt using default program
.\users.txt

# Set file attributes to Hidden and Archive
(Get-ChildItem C:\users.txt -force) | foreach {
($_.Attributes="Hidden","Archive")
}

# Read users.txt and assign to $file variable
$file = Get-Content C:\users.txt

#Reads the users.txt file assigns it to the variable $file
#Loops through the variable $file using a foreach statement and the dummy variable $user to read each line from the array $file.
#In the script block of the foreach statement do the following:
#Use the split method of the string object $user to split the line at the comma and assign this value to the variable $user_info
#Create a user account with the information in the $user_info array and the net user command
#
#
# Starting it: "foreach ($user in $file)…"
# To split would be: $users -split ","[/code2]

As you can see, I’ve created a text file (users.txt), added some content to it (2 popular fellows), and then created a variable with that content ($file). The last section, to me, seems like it would be a single function. I would split the variable at the <commas>, assign each sub-string to an array (or associative array?), then use the elements of that array and pipe them to the net user function to create 2 user accounts.

What I’m having trouble with (well, besides this whole thing) is when I split that variable at the commas, I get 6 substrings:

PS C:&gt; $users -split ","

ckent
Clark Kent
1Password3
bwayne
Bruce Wayne
2Password!

The instruction seems like it wants me to create only a single account, because "split the line at the comma and assign this value to the variable…" "…create a user account…" Does this make sense to you guys?

Anyways, I can’t seem to find anything that relates to this exact process, nor am I sure that I’m even on the right course of thinking. Basically I’m requesting assistance starting at line 19. As always, guidance is greatly appreciated! Thank you in advance,

Shaun
by eisenbergz at 2013-04-29 01:40:03
What is the goal of the script? AD users or local users? That can make a difference. If it’s AD, look at the cmdlets in the MS AD module or Quest’s AD module. I’d rather use those than use non-Powershell commands if possible. I don’t know that there is another option for local users.

Also, don’t forget that $file,$users,$etc are objects with methods and properties, not just flat variables. That shift in thinking can help open your mind to the possibilities.

.\users.txt doesn’t work (at least for me on v2) - Powershell needs to call the program that will open the file. Try
cmd /c "c:\users.txt" (because cmd.exe knows about app/file associations)
or
& notepad c:\users.txt

Setting file attributes is a little easier with this: set-itemproperty -path c:\users.txt -name Attributes -value "Hidden,Archive"(one command, no loops)

Your $file object is already an array, so it’s easy to process it as such (see below). A hash table might work well, too, depending on what other requirements you have.

Here’s one way to loop through the file taking a line at a time:
foreach ($user in $file) {
$username = $user.split(",")[0]
$fullname = $user.split(",")[1]
$userpwd = $user.split(",")[2]
net user $username $userpwd /FULLNAME:"$fullname" /ADD
}
by RichardSiddaway at 2013-04-29 05:17:47
Instead of .\users.txt

use
invoke-item .\users.txt

to open the file. It will open in the default program on your system for .txt files

As previously asked are you creating AD account or local account? If AD what tools are you using?

For instance the password won’t work if you are using the Microsoft AD cmdlets as they expect a secure string not plain text
by Danoni at 2013-04-29 12:20:30
Hi, new to all this but thought I’d have a go.

Could I ask why you’re creating the input file and adding the content to it through the script? I’d have thought it’d be easier to provide the file in the first place. Apologies if I’ve misunderstood a key reason for doing this.

I’d have thought providing a .csv file like this would have been easier - just typing it out in notepad and saving it as "C:\users.csv" should be fine.
Username,FullName,PlainPassword
ckent,Clark Kent,1Password3
bwayne,Bruce Wayne,2Password!


I tested this next bit by using Write-Host to see if the output was what I thought it’d be. Maybe try this and see what you get.
$importFile = Import-Csv $file
foreach ( $user in $importFile ){
Write-Host "The username: $user.Username"
Write-Host "The full name: $user.FullName"
Write-Host "The plain password: $user.PlainPassword"

Because you defined a header row in the CSV file it allows you to pull out the columns as required.

I’m not on an environment to test AD account creation but I pulled this off the net and it may suit for what you are trying to achieve.

New-ADUser -SamAccountName $user.Username -Name "$user.FullName" -UserPrincipalName $user.Username@dummydomain -AccountPassword (ConvertTo-SecureString -AsPlainText $user.PlainPassword -Force) -Enabled $true -Path 'CN=Users,DC=dummydomain,DC=local
I’ve used similar so I think that should work.

And once more, my apologies if I’ve misread/misunderstood what you are trying to achieve.

Regards,
by Shaun_Ketterman at 2013-04-29 15:36:35
[quote="eisenbergz"]What is the goal of the script? AD users or local users…? [/quote]

It is strictly local. This is just an assignment I’ve got for a class I’m taking, and I was stuck on the very last part.

[quote="eisenbergz"].\users.txt doesn’t work (at least for me on v2) - Powershell needs to call the program that will open the file. Try
cmd /c "c:\users.txt" (because cmd.exe knows about app/file associations)
or& notepad c]

Actually, when I run this script, the file users.txt opens TWICE in Notepad (both times with all the data). I wasn’t really concerned with that, though, so I didn’t mention it. I’m also using v2.0 (or, rather, whichever comes standard with Win 7 x64 Pro)

[quote="eisenbergz"]Your $file object is already an array, so it’s easy to process it as such (see below). A hash table might work well, too, depending on what other requirements you have.

Here’s one way to loop through the file taking a line at a time:
foreach ($user in $file) {
$username = $user.split(",")[0]
$fullname = $user.split(",")[1]
$userpwd = $user.split(",")[2]
net user $username $userpwd /FULLNAME:"$fullname" /ADD
}
[/quote]

This is perfect. Just for my knowledge, would this still work if you didn’t include the element numbers? It’s incredible how each time I make a thread here when I am stumped, I generally get a really good response like this, and when I see the code, I think "duh". It’s obvious how to do things once I see them, but for some reason I can’t figure them out on my own. Practice, practice right?

Accept my sincerest thank you for this!

Shaun
by Shaun_Ketterman at 2013-04-29 15:38:57
[quote="RichardSiddaway"]Instead of .\users.txt

use
invoke-item .\users.txt

to open the file. It will open in the default program on your system for .txt files

As previously asked are you creating AD account or local account? If AD what tools are you using?

For instance the password won’t work if you are using the Microsoft AD cmdlets as they expect a secure string not plain text[/quote]

Hi Richard,

Thank you for the response. I’ve added another response, but no, I’m not using AD whatsoever, this is just a local machine script for a class I’m taking. Also, .txt files are associated with notepad by default (at least on my machine), so the original code I provided DID open the file just fine, it was the last section starting at line 19 in my code that I needed assistance with. Thank you again,

Shaun
by Shaun_Ketterman at 2013-04-29 15:42:49
[quote="Danoni"]Hi, new to all this but thought I’d have a go.

Could I ask why you’re creating the input file and adding the content to it through the script? I’d have thought it’d be easier to provide the file in the first place. Apologies if I’ve misunderstood a key reason for doing this.

I’d have thought providing a .csv file like this would have been easier - just typing it out in notepad and saving it as "C:\users.csv" should be fine.
Username,FullName,PlainPassword
ckent,Clark Kent,1Password3
bwayne,Bruce Wayne,2Password!


I tested this next bit by using Write-Host to see if the output was what I thought it’d be. Maybe try this and see what you get.
$importFile = Import-Csv $file
foreach ( $user in $importFile ){
Write-Host "The username: $user.Username"
Write-Host "The full name: $user.FullName"
Write-Host "The plain password: $user.PlainPassword"

Because you defined a header row in the CSV file it allows you to pull out the columns as required.

I’m not on an environment to test AD account creation but I pulled this off the net and it may suit for what you are trying to achieve.

New-ADUser -SamAccountName $user.Username -Name "$user.FullName" -UserPrincipalName $user.Username@dummydomain -AccountPassword (ConvertTo-SecureString -AsPlainText $user.PlainPassword -Force) -Enabled $true -Path 'CN=Users,DC=dummydomain,DC=local
I’ve used similar so I think that should work.

And once more, my apologies if I’ve misread/misunderstood what you are trying to achieve.

Regards,[/quote]

Hi Danny,

No problem, and thank you for the response. Actually, the reason the script is designed the way it is is because the instructions for the assignment specify to do things exactly like this. I had to create a script with PowerShell that creates the file, adds the information, then uses the contents of the file to create the user accounts in that fashion.

Shaun
by eisenbergz at 2013-05-01 00:04:08
[quote="Shaun_Ketterman"]
[quote="eisenbergz"]Your $file object is already an array, so it’s easy to process it as such (see below). A hash table might work well, too, depending on what other requirements you have.

Here’s one way to loop through the file taking a line at a time:
foreach ($user in $file) {
$username = $user.split(",")[0]
$fullname = $user.split(",")[1]
$userpwd = $user.split(",")[2]
net user $username $userpwd /FULLNAME:"$fullname" /ADD
}
[/quote]

This is perfect. Just for my knowledge, would this still work if you didn’t include the element numbers? [/quote]
Try it and see. Read the help file "help about_split" to understand why. Read the "See Also" entries to know even more. That’s part of the practice that makes you better. :slight_smile: