cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements

193
Views
0
Helpful
1
Replies
tormenteddave
Beginner

CMS LDAP Sync via PowerShell

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
tormenteddave
Beginner

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

View solution in original post

1 REPLY 1
tormenteddave
Beginner

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

View solution in original post

Create
Recognize Your Peers
Content for Community-Ad