2014-03-13 36 views
0

試圖製作包含2個類別的哈希表:用戶和密碼。 這是我迄今爲止的代碼,但問題是輸出只顯示命令並且不執行它。從gc到哈希表的輸出值

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

$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 

這是我得到的輸出:

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 

回答

1

我想你想這一點,這會變成用戶名和密碼的列表爲HashTable,然後將其轉換爲PSCustomObject,這將具有兩個屬性:UsersPasswords

$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; 
+0

我想這一點,但現在我只輸出指出: '名稱值 ---- ----- Value {dV7a @ H,wT9x + U,bU3y @ Y,vC9s&R ...} Name {Mike Baker,Mary Martin,Uma Siveram,Uri Demisky ...} – Musa

+0

@TazB。請使用該信息更新您的問題。這不是所有可讀的。 –

0

或者哎,你很可能只是有一個內襯更換整個腳本:

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

除非你是超級連接到您的密碼生成循環即是。 [System.Web.Security.Membership]::GeneratePassword(X,Y)將生成複雜的密碼,其中X是長度,Y是特殊字符的數量(其餘將是大寫字母,小寫字母和數字的隨機組合)。所以在我的代碼(10,3)中是一個包含3個非字母數字字符的10個字符的密碼。

你想把它保存到一個文件?管到Export-CSV。或者通過在$UserList = <code>之前加上前綴來將它分配給一個變量。

或者,如果你真的想要一個哈希表,你可以做一個空的,然後改變它只是一點點,以每對添加到表是這樣的:

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

假設L8_users.txtL8_userpasswords.txt包含相同數量的項目,你可以做這樣的事情:

$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] 
}