我在PowerShell v2上的系统上。
我有一个删除旧备份文件的脚本。我想保存脚本的结果,因为我在屏幕上看到它被写入文件,以便我可以查看所有文件被删除的内容。
我读到我在屏幕上看到的Write-Host
输出无法重定向到文件,所以我也尝试使用Write-Output
,但可以将结果写入文件。文件已创建,但它是空的。
我在这里做错了什么?
以下是代码:
$Stamp1 = Get-Date -Format "MMddyyyy_hhmmss"
$File= "E:\SSBackups\PS_Delete_Script_logs\Delete_Backup_Results_$Stamp1.txt"
$ReadFile = Get-Content C:\Users\MSA050415\Desktop\List.txt
foreach ($Folder in $ReadFile) {
#Write-Host "Folder currently being worked on: $Folder" -ForegroundColor Magenta
Write-Output "Folder currently being worked on: $Folder"
$Path ="E:\SSBackups\Database_Backups\$Folder"
#Write-Host "The Path is set to $Path" -ForegroundColor Green
Write-Output "The Path is set to $Path"
Get-ChildItem $Path |
Where-Object {$_.LastWriteTime -lt "4/17/2018 12:15:00 AM"} |
Remove-Item -Verbose |
Out-File $File
}
答案 0 :(得分:1)
详细输出写入verbose output stream,无法在PowerShell v2中重定向。该功能已在PowerShell v3中添加。
你应该能够录制成绩单,但是:
Start-Transcript $File -Append
Get-ChildItem $Path |
Where-Object {$_.LastWriteTime -lt "4/17/2018 12:15:00 AM"} |
Remove-Item -Verbose
Stop-Transcript