<#
===========================================================================================
| |
| .NET Code |
| |
===========================================================================================
#>
$netCode = @'
Option Strict On
Option Explicit On
Option Infer Off
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Public Class FileUtils
Public Shared Function FormatFileSize(fileSize As Long) As String
Dim buffer As New StringBuilder(16)
NativeMethods.StrFormatByteSize(fileSize, buffer, buffer.MaxCapacity)
Return buffer.ToString()
End Function
End Class
Friend NotInheritable Class NativeMethods
<DllImport("Shlwapi.dll", CharSet:=CharSet.Auto)>
Friend Shared Function StrFormatByteSize(fileSize As Long,
<MarshalAs(UnmanagedType.LPTStr)> buffer As StringBuilder,
bufferSize As Integer) As Long
End Function
End Class
'@
$netType = Add-Type -TypeDefinition $netCode `
-CodeDomProvider (New-Object Microsoft.VisualBasic.VBCodeProvider) `
-PassThru `
-ReferencedAssemblies "System.dll" `
| where { $_.IsPublic }
<#
===========================================================================================
| |
| Functions |
| |
===========================================================================================
#>
function Show-WelcomeScreen {
Clear-Host
Write-Output ""
Write-Output " $($host.ui.RawUI.WindowTitle)"
Write-Output " +==========================================================+"
Write-Output " | |"
Write-Output " | This script will search for log files (*.log) inside the |"
Write-Output " | current working directory (including subdirectories) to |"
Write-Output " | truncate them by setting their length to zero. |"
Write-Output " | |"
Write-Output " | This may be useful to help reduce the size of a system |"
Write-Output " | full of heavy sized log files. |"
Write-Output " | |"
Write-Output " +==========================================================+"
Write-Output ""
}
function Confirm-Continue {
Write-Output " Press 'Y' key to continue or 'N' to exit."
Write-Output ""
Write-Output " -Continue? (Y/N)"
do {
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$char = $key.Character.ToString().ToUpper()
if ($char -ne "Y" -and $char -ne "N") {
[console]::beep(1500, 500)
}
} while ($char -ne "Y" -and $char -ne "N")
if ($char -eq "N") {Exit(1)} else {Clear-Host}
}
function Truncate-LogFiles {
Write-Output "Fetching log files in ""$($PWD)"", please wait..."
Write-Output ""
$logFiles = Get-ChildItem -Path $PWD -File -Filter "*.log" -Recurse -Force -ErrorAction SilentlyContinue
if (-not $logFiles) {
Write-Warning "No log files found in directory: $($PWD)"
Return
}
Clear-Host
foreach ($logFile in $logFiles) {
if ($logFile.Length -eq 0) {
Continue
}
$formattedFileSize = [FileUtils]::FormatFileSize($logFile.Length)
Write-Output "Truncating file: $($LogFile.FullName) ($($formattedFileSize)) ..."
try {
# [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($logFile.FullName, [Microsoft.VisualBasic.FileIO.UIOption]::OnlyErrorDialogs, [Microsoft.VisualBasic.FileIO.RecycleOption]::SendToRecycleBin)
$fs = [System.IO.File]::OpenWrite($logFile.FullName)
$fs.SetLength(0)
$fs.Close()
}
catch {
Write-Host "Access denied to file, it may be in use." -ForegroundColor Yellow
}
Start-Sleep -MilliSeconds 50
}
}
function Show-GoodbyeScreen {
Write-Output ""
Write-Host "Operation Completed." -BackgroundColor Black -ForegroundColor Green
Write-Output ""
Write-Output "Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(0)
}
<#
===========================================================================================
| |
| Main |
| |
===========================================================================================
#>
[System.Console]::Title = "Truncate Log Files - by Elektro"
[CultureInfo]::CurrentUICulture = "en-US"
try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
Show-WelcomeScreen
Confirm-Continue
Truncate-LogFiles
Show-GoodbyeScreen