-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClearLogs.ps1
140 lines (136 loc) · 5.06 KB
/
ClearLogs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<#
.SYNOPSIS
Clearlogs (Clear Windows Logs)
Created By: Dana L. Meli-Wischman
Created Date: August, 2018
Last Modified Date: July 27, 2023
.DESCRIPTION
This is a simple script to clear all your windows logs. (That are not in use.)
You Can run it in three ways. No Parameter, -Loud and -Silent
Normal has a progress bar. Loud shows each log processed and silent just shows results.
Command Line: ClearLogs.ps1 [[-loud] <bool>] [[-Silent] <bool>]
.EXAMPLE
ClearLogs (Normal operation)
ClearLogs -loud 1
ClearLogs -Silent 1
Clearlogs -Hidden 1
.NOTES
Still under development.
#>
Param([bool]$loud, [bool]$Silent, [bool]$Hidden)
$HoldError = "$ErrorActionPreference"
$ErrorActionPreference = "SilentlyContinue"
$FileVersion = "0.2.25"
$host.ui.RawUI.WindowTitle = "Clear Windows Logs $FileVersion"
<# Test and if needed run as admin #>
Function Test-Administrator {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
if (!($principal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))) {
Start-Process "pwsh.exe" -ArgumentList ($env:BASE + "\ClearLogs.ps1") -Verb RunAs
return
}
if (($Hidden)) {
Say "Running Clearlogs in hidden mode."
wevtutil el | ForEach-Object { wevtutil cl "$_" }
Say "Clearlogs complete"
return
}
if (!($WinWidth)) {
[int]$WinWidth = 70
[int]$BuffWidth = $WinWidth
}
if (!($WinHeight)) {
[int]$WinHeight = 25
[int]$BuffHeight = $WinHeight
}
Function FlexWindow {
$pshost = Get-Host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = $BuffHeight
$newsize.width = $BuffWidth
$pswindow.buffersize = $newsize
$newsize = $pswindow.windowsize
$newsize.height = $WinHeight
$newsize.width = $WinWidth
$pswindow.windowsize = $newsize
}
FlexWindow
Clear-Host
$PShost = Get-Host
$PSWin = $PShost.ui.rawui
$PSWin.CursorSize = 0
FlexWindow
[Console]::SetCursorPosition(0, 3); Say "Running ClearLogs $FileVersion"
$ClearSet = 0
FlexWindow
& wevtutil.exe el | ForEach-Object { $ClearSet++ }
[Console]::SetCursorPosition(0, 4); Say "Located $ClearSet logs. Setting Up the math."
$ClearSet = ($ClearSet / 100)
$i = 0
if (($loud)) {
[Console]::SetCursorPosition(0, 5); Say "Hold on a minute, running Clearlogs..."
wevtutil.exe el | ForEach-Object {
Say "Deleting: " $_
wevtutil.exe cl $_
$i++
}
Say -NoNewline "ClearLogs Processed $i log files."
Read-Host -Prompt "[Slap Enter To Exit]"
return
}
elseif (($Silent)) {
[Console]::SetCursorPosition(0, 5); Say "Hold on a minute, running Clearlogs..."
wevtutil.exe el | ForEach-Object {
wevtutil.exe cl $_
$i++
}
Say -NoNewline "ClearLogs Processed $i log files."
Read-Host -Prompt "[Slap Enter To Exit]"
return
}
else {
FlexWindow
$ec = 0
$OrgError = $ErrorActionPreference
$ErrorActionPreference = "Ignore"
Try {
& wevtutil.exe el | ForEach-Object {
Try { & wevtutil.exe cl "$_" }
catch { Continue }
if ($LastExitCode -ne 0) { $ec++ }
$i++
$p = ($i / $ClearSet); $p = [int][Math]::Ceiling($p)
$j = ($p / 2); $j = [int][Math]::Floor($j)
[Console]::SetCursorPosition(16, 6); Say -NoNewline " "
[Console]::SetCursorPosition(0, 6); Say -NoNewline "Delete count is: $i"
[Console]::SetCursorPosition(16, 7); Say -NoNewline " "
[Console]::SetCursorPosition(0, 7); Say -NoNewline "PercentComplete: $p%"
if ($p -eq ($h + 2)) { $h = $p }; $it = "#" * $j
if (($p % 2) -eq 1 -and $h -le $p) { $tip = "="; $it = ($it + $tip) }
if (($p % 2) -eq 0 -and $h -gt $p) { $tip = "#"; $it = ($it + $tip) }
[Console]::SetCursorPosition(0, 9); Say -NoNewline "[$it"
[Console]::SetCursorPosition(51, 9); Say -NoNewline "] "
[Console]::SetCursorPosition(0, 8); Say -NoNewline " "
[Console]::SetCursorPosition(0, 8); Say -NoNewLine ""
}
}
Catch { Continue }
if ($LastExitCode -ne 0) { $ec++ }
$ErrorActionPreference = $OrgError
$ans = ($i - $ec)
Asay.ps1 ClearLogs Cleared $ans Log Files.
$PShost = Get-Host
$PSWin = $PShost.ui.rawui
$PSWin.CursorSize = 25
$ErrorActionPreference = "$HoldError"
[Console]::SetCursorPosition(0, 11); Say -NoNewLine "Logs Located:" $i
[Console]::SetCursorPosition(0, 12); Say -NoNewLine "Logs In Use :" $ec
[Console]::SetCursorPosition(0, 14); Say -NoNewLine "ClearLogs Cleared $ans logs."
[Console]::SetCursorPosition(0, 15); Read-Host -Prompt "[Slap Enter To Exit]"
return
}