05-21-2021 06:02 PM - edited 05-21-2021 06:12 PM
Since LDAP doesn't sync automagicaly, I'm trying to write a PowerShell Script that will run and and send a Post to the server to kick off the sync. My issue is I'm getting a 401 unauthorized. I'm assuming its the "Username/Password" isn't what the API is expecting so its it failing? I do have a user ldapsync that has api rights.
ErrorMessage: Invoke-WebRequest : The remote server returned and error: (401) Unauthorized.
Below is my code (go easy, this is my first PowerShell script).
Set-StrictMode -Version Latest
[Net.ServicePointManager]::SecurityProtocol = 'Tls12'
$ErrorActionPreference = "stop" $Body = @{ Username = 'ldapsync' Password = 'syncme' } $LoginResponse = Invoke-WebRequest 'https://CMSFQDN:447/api/v1/ldapSyncs' -Body $Body -Method Post
Solved! Go to Solution.
05-26-2021 07:39 AM
I used this and finally got it to work, not super secure but it works.
Set-StrictMode -Version Latest [Net.ServicePointManager]::SecurityProtocol = 'Tls12' [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $ErrorActionPreference = "Stop" $username = "ldapsync" $password = "syncme" $pair = "${username}:${password}" $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $base64 = [System.Convert]::ToBase64String($bytes) $basicAuthValue = "Basic $base64" $headers = @{ Authorization = $basicAuthValue } $url = 'https://CMSFQDN:447/api/v1/ldapSyncs/' Invoke-WebRequest -uri $url -Headers $headers -Method Post -Body $Body
05-26-2021 07:39 AM
I used this and finally got it to work, not super secure but it works.
Set-StrictMode -Version Latest [Net.ServicePointManager]::SecurityProtocol = 'Tls12' [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $ErrorActionPreference = "Stop" $username = "ldapsync" $password = "syncme" $pair = "${username}:${password}" $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $base64 = [System.Convert]::ToBase64String($bytes) $basicAuthValue = "Basic $base64" $headers = @{ Authorization = $basicAuthValue } $url = 'https://CMSFQDN:447/api/v1/ldapSyncs/' Invoke-WebRequest -uri $url -Headers $headers -Method Post -Body $Body
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide