OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Adding teammates using API through Powershell

  • Thread starter Thread starter Jako
  • Start date Start date
J

Jako

Guest
I want to automate a process of adding new teammates to sendgrid. I manually used to add them via CURL which worked. To fully automate this I would need to achieve the same via Powershell. I converted the curl over to invoke-restmethod and it is throwing invalid JSON error. After spending a day it seems to me the body is exactly the same as the CURL request but it still fails. Can anyone help me with this? Below is the code I'm using:

Code:
$uri = "https://api.sendgrid.com/v3/teammates"
$emails = "[email protected]", "[email protected]"
$subusers = "subuser1", "subuser2"
$sendgridApiKey= "full_admin_key"

foreach ($email in $emails){
    foreach ($subuser in $subusers){

        $headers = @{"Authorization" = "Bearer " + $sendgridApiKey; "Content-Type"=          "application/json"; "on-behalf-of" = $subuser}
        $Body = @{
            email= "$email"
            scopes="alerts.create","alerts.read"
            is_admin="false"} | ConvertTo-Json

        echo "Inviting $email to subuser $subuser"
        #echo Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -Body $body
        Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -Body $body
    }
}

Original curl:

Code:
declare -a emails= ([email protected])
declare -a subusers= (realsubuser1 realsubuser2)
set -a 
sendgridApiKey="RealOneInScript"
set +a
for e in ${emails[@]}
do
    for s in ${subusers[@]}
    do
    
        echo "Inviting $e to subuser $s"
        #invite user, scope is applicable to marketing users
        curl -X "POST" "https://api.sendgrid.com/v3/teammates" -H "on-behalf-of:$s"  -H "Authorization: Bearer $sendgridApiKey" -H "Content-Type: application/json" -d \
        '{
        "email":"'$e'","validations.email.read","validations.email.create","email_testing.read", "2fa_required"],
        "is_admin":false
        }'
    sleep 10 
    done
done
<p>I want to automate a process of adding new teammates to sendgrid. I manually used to add them via CURL which worked. To fully automate this I would need to achieve the same via Powershell. I converted the curl over to invoke-restmethod and it is throwing invalid JSON error. After spending a day it seems to me the body is exactly the same as the CURL request but it still fails. Can anyone help me with this? Below is the code I'm using:</p>
<pre><code>$uri = "https://api.sendgrid.com/v3/teammates"
$emails = "[email protected]", "[email protected]"
$subusers = "subuser1", "subuser2"
$sendgridApiKey= "full_admin_key"

foreach ($email in $emails){
foreach ($subuser in $subusers){

$headers = @{"Authorization" = "Bearer " + $sendgridApiKey; "Content-Type"= "application/json"; "on-behalf-of" = $subuser}
$Body = @{
email= "$email"
scopes="alerts.create","alerts.read"
is_admin="false"} | ConvertTo-Json

echo "Inviting $email to subuser $subuser"
#echo Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -Body $body
Invoke-RestMethod -Uri $Uri -Method Post -Headers $headers -Body $body
}
}
</code></pre>
<p>Original curl:</p>
<pre><code>declare -a emails= ([email protected])
declare -a subusers= (realsubuser1 realsubuser2)
set -a
sendgridApiKey="RealOneInScript"
set +a
for e in ${emails[@]}
do
for s in ${subusers[@]}
do

echo "Inviting $e to subuser $s"
#invite user, scope is applicable to marketing users
curl -X "POST" "https://api.sendgrid.com/v3/teammates" -H "on-behalf-of:$s" -H "Authorization: Bearer $sendgridApiKey" -H "Content-Type: application/json" -d \
'{
"email":"'$e'","validations.email.read","validations.email.create","email_testing.read", "2fa_required"],
"is_admin":false
}'
sleep 10
done
done
</code></pre>
Continue reading...
 

Latest posts

Top