I’m trying to add a number of aliases to an account using the API and PowerShell.
The PowerShell Code is:
$aliaslist=get-content sample.txt
$u="admin@example.com"
$p="password"
$both="$($u):$($p)"
$encodedcreds=[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($both))
$basicAuthValue="Basic $encodedCreds"
$Headers=@{
Authorization=$basicAuthValue
}
$a=Invoke-RestMethod -Uri https://box.example.com/admin/mail/aliases?format=json -Headers $Headers
$a.aliases
$alias_url="https://box.example.com/admin/mail/aliases/add"
foreach ($alias in $aliaslist)
{
$alias=$alias+"@example.com"
write-output $alias
$header=@{
"Authorization"="$basicAuthValue"
"address"="$alias"
"forwards_to"="user@example.com"
"permitted_senders"="user@example.com"
"update_if_exists"="0"
}
write-output $header
$AliasResponse=Invoke-RestMethod -Uri https://box.example.com/admin/mail/aliases/add -Method Post -Headers $header
write-output $AliasResponse
}
sample.txt contains only 2 alias on different lines, eg:
alias1
alias2
The script does the username / password thing and gets a list of current aliases (this tells me my auth seems to be working). It fails to set the alias however. The error it returns is “No email address provided”
When i output the actual headers I send, permitted_senders, forwards_to are correct and the address is correct.
alias1@example.com
Key : permitted_senders
Value : user@example.com
Name : permitted_senders
Key : Authorization
Value : Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXX
Name : Authorization
Key : update_if_exists
Value : 0
Name : update_if_exists
Key : address
Value : alias1@example.com
Name : address
Key : forwards_to
Value : user@example.com
Name : forwards_to
I’m not sure what I’m missing here?