Discussions

Ask a Question
Back to All

Cant post Absences with onw script

I created a powershell script for inserting absence Data from another application.
My Problem is, that i don't get what i did wrong, i suspect ist so kind of data type issue.

This is what i came up with:

$apiUrl = "https://api.personio.de/v1/company/time-offs"
$apiKey = "Bearer $token"
$abwesenheits_data = Get-Content -Path "D:\Test\06_result_abw_mit_persnmr_und_personioid.json" | ConvertFrom-Json

$headers = @{
    "X-Personio-App-ID" = "ej_abw"
    "accept" = "application/json"
    "authorization" = $apiKey
    "content-type" = "application/x-www-form-urlencoded"
}

foreach ($item in $abwesenheits_data){
if($item.IsFixed -eq $true){

if($item.lasLastDayHalfFromTime){
[boolean]$half_day_end = $true}else{ 
[boolean]$half_day_end = $false}

$half_day_end

if($item.FirstDayHalfFromTime){
[boolean]$half_day_start = $true}else{ 
[boolean]$half_day_start = $false}

$half_day_start
 
[int]$employee_id = $item.personio_ID
$employee_id
                                                                   #ej                      personio  
if($item.AbsenceTypeId="67"){$time_off_type_id = 2304981 }  #67	Krankheit		    Krankheit	    2304981
if($item.AbsenceTypeId="49"){$time_off_type_id = 2372925 }  #49	Urlaub			    Urlaub		    2372925
if($item.AbsenceTypeId="68"){$time_off_type_id = 2304975 }  #68	Krankheit Kind		Krankheit Kind	2304975
if($item.AbsenceTypeId="69"){$time_off_type_id = 2434481 }  #69	Elternzeit		    Elternzeit	    2434481
if($item.AbsenceTypeId="70"){$time_off_type_id = 2372925 }  #70	Betriebsferien		Urlaub		    2372925
if($item.AbsenceTypeId="71"){$time_off_type_id = 2476057 }  #71	Sonderurlaub		Sonderurlaub	2476057

$time_off_type_id

[string]$start_date = $item.From | ForEach-Object { [System.DateTime]::ParseExact($_, "yyyy-MM-ddTHH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture).ToString("yyyy-MM-dd") }
[string]$end_date = $item.To | ForEach-Object { [System.DateTime]::ParseExact($_, "yyyy-MM-ddTHH:mm:ss", [System.Globalization.CultureInfo]::InvariantCulture).ToString("yyyy-MM-dd") }

$start_date
$end_date

[string]$comment = "test"
$comment

[boolean]$skip_approval = $true
$skip_approval



$body = @{
    "half_day_start" = $half_day_start
    "half_day_end" = $half_day_end
    "employee_id" = $employee_id
    "time_off_type_id" = $time_off_type_id
    "start_date" = $start_date
    "end_date" = $end_date
    "comment" = $comment
    "skip_approval" = $true
    }

    $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body

} 
}

And this is my error mesage from ps:

Invoke-RestMethod : Der Remoteserver hat einen Fehler zurückgegeben: (400) Ungültige Anforderung.
In Zeile:79 Zeichen:17
+ ... $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $hea ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-Res 
   tMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMe 
   thodCommand

Some Help would be apreciated.