Skip to content

Instantly share code, notes, and snippets.

@PCfromDCSnippets
Created December 15, 2017 20:30
Show Gist options
  • Save PCfromDCSnippets/cae067bdec7c2e09950fefd5a641a3ab to your computer and use it in GitHub Desktop.
Save PCfromDCSnippets/cae067bdec7c2e09950fefd5a641a3ab to your computer and use it in GitHub Desktop.
Update Log File
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