在循环中向文件添加文本(Set-Content,Add-Content,Out-File?)

时间:2017-05-04 08:23:48

标签: powershell

我正在尝试将内容添加到.txt文件中,但是当我使用Out-File时,结果会被覆盖,而当我使用Add-ContentSet-Content格式时,结果会被更改。

foreach ($item in (Get-ChildItem .\Upload)) {
    Write-Host -Object "Uploading $item..."
    $Send = Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Overwrite -ErrorAction SilentlyContinue #>> .\up.txt #.\Upload\test.txt
    if ($Send.Name -eq $item.Name) {
        $Rec = "OK"
    } else {
        $Rec = "!!!-FAIL-!!!"
    }
    $array = $line, $item, $connect, $Rec
    $FailTable = New-Object -TypeName PSObject -Property ([ordered]@{
        "FTP Server" = $array[0];
        "File"       = $array[1];
        "Connected"  = $array[2];
        "Uploaded"   = $array[3]
    })
    $FailTable >> .\stats.txt
    #$FailTable | Set-Content .\stats.txt
    #Write-Output $FailTable | Add-Content .\stats.txt
    #Add-Content .\stats.txt $FailTable
    $FailTable | Out-File '.\stats.txt' #-NoClobber
}

工作效果Out-File

FTP Server   File     Connected Uploaded
----------   ----     --------- --------
10.80.59.170 test.txt OK        OK      

以及工作Set-ContentAdd-Content的结果:

@{FTP Server=10.80.59.170; File=Config.xml; Connected=OK; Uploaded=OK}
@{FTP Server=10.80.59.170; File=test.txt; Connected=OK; Uploaded=OK}

0 个答案:

没有答案