-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add synopsis and script desctiption
- Loading branch information
Ivica Agatunovic
committed
Jul 26, 2024
1 parent
8c52118
commit 5ba07f1
Showing
9 changed files
with
365 additions
and
106 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,68 @@ | ||
#Cleanup .json and .mof files present in the C:\Windows\System32\Configuration\ConfigurationStatus | ||
$cleanuppath = "C:\Windows\System32\Configuration\ConfigurationStatus" | ||
$historyindays = 10 | ||
$date = ([System.DateTime]::Now).AddDays(-$historyindays) | ||
$eventidstart = 777 | ||
$eventidend = 888 | ||
$eventiderror = 999 | ||
<# | ||
.SYNOPSIS | ||
Cleans up .json and .mof files in the specified directory older than a specified number of days. | ||
New-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -ErrorAction SilentlyContinue | ||
Write-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -EntryType Information -EventID $eventidstart -Message "DSC Files Cleanup Started" | ||
.DESCRIPTION | ||
This function performs the following tasks: | ||
1. Initializes event logging. | ||
2. Logs the start of the cleanup process. | ||
3. Deletes .json and .mof files older than the specified number of days in the given directory. | ||
4. Logs the completion of the cleanup process. | ||
5. Handles exceptions and logs any errors encountered during the process. | ||
.PARAMETER CleanupPath | ||
The path to the directory where the cleanup will be performed. Default is "C:\Windows\System32\Configuration\ConfigurationStatus". | ||
.PARAMETER HistoryInDays | ||
The number of days to retain files. Files older than this will be deleted. Default is 10 days. | ||
.PARAMETER EventIDStart | ||
The event ID used to log the start of the cleanup process. Default is 777. | ||
.PARAMETER EventIDEnd | ||
The event ID used to log the successful completion of the cleanup process. Default is 888. | ||
.PARAMETER EventIDError | ||
The event ID used to log any errors encountered during the cleanup process. Default is 999. | ||
.EXAMPLE | ||
Cleanup-DSCFiles -CleanupPath "C:\Windows\System32\Configuration\ConfigurationStatus" -HistoryInDays 10 | ||
.NOTES | ||
Author : Ivica Agatunovic | ||
WebSite: https://github.com/ivicaagatunovic | ||
Linkedin: www.linkedin.com/in/ivica-agatunovic-96090024 | ||
- Ensure the script is run with appropriate permissions to delete files in the specified directory. | ||
- The script logs events in the Application log with the source 'DSC_CLEANUP'. | ||
#> | ||
|
||
function Cleanup-DSCFiles { | ||
[CmdletBinding()] | ||
param ( | ||
[string]$CleanupPath = "C:\Windows\System32\Configuration\ConfigurationStatus", | ||
[int]$HistoryInDays = 10, | ||
[int]$EventIDStart = 777, | ||
[int]$EventIDEnd = 888, | ||
[int]$EventIDError = 999 | ||
) | ||
|
||
$date = ([System.DateTime]::Now).AddDays(-$HistoryInDays) | ||
|
||
New-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -ErrorAction SilentlyContinue | ||
Write-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -EntryType Information -EventID $EventIDStart -Message "DSC Files Cleanup Started" | ||
|
||
try { | ||
foreach ($mof in ([System.IO.Directory]::EnumerateFiles("C:\Windows\System32\Configuration\ConfigurationStatus", "*.*", "AllDirectories").Where({[System.IO.File]::GetLastWriteTime($_) -le $date}))) | ||
{ [System.IO.File]::Delete($mof) } | ||
Write-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -EntryType Information -EventID $eventidend -Message "DSC Files Cleanup Finished" | ||
foreach ($file in ([System.IO.Directory]::EnumerateFiles($CleanupPath, "*.*", "AllDirectories").Where({[System.IO.File]::GetLastWriteTime($_) -le $date}))) { | ||
[System.IO.File]::Delete($file) | ||
} | ||
Write-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -EntryType Information -EventID $EventIDEnd -Message "DSC Files Cleanup Finished" | ||
} | ||
|
||
catch { | ||
Throw "-=== Something went wrong [$($Error[0])] ===-" | ||
Write-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -EntryType Information -EventID $eventiderror -Message "DSC Files Cleanup Failed due to: [$($Error[0])]!!!" | ||
Write-EventLog -LogName 'Application' -Source 'DSC_CLEANUP' -EntryType Error -EventID $EventIDError -Message "DSC Files Cleanup Failed due to: [$($_.Exception.Message)]" | ||
Throw "-=== Something went wrong [$($_.Exception.Message)] ===-" | ||
} | ||
} | ||
|
||
# Example usage of the function | ||
# Cleanup-DSCFiles -CleanupPath "C:\Windows\System32\Configuration\ConfigurationStatus" -HistoryInDays 10 |
Oops, something went wrong.