Cannot connect to API using my powershell script. It looks like it connects but then when it starts running through the AD portion of my script it errors out with this error:
t C:\PSAutomations\RCUpdateInfoV3-test.ps1:36 char:17 + ... $userinfo = Get-ADUser -Filter "UserPrincipalName -eq '$email'" -prop ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser Invoke-RestMethod : { "message": "Need Content-Type header"
Full Script:
$RC_SERVER_URL="https://platform.ringcentral.com" $RC_CLIENT_ID="Yxxxxxx" $RC_CLIENT_SECRET="xxxxxx" $RC_JWT_TOKEN = "xxxxxxxx" $headers = @{ ContentType = 'application/x-www-form-urlencoded; charset=UTF-8' Accept = 'application/json' Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${RC_CLIENT_ID}:${RC_CLIENT_SECRET}")) } # RC authenticate and obtain access token $response = Invoke-RestMethod -Method POST -Uri ($RC_SERVER_URL + "/restapi/oauth/token") -Headers $headers -Body @{ grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer' assertion = $RC_JWT_TOKEN } # Extyract Access Token $access_token = $response.access_token # Fetch User info from RC api $AllUsers = Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/?perPage=1000") -Headers @{ Authorization = "Bearer $access_token" } -Method Get # Array to store users with AD information $UsersArray = @() #import AD Module Import-Module ActiveDirectory # Iterating over user records foreach ($id in $AllUsers.records) { $email = $id.contact.email # Retrieving AD user information using email $userinfo = Get-ADUser -Filter "UserPrincipalName -eq '$email'" -properties department, title $emailCheck = $userinfo.UserPrincipalName if ($emailcheck -ne $null -and $id.type -ne "Department") { $department = $userinfo.department $jobtitle = $userinfo.title $UsersArray += @( @{ id = $id.id email = $id.contact.email department = $department jobtitle = $jobtitle } ) } } # Iterating over users and updating profiles foreach ($user in $UsersArray) { $did = $user.id $putbody = '{"contact": {"jobTitle":"' + $user.jobtitle + '"}}' Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/$did") -Headers @{ Authorization = "Bearer $access_token" ContentType = 'application/json' # Add Content-Type header } -Method PUT -Body $putbody $putdepartment = '{"contact": {"department":"' + $user.department + '"}}' Invoke-RestMethod ($RC_SERVER_URL + "/restapi/v1.0/account/~/extension/$did") -Headers @{ Authorization = "Bearer $access_token" ContentType = 'application/json' # Add Content-Type header } -Method PUT -Body $putdepartment Start-Sleep -Seconds 3 $did = $null }