public/Get-PodEvent.ps1

<#
.SYNOPSIS
Get events for a pod

.PARAMETER PodName
Name of the pod to get events for

.PARAMETER NoNormal
If set, don't return normal events, only warnings

.PARAMETER Namespace
K8s namespace to use, defaults to default

.EXAMPLE
Get-PodEvent -PodName mypod

Get all events for pod mypod

.EXAMPLE
Get-PodEvent -PodName mypod -NoNormal -Namespace test

Get all non-normal events for pod mypod in namespace test

.OUTPUTS
One or more event objects for the pod, $null if error
#>

function Get-PodEvent {
    param (
        [CmdletBinding()]
        [Parameter(Mandatory = $true)]
        [string] $PodName,
        [switch] $NoNormal,
        [string] $Namespace = "default"
    )
    Write-Verbose "kubectl get events --namespace $Namespace --field-selector `"involvedObject.name=$PodName`" -o json"
    $events = kubectl get events --namespace $Namespace --field-selector "involvedObject.name=$PodName" -o json | ConvertFrom-Json

    if ($LASTEXITCODE -ne 0) {
        return $null
    }
    if ($null -eq $events) { # valid if no events, such as it didn't need to create a new pod
        return @()
    }
    if ($NoNormal) {
        $events.items | Where-Object { $_.type -ne "Normal" }
    } else {
        $events.items
    }
}