2014-03-13 24 views
3

我创建了一个随机密码生成器,我要出文件中的所有10个输出到一个txt文件我不能“出文件”我的整个循环只有一行

但是我现在只有1行输出。

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)) 
Write-Host $pwd 

Out-File .\Documents\L8_userpasswords.txt -InputObject $pwd 

} 

当我打开的.txt我只看到1线的输出的,而不是10

回答

5

默认Out-File破坏(重写)在指定的路径(如果存在)。如果该文件不存在事先脚本执行,使用-Append追加到文件:

Out-File .\Documents\L8_userpasswords.txt -InputObject $pwd -Append 

注意,这将在每次脚本运行时追加到文件。如果你想要的文件每次重新创建,检查是否存在并进入for循环之前将其删除:

$file = ".\L8_userpasswords.txt" 
if (Test-Path -Path $file -PathType Leaf) { 
    Remove-Item $file 
} 
for ($i=1; $i -le 10; $i++){ 
... 
1

您需要使用-Append参数为Out-File cmdlet的,因为默认情况下它会覆盖指定的文件。

Out-File .\Documents\L8_userpasswords.txt -InputObject $pwd -Append; 
相关问题