Skip to content

Instantly share code, notes, and snippets.

@herculosh
Last active September 26, 2019 05:03
Show Gist options
  • Save herculosh/6c2393b4ddae1736b5348a137b007288 to your computer and use it in GitHub Desktop.
Save herculosh/6c2393b4ddae1736b5348a137b007288 to your computer and use it in GitHub Desktop.
Remove all Jobs in Jenkins older then 7 days
$jenkinsJobFolder = "\\servername\c$\Program Files (x86)\Jenkins\jobs\Falldaten Archivierung\builds"
# last* files are not taken for analysis
$allBuildJobFolder = Get-ChildItem -Directory $jenkinsJobFolder | ?{!($_.Name -ilike "last*")}
foreach($jobFolder in $allBuildJobFolder)
{
$buildxmlPath = (join-path -Path $jobFolder.fullname -ChildPath "build.xml")
if((get-item $buildxmlPath | Get-Content -Raw).Contains("<result>UNSTABLE</result>"))
{
if((get-item $buildxmlPath).LastWriteTime -lt (get-date).AddDays(-7))
{
write-host ("{0,-100}" -f $jobFolder.FullName) -BackgroundColor DarkYellow -ForegroundColor White
write-host ("`t{0} - {1}" -f "build.xml", (get-item $buildxmlPath).LastWriteTime) -ForegroundColor DarkGreen
remove-item $jobFolder.fullname -Recurse
write-host ("`tDeleteing - Job {0}" -f $jobFolder.name) -ForegroundColor DarkGreen
write-host ("`t{0}`n`r" -f $buildxmlPath )
}
}
}
# Keep in mind to reload the Jenkins Configuration after execution of this Script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment