Functions/Public/Get-MinionActivity.ps1
<#
.SYNOPSIS Gets activities for Minions. .DESCRIPTION This function will use the Invoke-SaltStackAPIMethod command to query the get_returns method on the ret resource to return Activities. .EXAMPLE Get-MinionActivity -SaltConnection $SaltConnection This will return the 50 most recent activities in SaltStack Config. 50 is the default limit. .EXAMPLE Get-MinionActivity -SaltConnection $SaltConnection -Limit 150 This will return the 150 most recent activities in SaltStack Config by changing Limit to 150. .EXAMPLE Get-MinionActivity -SaltConnection $SaltConnection -MinionID 'minionname' This will return the 50 most recent activities for the MinionID provided .EXAMPLE Get-MinionActivity -SaltConnection $SaltConnection -MinionID 'minionname' -JobID '20210204178834112766' This will return the activity in JobId 20210204570834112766 for the provided MinionID .EXAMPLE Get-MinionActivity -SaltConnection $SaltConnection -Function state.apply This will return the 50 most recent activities in SaltStack Config that us the function state.apply. .OUTPUTS PSCustomObject .NOTES General notes .LINK #> function Get-MinionActivity { [CmdletBinding(SupportsShouldProcess = $true)] param ( # Salt connection object [Parameter(Mandatory = $true)] [SaltConnection] $SaltConnection, # MinionID [String] $MinionID, # JobId [String] $JobID, # Function [String] $Function, # Limit [Int] $Limit = 50, # HasErrors [Switch] $HasErrors, # SortBy [Validateset('minion_id','function','has_errors','jid')] [String] $SortBy = 'jid', # SortOrder [Validateset('Ascending','Descending')] [String] $SortOrder = 'Descending' # Start # End ) $arguments = @{} $arguments.Add('sort_by',$SortBy) if ($SortOrder -eq 'Descending') { $arguments.Add('reverse','true') } if ($MinionID) { $arguments.Add('minion_id',$MinionID) } $arguments.Add('limit',$Limit) if ($JobID) { $arguments.Add('jid',$JobID) } if ($Function) { $arguments.Add('fun',$Function) } if ($HasErrors) { $arguments.Add('has_errors','true') } $return = Invoke-SaltStackAPIMethod -SaltConnection $SaltConnection -Resource ret -Method get_returns -Arguments $arguments $results = $return.ret.results $activity = @() foreach ($result in $results) { $inDesiredState = @() $notInDesiredState = @() $resultsArray = @() $resultProperties = $result.return.PSObject.Properties | Where-Object MemberType -eq 'NoteProperty' | Select-Object -ExpandProperty Name $minionChanged = $false foreach ($resultProperty in $resultProperties) { if ($result.return.$resultProperty.comment -ne 'State was not run because none of the onchanges reqs changed') { $changed = $false $testChange = $result.return.$resultProperty.changes.PSObject.members | Select-Object -First 1 if ($testChange.OverloadDefinitions) { $changed = $false } else { $changed = $true $minionChanged = $true } $resultsObj = $null $resultsObj = [PSCustomObject]@{ ID = $result.return.$resultProperty.__id__ Result = $result.return.$resultProperty.result Command = $resultProperty StartTime = $result.return.$resultProperty.start_time Duration = $result.return.$resultProperty.Duration Changes = $result.return.$resultProperty.changes PChanges = $result.return.$resultProperty.pchanges SLS = $result.return.$resultProperty.__sls__ Comment = $result.return.$resultProperty.comment Name = $result.return.$resultProperty.name RunNum = $result.return.$resultProperty.__run_num__ SkipWatch = $result.return.$resultProperty.skip_watch Changed = $changed } $resultsArray += $resultsObj if ($changed) { $notInDesiredState += $resultsObj } else { $inDesiredState += $resultsObj } } } $obj = [PSCustomObject]@{ MinionID = $result.minion_id JID = $result.jid Date = ([DateTime]$result.alter_time).ToLocalTime() Function = $result.fun Arguments = $result.fun_args.mods Changes = $minionChanged HasErrors = $result.has_errors Results = $resultsArray InDesiredState = $inDesiredState NotInDesiredState = $notInDesiredState } $activity += $obj } Write-Output $activity | Select-Object MinionID,JID,Date,Function,Arguments,Changes,HasErrors,Results,InDesiredState,NotInDesiredState } |