powershell - 我不能 'Out-File' 我的整个循环只有一行

标签 powershell powershell-ise

我创建了一个随 secret 码生成器,我需要将所有 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++){
...

关于powershell - 我不能 'Out-File' 我的整个循环只有一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382672/

相关文章:

powershell - 如何将管道转换为CSV格式并指定UTF-8编码

powershell - 在 Powershell 中工作时,如何在列表项之间暂停?

visual-studio - 如何使用 PowerShell 添加许可证文件作为自定义 NuGet 包的一部分

powershell - $MyInvocation.MyCommand.Path 返回 NULL

powershell - 如何使用 PowerShell 在 Azure 存储表中获取行?

.net - Docker容器未运行Windows系统调用错误

powershell - 如何在 ISE 中输入多行命令?

powershell - Active Directory路径中的字符串变量不起作用

powershell - 检查服务是否正在运行或停止并相应输出

powershell - 使用 Powershell GUI 的实时数据