Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##For some reason ./ doesnt work since it defaults to documents
- $path = Split-Path -Parent -Path $myInvocation.MyCommand.Definition
- $path = $path + "\jobs.csv"
- ##Login info
- $username = "test"
- $password = "test"
- $credentialAsBytes = [System.Text.Encoding]::ASCII.GetBytes($username + ":" + $password)
- $credentialAsBase64String = [System.Convert]::ToBase64String($credentialAsBytes)
- ##Jenkins CLI
- (new-object System.Net.WebClient).DownloadFile("http://localhost:8080/jnlpJars/jenkins-cli.jar", ".\jenkins-cli.jar")
- ##List of all jobs
- java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs --username $username --password $password > jobs.csv
- ##Login to Jenkins
- $webClient = new-object System.Net.WebClient
- $webClient.Headers[[System.Net.HttpRequestHeader]::Authorization] = "Basic " + $credentialAsBase64String
- ##Excel output
- $xl = new-object -com excel.application
- $xl.visible=$false
- $wb = $xl.Workbooks.Open($path)
- $ws = $wb.WorkSheets.item(1)
- $rows = $ws.UsedRange.Rows.Count
- for ($i=1; $i -le $rows; $i++) {
- $job = $ws.Cells.Item($i,1).text
- $xml = $webClient.DownloadString("http://localhost:8080/job/$job/lastBuild/api/xml")
- if ($xml -match "<result>(.*)</result>") {
- $state = new-object System.String($matches[0])
- $state = $state -replace "<result>", "" -replace "</result>", ""
- $xml -match "<number>(.*)</number>" | out-null
- $build = new-object System.String($matches[0])
- $build = $build -replace "<number>", "" -replace "</number>", ""
- Remove-Variable xml
- }
- Else {
- $state = "NO BUILD"
- $build = "0"
- }
- $ws.Cells.Item($i,2)=$state
- $ws.Cells.Item($i,3)=$build
- }
- ##Clean up
- $webClient.Quit
- $xl.DisplayAlerts=$false
- $wb.Save()
- $xl.Quit()
- [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
- ##Write-Host "Press any key to close..."
- ##$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement