54 lines
2.5 KiB
PowerShell
54 lines
2.5 KiB
PowerShell
<#
|
|
Script: MDL-NotProcessed-Cleanup.ps1
|
|
Emil Holgersen, Thales, 2021-04-21
|
|
This script deletes old subfolders from Incoming_notprocessed_files
|
|
The script is meant to run as a daily scheduled job on the MDL servers, and it has to be run with an account that has "full access" to the folder in question.
|
|
Note that this script was made as a response to the fact that no such feature existed after platform reinstall with version 10.2. I have no idea how this was folder was kept from eating the disk space in prior versions of the system
|
|
#>
|
|
|
|
$LogFile = "D:\ifsbosystem\log\EWOPS-MDL-NotProcessedCleanup-$(Get-Date -Format 'yyyy-MM-dd').log"
|
|
$RootFolder = 'D:\IFSBOData\FSV\Incoming_notprocessed_files\*'
|
|
$MaxAge = 14
|
|
|
|
Function Write-Log([String] $LogText,[ValidateSet('INFO','WARN','ERROR','FATAL','DEBUG')][String]$Level='INFO'){
|
|
# Please note that this function requires a $logfile var to be defined, otherwise it will output to pipeline (the standard output, usually the screen)
|
|
If ($LogFile) {
|
|
Add-Content -Path $LogFile -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $Level - $Logtext"
|
|
}
|
|
Else {
|
|
Write-Output "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $Level - $Logtext"
|
|
}
|
|
|
|
}
|
|
|
|
Write-Log -LogText "Job start"
|
|
|
|
$Folders = Get-ChildItem -Path $RootFolder -Directory
|
|
|
|
If(-not $Folders) {
|
|
Write-Log -Level WARN -LogText "Job stopping prematurely - No folders found"
|
|
}
|
|
Else {
|
|
Write-Log -LogText "Found $($Folders.Count) folders"
|
|
ForEach($Folder in $Folders) {
|
|
Try {
|
|
Write-Log -LogText "Checking $($Folder.FullName)"
|
|
If (($Folder.Name -match '^(?<Year>[0-9]{4})(?<Month>[0-9]{2})(?<Day>[0-9]{2})$') -eq 'True') { # this line is to validate the foldername and simultaneously extract the date
|
|
If ($([DateTime]"$($Matches.Year)-$($Matches.Month)-$($Matches.day)").AddDays($MaxAge) -le $(Get-Date)) { # Check that the date is older than maxage
|
|
$Folder | Remove-Item -Recurse -Force
|
|
Write-Log -LogText "$($Folder.Name) is more than $MaxAge days old, folder deleted"
|
|
}
|
|
Else {
|
|
Write-Log -LogText "$($Folder.Name) kept"
|
|
}
|
|
}
|
|
Else {
|
|
Write-Log -Level WARN -LogText "$($Folder.Name) doesn't fit naming format, folder ignored"
|
|
}
|
|
}
|
|
Catch {
|
|
Write-Log -Level FATAL -LogText "Failed: $($_.ToString().Trim())"
|
|
}
|
|
}
|
|
}
|
|
Write-Log -LogText "Job complete" |