Skip to content

Instantly share code, notes, and snippets.

@mendel129
Last active February 22, 2017 12:53
Show Gist options
  • Save mendel129/d7f337d9fb6b4979a7ab to your computer and use it in GitHub Desktop.
Save mendel129/d7f337d9fb6b4979a7ab to your computer and use it in GitHub Desktop.
Quick and dirty PowerShell interface to 3D-ICT timesheet application
# get all mondays, wednesdays and fridays of december
[int]$Month = '12'
[int]$Year = '2015'
# days not worked in december
$exceptions = @(9,11,13)
$alldays = @()
0..31 | ForEach-Object -Process {
$evaldate = (Get-Date -Year $Year -Month $Month -Day 1).AddDays($_)
if ($evaldate.Month -eq $Month)
{
if (($evaldate.DayOfWeek -eq "Monday") -or ($evaldate.DayOfWeek -eq "Wednesday") -or ($evaldate.DayOfWeek -eq "Friday") -and ($exceptions -notcontains $evaldate.day))
{
$alldays += $evaldate.Day
}
}
}
# show overview of days worked
$alldays
# get token and create session variable
$loginpage = invoke-webrequest -uri "https://timesheets.3d-apps.be/login" -sessionvariable ws
$token = ($loginpage.inputfields | ?{$_.name -eq "_token"}).value
# url encode your password, 3dapps can't handle the '&' sign in your password
$postdata = New-Object -TypeName PSObject -Property @{
token = $token;
email = "emailaddress";
password = "urlencodedpassword"
}
# login to 3D apps
$headervals = @{'Referer'='https://timesheets.3d-apps.be/login';'Content-Type'='application/x-www-form-urlencoded'}
$postdata = "_token=" + $postdata.token + "&email=" + $postdata.email + "&password=" + $postdata.password
$jsonpostdata = $postdata |convertto-json
$login = invoke-webrequest -uri "https://timesheets.3d-apps.be/login" -Method Post -Body $postdata -Headers $headervals -websession $ws
$cookietoken = ($ws.cookies.getcookies("https://timesheets.3d-apps.be") | ?{$_.name -eq "xsrf-token"}).value
# find the timesheet code
$headervals = @{'Referer'='https://timesheets.3d-apps.be/';'DNT'='1';'Content-Type'='application/json;charset=utf-8';'X-Requested-With'='XMLHttpRequest';'X-XSRF-TOKEN'="$cookietoken"}
$var = "https://timesheets.3d-apps.be/api/v1/tsm/sheets?month=$year-$month-01"
$result = Invoke-RestMethod -Method Get -Uri $var -websession $ws -Headers $headervals
$TSCode = ($result | ?{$_.customer_name -eq "customer name"}).id
# or change to the amount of hours you worked
$timearr = @{"time"="8"}
Foreach($day in $alldays)
{
$postdata = New-Object -TypeName PSObject -Property @{
date="$year-$month-$($day-1)T23:00:00.000Z";
time=$timearr ;
tariff_id="1";
entry_date="$year-$month-$day";
sheet_id=$TSCode;
order="1";
weekend="False"
}
$postParams = ConvertTo-Json $postdata -Compress:$true
$cookietoken = ($ws.cookies.getcookies("https://timesheets.3d-apps.be") | ?{$_.name -eq "xsrf-token"}).value
$Decode = [System.Web.HttpUtility]::UrlDecode($cookietoken)
$headervals = @{'Referer'='https://timesheets.3d-apps.be/timesheets/1774';'DNT'='1';'Content-Type'='application/json;charset=utf-8';'X-Requested-With'='XMLHttpRequest';'X-XSRF-TOKEN'="$Decode"}
$jsonpage = "https://timesheets.3d-apps.be/api/v1/tsm/entries"
#login, get xsfr token and store in cookie
$var = Invoke-RestMethod -Method Post -Uri $jsonpage -Body $postParams -Headers $headervals -websession $ws
#post data
}
write-output "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment