Hello Klas,
The issue occurs because at the beginning of your script, you use PowerShell variables that are not yet assigned to any values:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$tokenKPI)))
Later in your script you assign values to these variables:
$tokenKPI = 'z1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$user = "xxxx.yyyy@zzzzzz.se/token"
However, variable $base64AuthInfo has already been assigned a value earlier, and assigning the values to $tokenKPI and $user will not change the value of $base64AuthInfo. To resolve the issue, you need to place these lines before assigning a value to $base64AuthInfo, for example at the very beginning of your script:
$tokenKPI = 'z1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$user = "xxxx.yyyy@zzzzzz.se/token"
This version should work:
Import-Module Adaxes
#credentials to zendesk
$tokenKPI = 'z1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$user = "xxxx.yyyy@zzzzzz.se/token"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$tokenKPI)))
#Replace international chars
$namn= $user.name -replace 'ö','o'
$namn= $namn -replace 'å','a'
$namn= $namn -replace 'ä','a'
$namn= $namn -replace 'é','e'
$namn= $namn -replace 'ü','u'
$namn= $namn -replace '´','-'
#Check if user is student or staff
if ($Context.TargetObject.Get("CanonicalName") -match 'personal')
{
$notes = 'Personal'
}
else
{
$notes = 'Deltagare'
}
$kommando = @{
name = $namn
email = $Context.TargetObject.Get("mail")
time_zone = 'Stockholm'
external_id = $Context.TargetObject.Get("sAMAccountName")
verified = $true
notes = $notes
}
$app = @{
user = $kommando
}
$PSserver = 'servername'
$session = New-PSSession -ComputerName $PSserver
try
{
$resultat = Invoke-Command -session $session -args $app -ScriptBlock {
$app = $args[0]
$uri = "https://zzzzzzz.zendesk.com/api/v2/users.json"
Invoke-RestMethod -Uri $uri -Method get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body (ConvertTo-Json $app -Compress)
}
}
catch
{
$Context.LogMessage("No user in Zendesk created", "Information") # TODO: modify me
}
try
{
$Context.TargetObject.Put("extensionattribute3", $result.user.id)
}
catch
{
$Context.LogMessage("Can not write AD attribute 3", "Information") # TODO: modify me
}