Module/DevOps/Get-BCSDevOpsProjectBuildLog.ps1

<#
.SYNOPSIS
  Get warning and errors from a DevOps Pipeline run
 
.DESCRIPTION
  Gets a list of warnings and errors from a DevOps Pipeline run
 
.PARAMETER organisation
  DevOps Organisation Name, Default BrightComSolutions
 
.PARAMETER projectName
  DevOps Project Name
 
.PARAMETER buildId
  DevOps Build Id
  
.PARAMETER sourcePat
  DevOps Personal Access Token.
 
.EXAMPLE
  Get-BCSDevOpsProjectBuildLog -projectName "MyProjectName" -buildId "myBuildId" -sourcePat (Get-BCSSecureString -InputString "MyDevOpsPat")
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
#>


function Get-BCSDevOpsProjectBuildLog {
  Param (
    [Parameter(Mandatory = $false)]
    [string]$organisation = "BrightComSolutions",
    [Parameter(Mandatory = $true)]
    [string]$projectName,
    [Parameter(Mandatory = $true)]
    [string]$buildId,
    [Parameter(Mandatory = $true)]
    [securestring]$sourcePat
  )
  
  try {
    $fullOrgUrl = "https://dev.azure.com/$organisation";
  
    $Pat = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sourcePat))
  
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $encodedPat = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$pat"))
    $headers.Add("Authorization", "Basic $encodedPat")
    $headers.Add("Accept", "application/json")
    $headers.Add("Accept-Charset", "utf-8")
      
    $url = [uri]::EscapeUriString("$fullOrgUrl/$projectName/_apis/build/builds/$buildId/logs/?api-version=7.2-preview.2")
    $buildLogs = Invoke-RestMethod $url -Method 'GET' -Headers $headers
    
    $logEntries = @()

    $lastLogEntry = $buildLogs.value[$buildLogs.value.Count - 2]

    #foreach ($buildLogsEntry in $buildLogs.value) {
      $logId = $lastLogEntry.id

      $url = [uri]::EscapeUriString("$fullOrgUrl/$projectName/_apis/build/builds/$buildId/logs/${logId}?api-version=7.2-preview.2")
      $buildLog = Invoke-RestMethod $url -Method 'GET' -Headers $headers
  
      foreach ($entry in $buildLog.value) {
        if ($entry -match '\#\#\[section\]Starting:') {
          $textAfterSection = $entry -split '##\[section\]Starting: ', 2 | Select-Object -Last 1
          $logSection = $textAfterSection.Trim() 
        }
        if ($entry -match '\#\#\[warning\]') {
          $item = New-Object PSObject
          $item | Add-Member -type NoteProperty -Name 'logId' -Value $logId
          $item | Add-Member -type NoteProperty -Name 'section' -Value $logSection
          $item | Add-Member -type NoteProperty -Name 'type' -Value "Warning"

          $entryMessage = $entry -split '##\[warning\]', 2 | Select-Object -Last 1
          $item | Add-Member -type NoteProperty -Name 'message' -Value $entryMessage
          
          $logEntries += $item
        }
        if ($entry -match '\#\#\[error\]') {
          $item = New-Object PSObject
          $item | Add-Member -type NoteProperty -Name 'logId' -Value $logId       
          $item | Add-Member -type NoteProperty -Name 'section' -Value $logSection   
          $item | Add-Member -type NoteProperty -Name 'type' -Value "Error"

          $entryMessage = $entry -split '##\[error\]', 2 | Select-Object -Last 1
          $item | Add-Member -type NoteProperty -Name 'message' -Value $entryMessage
          
          $logEntries += $item
        }
      }
    #}

    $logEntries
  }
  catch {
    throw $_.Exception;
  }
}



Export-ModuleMember -Function Get-BCSDevOpsProjectBuildLog