From 11343ad88ca74be05532748c22a5c0588e06e418 Mon Sep 17 00:00:00 2001 From: Mendy Green Date: Thu, 2 Nov 2023 21:22:45 -0400 Subject: [PATCH 1/2] Create Invoke-HaloSQL.ps1 Add SQL queries capability to HaloAPI --- Public/Invoke/Invoke-HaloSQL.ps1 | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Public/Invoke/Invoke-HaloSQL.ps1 diff --git a/Public/Invoke/Invoke-HaloSQL.ps1 b/Public/Invoke/Invoke-HaloSQL.ps1 new file mode 100644 index 0000000..8d74ba1 --- /dev/null +++ b/Public/Invoke/Invoke-HaloSQL.ps1 @@ -0,0 +1,47 @@ +Function Invoke-HaloSQL { + <# + .SYNOPSIS + Uses the Reports API endpoint to run arbitrary SQL. + .DESCRIPTION + Function to run a report preview with a SQL query parameter. Use -IncludeFullReport to get the entire report object instead of just the results + .OUTPUTS + Outputs an object containing the response from the web request. + #> + [CmdletBinding( SupportsShouldProcess = $True )] + [OutputType([Object[]])] + Param ( + # String or Here String for SQL Statement + [Parameter( Mandatory = $True, ValueFromPipeline )] + [String]$SQLQuery, + # Return Full Report Object. + [Parameter()] + [Switch]$IncludeFullReport + ) + Invoke-HaloPreFlightCheck + try { + $Payload = @(@{id = 0; name= ''; sql = $SQLQuery; apiquery_id = 0; "_testonly" = $false; "_loadreportonly" = $true}) + if ($PSCmdlet.ShouldProcess($Payload -is [Array] ? 'Reports' : 'Report', 'Execute')) { + $Report = New-HaloPOSTRequest -Object $Payload -Endpoint 'report' + if (-not $Report.report.load_error) { + + if ($Report.report.rows.count -eq 0) { + throw "No results were returned" + } + + else { + if ($IncludeFullReport) { + return $Report + } + else { + return $Report.report.rows + } + } + } else { + Throw $Report.report.load_error + } + } + + } catch { + New-HaloError -ErrorRecord $_ + } +} From af5457a87b241fca1716333d2df21f54530fc3ec Mon Sep 17 00:00:00 2001 From: Mendy Green Date: Thu, 2 Nov 2023 21:23:41 -0400 Subject: [PATCH 2/2] Update HaloAPI.psd1 --- HaloAPI.psd1 | 1 + 1 file changed, 1 insertion(+) diff --git a/HaloAPI.psd1 b/HaloAPI.psd1 index fb6d407..2b1d271 100644 --- a/HaloAPI.psd1 +++ b/HaloAPI.psd1 @@ -117,6 +117,7 @@ 'Get-HaloWorkflow', 'Get-HaloWorkflows', 'Invoke-HaloRequest', + 'Invoke-HaloSQL', 'New-HaloAction', 'New-HaloActionBatch', 'New-HaloAgent',