我已编写此代码以在foreach循环中打印A;密码。
$array = "A","B","C","D","E","F","G"
$theDatabaseFile = "C:\Users\saied\Documents\MyWork\test.csv"
foreach($a in $array)
{
$password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | sort {Get-Random})[0..8] -join ''
$final = $a +";"+ $password
$final
}
output
A;pgf5I^4aQ
B;g.62aG9+u
C;7MW<j)x85
D;]2PNv<&Fc
E;9iB^JDyj8
F;|tYV6<Qmd
G;?!B>mwvys
但如果我使用
&#34; | Set-Content $theDatabaseFile -Encoding ascii
&#34;在$final
之后打开我的csv文件。该文件只会占据我的数组中的最后一个。
答案 0 :(得分:1)
这是因为你在循环中重新分配$final
所以它总是设置为当前项目。您必须创建数组并将密码存储在其中:
$passwords = @()
foreach($a in $array)
{
$password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | sort {Get-Random})[0..8] -join ''
$final = $a +";"+ $password
$passwords += $final
$final
}
$passwords | Set-Content $theDatabaseFile -Encoding ascii