简体   繁体   English

如何使用Powershell脚本在日志文件中附加换行文本?

[英]How to append new line text inside a log file using powershell script?

I need to pull logs from the original path in C:\\ to log directory in D:\\Logs but everytime the original path create new log, the script need to append new lines, not replace or rewrite the whole lines. 我需要将日志从C:\\中的原始路径拉到D:\\ Logs中的日志目录,但是每次原始路径创建新日志时,脚本都需要追加新行,而不是替换或重写整行。

I already tried this but i guess this replace the whole file and I'm not sure about the Param things. 我已经尝试过了,但是我想这会替换整个文件,并且我不确定Param的事情。

$SourceFolder       = "C:\ProgramData\Sophos\Sophos Anti-Virus\logs"
$DestinationFolder  = "D:\Logs\SophosAntivirus"

Function ChangeTabToSpace 
{
    Param(
        [string] $OldFile = "",
        [string] $NewFile = ""
    )
    $OldText = (Get-Content $OldFile -Raw)
    #Change all tabt \t to space 
    $NewText = ($OldText -replace "`t"," ")
    #Delete the last empty line
    if ($NewText.Length -ge 2) {
        $NewText = $NewText.Substring(0,$NewText.Length-2)
    }
    if (!(Test-path "$NewFile")) {
        New-Item -type file "$NewFile" -force | Out-Null
    }
    #Write-Output $NewText | Out-File -Encoding utf8 "$NewFile"
    [System.IO.File]::WriteAllLines($NewFile, $NewText)
}

If its a simple text file you can use the following 如果它是一个简单的文本文件,则可以使用以下内容

"the string you want or have" | out-file -path $path -append

This will add the string to a new line at the end of the file. 这会将字符串添加到文件末尾的新行中。

You don't need to pipe the input in like I did... its just how I learned to use it and just kept using it. 您不需要像我一样输入输入内容……这只是我学会使用并一直使用的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Powershell中附加到日志文件? - How to append to a log file in powershell? Powershell将字符串附加到文本会创建一个新行 - Powershell append string to text creates a new line 将Powershell脚本行附加到文件.bat中 - Append powershell script line into file .bat 如何使用 powershell 脚本添加新的 3 行以响应 nginx.conf 文件中的特定 3 行 - How to add new 3 line in response to a specific 3 line in nginx.conf file using powershell script 如何使用 PowerShell 脚本删除文本/日志文件的内容,直到第一次出现关键字 - How do I delete the content of a text / Log file until the first occurrence occurrence of the keyword using PowerShell script 如何在PowerShell中将-Verbose的输出附加到日志文件? - How to append the output of -Verbose to the log file in PowerShell? 使用Powershell将结果或文本附加到文本文件 - Append results or text to text file by using Powershell 如何使用 powershell 脚本在文本文件的行首和行尾添加特殊字符? - How to add special character at the beginning and end of a line in a text file using a powershell script? 如何在Powershell脚本中匹配文本文件内容的每一行 - How to match each line of a text file contents in powershell script 如何通过 powershell 脚本编辑文本文件中的行 - How to edit line in text file via powershell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM