Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. ##For some reason ./ doesnt work since it defaults to documents
  2. $path = Split-Path -Parent -Path $myInvocation.MyCommand.Definition
  3. $path = $path + "\jobs.csv"
  4. ##Login info
  5. $username = "test"
  6. $password = "test"
  7. $credentialAsBytes = [System.Text.Encoding]::ASCII.GetBytes($username + ":" + $password)
  8. $credentialAsBase64String = [System.Convert]::ToBase64String($credentialAsBytes)
  9. ##Jenkins CLI
  10. (new-object System.Net.WebClient).DownloadFile("http://localhost:8080/jnlpJars/jenkins-cli.jar", ".\jenkins-cli.jar")
  11. ##List of all jobs
  12. java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs --username $username --password $password > jobs.csv
  13. ##Login to Jenkins
  14. $webClient = new-object System.Net.WebClient
  15. $webClient.Headers[[System.Net.HttpRequestHeader]::Authorization] = "Basic " + $credentialAsBase64String
  16. ##Excel output
  17. $xl = new-object -com excel.application
  18. $xl.visible=$false
  19. $wb = $xl.Workbooks.Open($path)
  20. $ws = $wb.WorkSheets.item(1)
  21. $rows = $ws.UsedRange.Rows.Count
  22. for ($i=1; $i -le $rows; $i++) {
  23. $job = $ws.Cells.Item($i,1).text
  24. $xml = $webClient.DownloadString("http://localhost:8080/job/$job/lastBuild/api/xml")
  25. if ($xml -match "<result>(.*)</result>") {
  26. $state = new-object System.String($matches[0])
  27. $state = $state -replace "<result>", "" -replace "</result>", ""
  28. $xml -match "<number>(.*)</number>" | out-null
  29. $build = new-object System.String($matches[0])
  30. $build = $build -replace "<number>", "" -replace "</number>", ""
  31. Remove-Variable xml
  32. }
  33. Else {
  34. $state = "NO BUILD"
  35. $build = "0"
  36. }
  37. $ws.Cells.Item($i,2)=$state
  38. $ws.Cells.Item($i,3)=$build
  39. }
  40. ##Clean up
  41. $webClient.Quit
  42. $xl.DisplayAlerts=$false
  43. $wb.Save()
  44. $xl.Quit()
  45. [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
  46. ##Write-Host "Press any key to close..."
  47. ##$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement