Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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