Get information via Poweshell and Icinga Director API

Thanks for your help, that’s what it was.

Solution:
I created a user under IcingaWeb2 → configuration → authentication → user → "Create new user

After that I created a new role under IcingaWeb2 → configuration → authentication → Rollen→ “Create new role” and in the module “director” I added the rights “General Module Access” and “director/*”. I added the user “user” to the role.

This gave me a correctly configured user for working with the Icinga Director API.

Then I wrote a script that creates the BasicAuthToken so that I can use it in the API script and the username and password are not visible in the API script.

#Step 0. Set User and Password
$username = “user”
$password = “pass”

#Step 1. Create a username:password pair
$credPair = “$($username):$($password)”

#Step 2. Encode the pair to Base64 string
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))

#Step 3. Write.Output BasicAuth String
Write-Output $encodedCredentials

Now that I have a working user I have built a simple script for testing, it looks like this:

#Enable TLS 1.2 - Is disabled by default
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”

$headers.Add(“Accept”, “application/json”)

$headers.Add(“Authorization”, “Basic PasteBasicAuthStringhere”)

$response = Invoke-RestMethod ‘https://icingaserver.here.local/icingaweb2/director/host?name=CLIENT01.here.local’ -Method ‘GET’ -Headers $headers

$response | ConvertTo-Json

2 Likes