Has anyone got the api to work successfully with powershell?
VastattuI'm trying to use the api with powershell and getting some weird errors that I've not been able to work through.
The initial attempts to connect would give me Invoke-RestMethod : The request was aborted: Could not create SSL/TLS secure channel.
This was resolved by first using
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11,tls,ssl3"
Once that was entered I could connect,but just returns
Invoke-RestMethod : The remote server returned an error: (406) Not Acceptable
Sample code as follows:-
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11,tls,ssl3"
#$hdrs=@{"Authorization","Bearer eyJ0es_Snip_Of_Full_API_Key_...g8"}
$uri="https://us.api.knowbe4.com/v1/groups"
$response = Invoke-RestMethod -uri $uri -Headers $Hdrs
Invoke-RestMethod : The remote server returned an error: (406) Not Acceptable.
At line:1 char:13
+ $response = Invoke-RestMethod -uri $uri -Headers $Hdrs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
-
You're close!
I just started using the API myself and had the same issue.
Error 406 is client side, so I looked into it. It needs an 'Accept' header that tells the server a valid response can only contain types of info. As a shot in the dark, and just by looking at what the API returns (json) I decided to add in the MIME type for json:
$header = @{"Authorization" = "Bearer $apikey"; "Accept"="application/json"}
Invoke-RestMethod -Uri https://us.api.knowbe4.com/v1/account/ -header $header
-
Hi Renato!
Thanks for providing this solution!
Andy, if this does not resolve the error, feel free to let us know, or email support@KnowBe4.com for assistance.
Thanks!
Lauren
KnowBe4 -
Thank you Renato - this worked a treat!
I also found a powershell module at https://www.powershellgallery.com/packages/KnowBe4/1.1.1 that also helped me to get information back from Knowbe4. It has three functions included, Get-KnowBe4Campaign Get-KnowBe4User Get-KnowBe4IncompleteTraining. Interestingly it uses curl.exe instead of the builtin invoke-restmethod commands.
@Lauren - it would be useful to add the sample code to your documentation to help others in the future.
$knowbe4apikey="insertyourapikeyhere"
$hdrs=@{"Authorization"="Bearer $knowbe4apikey";"Accept"="application/json"}
$uri="https://us.api.knowbe4.com/v1/groups"
$response = Invoke-RestMethod -uri $uri -Headers $Hdrs
$response
id : 694443
name : Clickers
group_type : console_group
adi_guid :
member_count : 10
status : activeid : 710278
name : Report Viewers
group_type : console_group
adi_guid :
member_count : 1
status : activeid : 710090
name : Tset
group_type : console_group
adi_guid :
member_count : 1
status : archived -
Thanks, Andy!
I've added a note for our team to add this information to our Knowledgebase.
It appears that you've provided all of the information we could need, but would you be open to me, or someone else from our team reaching out for clarification, if necessary?
As always, we appreciate your involvement in our product, and our community board, Andy.
Thanks again!
Lauren
KnowBe4
Kirjaudu sisään jättääksesi kommentin.
Kommentit
5 kommenttia