[英]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.