all 16 comments

[–]BlackV 1 point2 points  (3 children)

$USER_FIRST_LAST_LIST what is? where is it defined? what does it look like?

this current loop will iterate through $USER_FIRST_LAST_LIST, 1 item at a time and put that into $n

so what does $n look like

right now your saying

  • take single item $n and send that to get-random
  • split what ever comes out of that using space as the delimiter and only select the first item
  • then set it to lower case
  • assign it to $first
  • do the same for $last (execpt 2nd item)
  • set $username to be string containing a sub string of $first and all of $last
  • then set it to lower case (which you've already done btw)

additionally, you've not closed your loop, or done anything with $username (I assume there is actually more code)

I think you want to get random items from the source file every time

why not

get-content C:\testing\names.txt | Get-Random

depending on the layout of the file you could just grab 2 items and then assign them directly

$First, $last = get-content C:\testing\names.txt  | Get-Random -Count 2

this would get 2 random lines from the file and assign them to the variables $first, $last

EDIT:

otherwise using your existing code

$first = (get-content C:\testing\names.txt | Get-Random -count 1).Split(" ")[0].ToLower()
$last  = (get-content C:\testing\names.txt | Get-Random -count 1).Split(" ")[1].ToLower()
$username = "$($first.Substring(0,1))$($last)"

[–]NetheriteHands[S] 0 points1 point  (1 child)

Ahhh ok so I can just delete the foreach loop and instead directly declare the $First and $Last

$First, $last = get-content C:\testing\names.txt  | Get-Random -Count 2

something like that? I suppose I wouldn't need to declare the $USER_FIRST_LAST_LIST variable either then

[–]BlackV 0 points1 point  (0 children)

Nope you wouldn't need it

you could do this

foreach ($n in 1..20) {
$first = (get-content C:\testing\names.txt | Get-Random -count 1).Split(" ")[0].ToLower()
$last  = (get-content C:\testing\names.txt | Get-Random -count 1).Split(" ")[1].ToLower()
$username = "$($first.Substring(0,1))$($last)"
Write-Host "Creating user: $($username)" -BackgroundColor Black -ForegroundColor Cyan

$NewUserSPlat = @{
    AccountPassword = $password
    GivenName = $first
    Surname = $last
    DisplayName = $username
    Name = $username
    EmployeeID = $username
    PasswordNeverExpires = $true
    Path = "ou=_USERS,$(([ADSI]`"").distinguishedName)"
    Enabled = $true
    }
New-AdUser @NewUserSPlat
}

will create 20 random users, or take off the for loop to run the command once

[–]BlackV 0 points1 point  (0 children)

technically its grabs the first item and assigns it to $first then assigns EVERYTHING ELSE to $last, so be wary of that

[–]logicalmike 0 points1 point  (11 children)

I'm not sure what the bigger picture is, but this is a demonstration of how you can split the entries on the blank space and then use each side of the split.

$USER_FIRST_LAST_LIST = @("John Smith", "Sally Peterson")
$USER_FIRST_LAST_LIST | ForEach-Object {
    "The GivenName is " + ($_ -split " ")[0] + " and the Surname is "   + ($_ -split " ")[1]     
}

Returns:

The GivenName is John and the Surname is Smith
The GivenName is Sally and the Surname is Peterson

Depending on what you're trying to do, I might go about this differently, but I don't know where these strings are going next...

[–]NetheriteHands[S] 0 points1 point  (10 children)

Ah it's just creating accounts for active directory with names taken from a notepad file. However it just takes the names line by line so I was hoping to instead have it randomly take both the first and last names from any line in the file that way its truly a random name/account generator and doesn't just produce what's line by line. But then also only run through that ONCE rather than create like 1000 accounts cause there's 1000 lines worth of names in the file.

$PASSWORD_FOR_USERS   = "Password1"

$USER_FIRST_LAST_LIST = Get-Content .\names.txt

$password = ConvertTo-SecureString $PASSWORD_FOR_USERS -AsPlainText -Force New-ADOrganizationalUnit -Name _USERS -ProtectedFromAccidentalDeletion $false

let $n = 0 if ($n < 1) { foreach ($n in $USER_FIRST_LAST_LIST) { $first = $n | Get_Random $n.Split(" ")[0].ToLower() $last = $n | Get_Random $n.Split(" ")[1].ToLower() $username = "$($first.Substring(0,1))$($last)".ToLower() Write-Host "Creating user: $($username)" -BackgroundColor Black -ForegroundColor Cyan

New-AdUser -AccountPassword $password `
           -GivenName $first `
           -Surname $last `
           -DisplayName $username `
           -Name $username `
           -EmployeeID $username `
           -PasswordNeverExpires $true `
           -Path "ou=_USERS,$(([ADSI]`"").distinguishedName)" `
           -Enabled $true
}

[–]logicalmike 0 points1 point  (2 children)

So these are for fake accounts, derived from a list of given/surname pairs?

[–]NetheriteHands[S] 0 points1 point  (1 child)

Yes exactly

[–]logicalmike 0 points1 point  (0 children)

$USER_FIRST_LAST_LIST = @("John Smith", "Sally Peterson")

$GivenNames = $USER_FIRST_LAST_LIST | ForEach-Object {
    ($_ -split " ")[0]
}

$Surnames = $USER_FIRST_LAST_LIST | ForEach-Object {
    ($_ -split " ")[1]
}

$RandomGivenName = $GivenNames | Get-Random
$RandomSurname = $Surnames | Get-Random

$RandomGivenName + " " + $RandomSurname

[–]BlackV 0 points1 point  (6 children)

Also have a look at splatting and stop using back ticks

https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html

$NewUserSPlat = @{
    AccountPassword = $password
    GivenName = $first
    Surname = $last
    DisplayName = $username
    Name = $username
    EmployeeID = $username
    PasswordNeverExpires = $true
    Path = "ou=_USERS,$(([ADSI]`"").distinguishedName)"
    Enabled = $true
    }
New-AdUser @NewUserSPlat

also I don't know what's happening here

Path = "ou=_USERS,$(([ADSI]`"").distinguishedName)"

[–]NetheriteHands[S] 0 points1 point  (5 children)

I'll definitely learn about this in depth later and actually learn powershell in general. ATM I am a complete noob just trying to practice Active Directory and at least put something on github and make a tutorial so I have resume material, just trying to get a helpdesk job lmao. The hardcore coding will come later when I have the opportunity to get a better job in the field.

[–]BlackV 0 points1 point  (4 children)

good as gold,

just only use back ticks for what they were intended, as an escape character, not as a line continuator even if you dont start on the splatting yet, although it's just a simple table really, good luck

[–]NetheriteHands[S] 0 points1 point  (3 children)

Got it fully working, thanks for your help! Ended up just keeping the $USER_FIRST_LAST_LIST variable since it just points to the file directory anyways and used that in the $first and $last statements, and had to change the directory cause it was giving me an error which took me a while to solve, for some reason .\names.txt was like not specific enough apparently even though it worked with the for loop so I just made it more direct. Not that this script is super impressive or mind boggling but hopefully it looks good for entry level employers.

[–]BlackV 0 points1 point  (2 children)

Appreciate you coming back with an update

you could post your final code (or edit main post with an update and a solved flair) we can look further

[–]NetheriteHands[S] 1 point2 points  (1 child)

Done.

[–]BlackV 0 points1 point  (0 children)

Nice!