PSGraylog/Functions/Public/Invoke-GraylogRequest.ps1

63 lines
2.3 KiB
PowerShell

function Invoke-GraylogRequest {
<#
.SYNOPSIS
Invokes a request to the Graylog API.
.DESCRIPTION
Invokes a request to the Graylog API using the provided parameters and HTTP method.
.PARAMETER Method
The HTTP method to use for the request (default: GET, available: DELETE, GET, HEAD, OPTIONS, POST, PUT, PATCH).
.PARAMETER Path
The path to the API endpoint to invoke the request to.
.PARAMETER Body
The (optional) body of the request to send to the API endpoint, which should be provided as a JSON string (or eqivalent object for other formats).
.PARAMETER ContentType
The content type of the request body (default: application/json).
.OUTPUTS
The response from the Graylog API.
.EXAMPLE
Invoke-GraylogRequest GET "/api/system/sessions"
.EXAMPLE
Invoke-GraylogRestRequest -Method GET -Path "/api/system/sessions"
Invokes a GET request to the /api/system/sessions endpoint in the Graylog API using the global WebSession object.
.EXAMPLE
Invoke-GraylogRestRequest -Method POST -Path "/api/system/sessions" -Body @{
host = $BaseURI.Authority
username = $Credential.Username.Split("@")[0]
password = $Credential.GetNetworkCredential().Password
}
Invokes a POST request to the /api/system/sessions endpoint in the Graylog API using the global WebSession object and the provided body.
.NOTES
This function is used to invoke requests to the Graylog API using the provided parameters.
The Method, Path, and WebSession parameters are required, while the Body, ContentType, Headers, and UserAgent parameters are optional.
#>
[Alias("igsr")]
param (
[Parameter(Mandatory)]
[ValidateSet("DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH")]
[string]
$Method,
[Parameter(Mandatory)]
[string]
$Path,
[Parameter()]
[string]
$Body,
[Parameter()]
[string]
$ContentType = "application/json"
)
if (-NOT (Test-GraylogSession -SkipSessionCheck)) { return } # Ensure that the Secrets are set
$Session = Get-Secret Graylog_Session -Vault Graylog -AsPlainText -ErrorAction Stop
$Request = @{
Method = $Method
URI = "$(Get-Secret Graylog_BaseURI -Vault Graylog -AsPlainText)/api/$($Path.TrimStart('/api'))"
WebSession = (ConvertTo-GraylogSession $Session)
ContentType = $ContentType
}
if ($Body) { $Request.Body = $Body }
try { Invoke-RestMethod @Request }
catch { throw $_.Exception.Message }
}