I have a powershell script which reads the path from the batch file as it calls the script to look into delete the files from various paths. I would like to tidy up the script and use it as one script to look into various path and delete the files respectively and log them in a log files as it deletes with some formatting or information Like it has deleted files from Bitbucket, Jira and Confluence and their respective path/locations. Is there any better way to achieve this. Pleas help.
Actual function :
param(
$Keep = 1000,
$Path,
$Regex)
$OldPath = Get-Location
if (Test-Path $Path){
Set-Location $Path
# Use [array] to force $Files to be an array. Otherwise when there is a single file then $Files
# would be set to be the the contents of that single file instead of an array of filenames
[array]$Files = ls | Sort-Object LastWriteTime | Where-Object {$_.Name -match $Regex}
$Delete = $Files.Length - $Keep
$FileCount = $Files.Length
if ($Delete -gt 0) {
for ($i=0; $i -lt $Delete; $i ){
Add-Content E:\backup\housekeep_log\Housekeep.log $files # $filename
$Files[$i].Delete()
}
} else {
Write-Host "Keep $Keep is equal to or greater than number of files $FileCount, keeping all"
}
} else
{
Write-Host "Folder not valid"
}
Set-Location $OldPath
the above function gets called using below batch file
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Atlassian\ApplicationData\Bitbucket\export" -Keep 5 -Regex ^backup.*.zip
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Atlassian\ApplicationData\Bitbucket\log\audit" -Keep 30 -Regex ^atlassian-bitbucket-audit-
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Atlassian\ApplicationData\Bitbucket\log" -Keep 30 -Regex ^atlassian-bitbucket-2
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Atlassian\ApplicationData\Bitbucket\log" -Keep 30 -Regex ^atlassian-bitbucket-access-
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Atlassian\bitbucket-backup-client-4.0.0\backups" -Keep 3 -Regex ^bitbucket.*.tar
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\Application Data\Confluence\backups" -Keep 5 -Regex ^backup.*.zip
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\Confluence\logs" -Keep 30 -Regex ^catalina.*log
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\Confluence\logs" -Keep 30 -Regex ^confluence
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\Confluence\logs" -Keep 30 -Regex ^commons-daemon
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\Confluence\logs" -Keep 30 -Regex ^gc-
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\Application Data\JIRA\export" -Keep 5 -Regex ^zip
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\JIRA\logs" -Keep 30 -Regex ^catalina.*log
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\JIRA\logs" -Keep 30 -Regex ^access.*log
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\JIRA\logs" -Keep 30 -Regex ^atlassian-jira-gc
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\JIRA\logs" -Keep 30 -Regex ^commons-daemon
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\JIRA\logs" -Keep 30 -Regex ^jirasoftware
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1 -Path "C:\Program Files\Atlassian\JIRA\logs" -Keep 30 -Regex ^localhost
CodePudding user response:
I think I would create a CSV file where all paths, number of items to keep and nam regexes are stored, next to where you have the Housekeep.log.
That way, it would be fairly easy to maintain.
Something like this (lets call it HousekeepConfig.csv
):
"Path","Keep","Regex"
"C:\Atlassian\ApplicationData\Bitbucket\export","5","^backup.*.zip"
"C:\Atlassian\ApplicationData\Bitbucket\log\audit","30","^atlassian-bitbucket-audit-"
"C:\Atlassian\ApplicationData\Bitbucket\log","30","^atlassian-bitbucket-2"
"C:\Atlassian\ApplicationData\Bitbucket\log","30","^atlassian-bitbucket-access-"
"C:\Atlassian\bitbucket-backup-client-4.0.0\backups","3","^bitbucket.*.tar"
"C:\Program Files\Atlassian\Application Data\Confluence\backups","5","^backup.*.zip"
"C:\Program Files\Atlassian\Confluence\logs","30","^catalina.*log"
"C:\Program Files\Atlassian\Confluence\logs","30","^confluence"
"C:\Program Files\Atlassian\Confluence\logs","30","^commons-daemon"
"C:\Program Files\Atlassian\Confluence\logs","30","^gc-"
"C:\Program Files\Atlassian\Application Data\JIRA\export","5","^zip"
"C:\Program Files\Atlassian\JIRA\logs","30","^catalina.*log"
"C:\Program Files\Atlassian\JIRA\logs","30","^access.*log"
"C:\Program Files\Atlassian\JIRA\logs","30","^atlassian-jira-gc"
"C:\Program Files\Atlassian\JIRA\logs","30","^commons-daemon"
"C:\Program Files\Atlassian\JIRA\logs","30","^jirasoftware"
"C:\Program Files\Atlassian\JIRA\logs","30","^localhost"
Note:
Since you are using regex -match
, I'd advice you to check if in the regex strings some dots need escaping and may need end-anchors like .zip
--> \.zip$
if you are referring to the extension there.
Then your script could be like below:
# read the HousekeepConfig.csv file and loop through the items
$config = Import-Csv -Path 'E:\backup\housekeep_log\HousekeepConfig.csv'
foreach ($item in $config) {
if (Test-Path $item.Path -PathType Container) {
$keep = [int]$item.Keep
# Use the @() operator to force $files to be an array.
$files = @(Get-ChildItem -Path $item.Path -File | Where-Object {$_.Name -match $item.Regex})
if (($files.Count - $keep) -gt 0) {
# reduce the array to contain only the number of files in $keep
$files = $files | Sort-Object LastWriteTime | Select-Object -First $keep
$files.FullName | Add-Content -Path 'E:\backup\housekeep_log\Housekeep.log'
$files | Remove-Item -Force
}
else {
Write-Host "Keep $($item.Keep) is equal to or greater than number of files $($Files.Count), keeping all"
}
}
else {
Write-Warning "Folder $($item.Path) not valid"
}
}
From a batch file (although why not directly using PowerShell??) you can then simply call
powershell.exe -ExecutionPolicy remotesigned -File housekeep.ps1