Created
December 15, 2017 20:30
-
-
Save PCfromDCSnippets/cae067bdec7c2e09950fefd5a641a3ab to your computer and use it in GitHub Desktop.
Update Log File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Update-Logs ($content) { | |
$logPath = 'C:\S2S Logs' | |
if (-not (Test-Path -Path $logPath)) {New-Item -Path $logPath -ItemType Directory} | |
$date = Get-Date | |
$lastMonth = $date.AddMonths(-1) | |
$fileName = $date.ToString("yyyy-MM-dd") + "- S2S Log.txt" | |
$filePath = ($logPath + "\" + $fileName) | |
$exists = Test-Path $filePath | |
if ($exists) { | |
$string = (Get-Date).ToShortTimeString().ToString() + " $content" | |
$string | Out-File -FilePath $filePath -Append | |
} | |
if (-not $exists) { | |
$string | Out-File -FilePath $filePath | |
} | |
# Clean Up Logs Older than 1 month | |
$items = Get-ChildItem -Path $logPath -Recurse -Filter *.txt | Where-Object {$_.CreationTime.Date -lt $lastMonth} | |
$items | Remove-Item -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment