0

I have been working on a script that essentially creates a user using an API. It takes a list of active directory users and creates account on the second software using the API using invoke method. Now the problem is that it will go through for some of them while give error 400 for others saying the required field is missing so i logged the body of failed requests and tried them with postman they work correctly. I can't understand this behavior and if there is any way around it.

Function CreatePhishUser($user){
        
               
            if($user.PreferredLanguage -eq $null)
                {
                Write-Output "heh"
                   $body = @{
                    "id"= "somethingsomething"; #English
                   "target"= @{
                        "email"= $user.emailaddress;
                        "first_name"= $user.GivenName;
                        "last_name"= $user.Surname;
                        "company"= $user.Company;
                        "title"= $user.Title;
                        "address_one"= $user.StreetAddress;
                        "city"= $user.City;
                        "state"= $user.State;
                        "zip"= $user.PostalCode;
                        "country"= $user.Country;
                        "phone_business"= $user.OfficePhone;
                        "phone_business_fax"= $user.Fax;
                        "phone_mobile"= $user.telephoneNumber;
                        "language"= "en";
                        "department"= $user.Department;
                        "manager"= "hello";
                        "is_active"= 1;
                        "groups" = @{
                            "id"="something something9";
                            "name"="English"
                            
                       
                       
                       }
                    }
                    }
             }else {
                        $body = @{
                        "id"= "somethingsomething";  #French
                       "target"= @{
                            "email"= $user.emailaddress;
                            "first_name"= $user.GivenName;
                            "last_name"= $user.Surname;
                            "company"= $user.Company;
                            "title"= $user.Title;
                            "address_one"= $user.StreetAddress;
                            "city"= $user.City;
                            "state"= $user.State;
                            "zip"= $user.PostalCode;
                            "country"= $user.Country;
                            "phone_business"= $user.OfficePhone;
                            "phone_business_fax"= $user.Fax;
                            "phone_mobile"= $user.telephoneNumber;
                            "language"= "fr";
                            "department"= $user.Department;
                            "manager"= "hello";
                            "is_active"= 1;
                            "groups" = @{
                            "id"="something something";
                            "name"="French"
                            
                       
                       
                       }
                        }
                      }



                }


        $jsonBody = $body | ConvertTo-Json
        Write-Output $jsonBody
        #create user 
      
          $response = Invoke-RestMethod -URI somelink/somelink  -
ContentType "application/json" -Method  Post  -Headers @{"api-token" = $token}   -Body $jsonbody
          
}
CreatePhishUser($aduser)

Thanks :)

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • It is not clear whether you added all these properties to the code you used to get these users since you don't show us that. `Get-ADUser` (which you probably used) by default only returns these properties: `DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName`, so you need to ask for all the other attributes you want. Then, it is not clear if the API accepts empty ($null) properties or not. If that is the case, you should leave those out if the attribute for that particular user is not set. – Theo May 06 '22 at 14:08
  • hey thanks for replying i found out it was because of the special characters of french that was making the api not acccepting some of the fields as when later i added encoding to it , it started working whie in postman encoding is by default so it was working there all along. – vivek gupta Jun 01 '22 at 13:37

0 Answers0