0

Trying to make a hash table with 2 categories: Users and Passwords. This is my code thus far but the issue is the output only displays the command and does not execute it.

for ($i=1; $i -le 10; $i++){
$caps = [char[]] "ABCDEFGHJKMNPQRSTUVWXY"
$lows = [char[]] "abcdefghjkmnpqrstuvwxy" 
$nums = [char[]] "2346789"
$spl = [char[]] "!@#$%^&*?+"

$first = $lows | Get-Random -count 1;
$second = $caps | Get-Random -count 1;
$third = $nums | Get-Random -count 1;
$forth = $lows | Get-Random -count 1;
$fifth = $spl | Get-Random -count 1;
$sixth = $caps | Get-Random -count 1;

$pwd = [string](@($first) + @($second) + @($third) + @($forth) + @($fifth) + @($sixth))

Out-File C:\Users\Administrator\Documents\L8_userpasswords.txt -InputObject $pwd -Append

}

$here = @'
$users=Get-Content C:\\Users\\Administrator\\Desktop\\L8_users.txt
$passwords=Get-Content C:\\Users\\Administrator\\Documents\\L8_userpasswords.txt
'@
convertfrom-stringdata -stringdata $here

This is the output I am getting:

PS C:\Users\Administrator> C:\Users\Administrator\Documents\l8.ps1

Name            Value
----            -----
$users          Get-Content C:\Users\Administrator\Desktop\Lab8_users.txt
$passwords      Get-Content C:\Users\Administrator\Documents\L8_userpasswords.txt
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Musa
  • 553
  • 1
  • 7
  • 19

3 Answers3

1

I think you want this, which will turn the list of users and passwords into a HashTable, and then cast it to a PSCustomObject, which will have two properties: Users and Passwords.

$Data = [PSCustomObject]@{
    Users = Get-Content -Path C:\Users\Administrator\Desktop\L8_users.txt;
    Passwords = Get-Content -Path C:\Users\Administrator\Desktop\L8_userpasswords.txt;
}
$Data;
  • I am trying this but now my output just states: `Name Value ---- ----- Value {dV7a@H, wT9x+U, bU3y@Y, vC9s&R...} Name {Mike Baker, Mary Martin, Uma Siveram, Uri Demisky...}` – Musa Mar 13 '14 at 18:45
  • @TazB. Please update your question with that information. It's not all that readable like this. – Ansgar Wiechers Mar 13 '14 at 20:10
0

Or hey, you could probably just replace the entire script with a one liner:

GC C:\Users\Administrator\Desktop\L8_users.txt|%{[PSCustomObject]@{User=$_;Password=[System.Web.Security.Membership]::GeneratePassword(10,3)}}

Unless you are super attached to your password generation loop that is. [System.Web.Security.Membership]::GeneratePassword(X,Y) will generate complex passwords where X is the length and Y is the number of special characters (the rest will be a random mix of upper case letters, lower case letters, and numbers). So in my code (10,3) is a 10 character password with 3 non-alphanumeric characters.

You want it saved to a file? Pipe that to Export-CSV. Or assign it to a variable by prefixing it with something like $UserList = <code>.

Or if you really, really want a Hash Table you could make an empty one and then alter it just a little to add each pair to the table like this:

$UserList = @{}
GC C:\Users\Administrator\Desktop\L8_users.txt|%{$UserList.add($_,[System.Web.Security.Membership]::GeneratePassword(10,3))}
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
0

Assuming that L8_users.txt and L8_userpasswords.txt contain the same number of items, you could do something like this:

$users     = Get-Content 'C:\Users\Administrator\Desktop\L8_users.txt'
$passwords = Get-Content 'C:\Users\Administrator\Documents\L8_userpasswords.txt'

$userpasswords = @{}
for ($i = 0; i -lt $users.Length; $i++) {
  $userpasswords[$users[$i]] = $passwords[$i]
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328