【问题标题】:Weird new line behavior between Windows 7 Notepad and Windows 2012 Server NotepadWindows 7 记事本和 Windows 2012 Server 记事本之间奇怪的换行行为
【发布时间】:2014-09-27 00:36:01
【问题描述】:

我编写了一个删除旧视频目录的脚本,它会创建一个 .txt 文件中已删除内容的日志。

在我的 Windows 7 PC 上运行脚本,记事本中的 .txt 文件将每个输出放在一个新行上。

在我的 Windows 2012 服务器上运行脚本,记事本中的 .txt 文件将输出放在同一行。如果我用写字板打开日志文件,则输出将在新行上。

powershell 脚本如下所示:

$limit = (Get-Date).AddDays(0)
$logFile = "C:\BodyCam\bodycamlog.txt"
$output = "The following directories contain incorrect video dates" + "`n"

#Throw a timestamp on the log file
Add-Content -Path $logFile "=======$(Get-Date)======="

#Get all of the paths for each camera
$paths = Get-ChildItem -Path "D:\Videos\" |Select-Object -ExpandProperty FullName

#for each path we found
foreach ($pa in $paths) {
    # Delete directories older than the $limit.
    $dir = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } 
    $dir | Remove-Item -Recurse -Force
    $dir | Select -Expand FullName | Out-File $logFile -append
    if ($dir -match "1970"){
        $output += $pa + "`n"
    }
}
Send-MailMessage -to "Ray <***>" -from "BodyCamScript <***>" -subject "Bad Bodycam Date Found" -body $output -SmtpServer ***

这里有什么问题?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    Add-Content 的默认编码是 ASCII,但Out-File 的默认编码是 Unicode。在两者上使用 encoding 参数来强制使用相同的编码,它应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      相关资源
      最近更新 更多