|
| 1 | +function Invoke-IcingaForWindowsRESTApi() |
| 2 | +{ |
| 3 | + param ( |
| 4 | + [string]$Uri = 'v1', |
| 5 | + [ValidateSet('GET', 'POST')] |
| 6 | + [string]$Method = 'GET', |
| 7 | + [hashtable]$Body = @{ } |
| 8 | + ); |
| 9 | + |
| 10 | + if ((Test-IcingaCAInstalledToAuthRoot) -eq $FALSE) { |
| 11 | + Write-IcingaConsoleError 'The Icinga CA certificate is not installed to the local machine certificate store. Please run the "Start-IcingaWindowsScheduledTaskRenewCertificate" command to fix this issue.'; |
| 12 | + return $null; |
| 13 | + } |
| 14 | + |
| 15 | + Set-IcingaTLSVersion; |
| 16 | + $IcingaForWindowsCertificate = Get-IcingaForWindowsCertificate; |
| 17 | + $RestApiPort = 5668; |
| 18 | + [int]$Timeout = 20; |
| 19 | + $BackgroundDaemons = Get-IcingaBackgroundDaemons; |
| 20 | + |
| 21 | + if ($null -ne $BackgroundDaemons -And $BackgroundDaemons.ContainsKey('Start-IcingaWindowsRESTApi')) { |
| 22 | + $Daemon = $BackgroundDaemons['Start-IcingaWindowsRESTApi']; |
| 23 | + |
| 24 | + # Fetch our deamon configuration |
| 25 | + if ($Daemon.ContainsKey('-Port')) { |
| 26 | + $RestApiPort = $Daemon['-Port']; |
| 27 | + } elseif ($Daemon.ContainsKey('Port')) { |
| 28 | + $RestApiPort = $Daemon['Port']; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + [string]$WebBaseURL = [string]::Format('https://{0}:{1}', ($IcingaForWindowsCertificate.Subject.Replace('CN=', ''), $RestApiPort)); |
| 33 | + |
| 34 | + $WebBaseURL = Join-WebPath -Path $WebBaseURL -ChildPath $Uri; |
| 35 | + |
| 36 | + [hashtable]$Arguments = @{ |
| 37 | + '-Method' = $Method; |
| 38 | + '-UseBasicParsing' = $TRUE; |
| 39 | + '-Uri' = $WebBaseURL; |
| 40 | + '-ContentType' = 'application/json'; |
| 41 | + '-TimeoutSec' = $Timeout; |
| 42 | + '-Certificate' = $IcingaForWindowsCertificate; |
| 43 | + '-ErrorAction' = 'Stop'; |
| 44 | + }; |
| 45 | + |
| 46 | + if ($null -ne $Body -And $Body.Count -ne 0 -And $Method -eq 'POST') { |
| 47 | + $Arguments.Add('-Body', (ConvertTo-JsonUTF8Bytes -InputObject $Body -Depth 100 -Compress)); |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + Invoke-WebRequest @Arguments |
| 52 | + ); |
| 53 | +} |
0 commit comments