#Convert Credentials to Base64 for URL Header
$bytes = [System.Text.Encoding]::UTF8.GetBytes((’{0}:{1}’ -f $directorUser , $directorPass))
$authorization = ‘Basic {0}’ -f ([Convert]::ToBase64String($bytes))
#Create a new Header
$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
#Add converted Authorization
$headers.Add(“Authorization”, “$authorization”) #Add Application type
$headers.Add(‘Accept’,‘application/json’)
#RestMethod
Invoke-RestMethod -Method GET -Headers $headers -Uri “$DirectorURL/host?name=CLIENT01.here.local”
If I use a non admin powershell I get the following result: Invoke-RestMethod : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
If I use a powershell with admin privileges I get the following result:
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
I have tested the credentials with “RESTED” a Firefox extension.
Hi,
I’m not really familiar with PowerShell so I can’t give you a special hint for this.
But maybe the source code of the “Icinga 2 Powershell Module” helps you to find some ideas for this.
This only allows TLS12 for your script. Microsoft not configured tls1.2 to be enabled by default
Btw. for some reason I needed the “Convert credential” thing on one of my installations. It normally works with Invoke-Restmethod -Credential switch too and without the conversion.
example:
Invoke-RestMethod -Method Get -Headers $headers -Uri “$BaseURL/host`?name=myhostname” -Credential $Credential
I changed my script and the SSL/TLS Error is not present but i get the Message “Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.”
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))