Windows PowerShell of AD Users to JSON Endpoint Fails to Upload Data

Windows PowerShell of AD Users to JSON Endpoint Fails to Upload Data

by Tim West -
Number of replies: 0

Hi All,

I am trying to update a JSON endpoint from PowerShell and have test it is working fine from the Advanced REST Client Chrome Add On.

 

$CreateUserDealer = (Get-ADUser -Filter * -ResultSetSize $RSS -SearchBase $OUD -Properties * | Where {$_.Enabled -eq $True -and $_.EmailAddress -ne $Null -or $_.Manager -ne $Null} | Select UserPrincipalName,GivenName,Surname,EmailAddress,Department,Company,Manager,Title,EmployeeType,EmployeeNumber | Sort UserPrincipalName)

$URI = 'https://<REDACTED>/webservice/rest/server.php' #JSON Endpoint
$wsToken = '6<REDACTED>0'
$wsFunction = 'local_<REDACTED>_webservices_create_user'
#$RestFormat = 'json'
$RestFormat = 'application/x-www-form-urlencoded'
$Data = @($CreateUserDealer)

$a = @{wstoken=$wsToken;wsfunction=$wsFunction;moodlewsrestformat=$RestFormat;data=$Data}
$Body = ($a | ConvertTo-Json)
$ContentType = 'application/x-www-form-urlencoded'

$r = (Invoke-RestMethod -Uri $URI -Body $Body -Method Post -ContentType $ContentType -verbose)
$r.EXCEPTION



#########

OUTPUT:

PS C:\WINDOWS\system32> $a

Name                           Value                                                                                                                                                                               
----                           -----                                                                                                                                                                               
wstoken                        6<REDACTED>0                                                                                                                                                    
data                           {@{UserPrincipalName=John.Smith@domain.com; GivenName=John; Surname=Smith; EmailAddress=John@domain.com; Department=Retail Dealer; Company=Team Company; Manager=CN=Simon Smith...
moodlewsrestformat             application/x-www-form-urlencoded                                                                                                                                                   
wsfunction                     local_<REDACTED>_webservices_create_user   


PS C:\WINDOWS\system32> $r

xml EXCEPTION                                                                                               

--- ---------                                                                                               

version="1.0" encoding="UTF-8" EXCEPTION    



PS C:\WINDOWS\system32> $Body
{
    "wstoken":  "6<REDACTED>0",
    "data":  [
                 {
                     "UserPrincipalName":  "John.Smith@Domain.com",
                     "GivenName":  "John",
                     "Surname":  "Smith",
                     "EmailAddress":  "john@domain.com",
                     "Department":  "Retail Dealer",
                     "Company":  "Team Company",
                     "Manager":  "CN=Simon Smith,OU=Managers,OU=Users,DC=DOMAIN,DC=com",
                     "Title":  "Dealer Store Manager",
                     "EmployeeType":  "Retail Dealer",
                     "EmployeeNumber":  null
                 },
                 {
                     "UserPrincipalName":  "Brian.Smith@domain2.com",
                     "GivenName":  "brian",
                     "Surname":  "Smith",
                     "EmailAddress":  "brian@domain2.com",
                     "Department":  "Retail Dealer",
                     "Company":  "Company2",
                     "Manager":  "CN=Krystal Smith,OU=Managers,OU=Users,DC=domain,DC=com",
                     "Title":  "Sales Assistant",
                     "EmployeeType":  "Retail Dealer",
                     "EmployeeNumber":  null
                 },
              ],
    "moodlewsrestformat":  "application/x-www-form-urlencoded",
    "wsfunction":  "local_<REDACTED>_webservices_create_user"
}


Any help would be gratefully received.


Average of ratings: -