我创建了一个随机密码生成器,我需要将所有10个输出输出到一个.txt文件中
但是我现在只有1行输出。
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))
Write-Host $pwd
Out-File .\Documents\L8_userpasswords.txt -InputObject $pwd
}
当我打开.txt文件时,我只看到输出的1行而不是10行。
默认情况下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++){
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句