Param Parameter Mandatory true Position HelpMessage Your domain user n

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Param(
[Parameter(Mandatory=$true, Position=0, HelpMessage="Your domain user name (without domain name).")]
[string]$username,
[Parameter(Mandatory=$true, Position=1, HelpMessage="Your domain account password.")]
[SecureString]$password
)
function UnixTimeStampToDateTime([long]$unixTimeStamp)
{
$UnixEpoch = New-Object DateTime @(1970, 1, 1, 0, 0, 0, 0, "Utc")
return $UnixEpoch.AddMilliseconds($unixTimeStamp).ToLocalTime()
}
function GetIssueFromUrl([string]$url) {
return ($url -cReplace ".*/(MKT-\d+)/.*", '$1')
}
function IsToday($date) {
return (UnixTimeStampToDateTime $date).Date -eq [DateTime]::Today
}
$me = $username
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
Invoke-RestMethod `
-Uri "http://youtrack.crossmarket.across.lan/rest/user/login" `
-Body @{login="acrosssys\$me"; password=$pass} `
-Method Post `
-SessionVariable sess `
-ErrorAction Stop | Out-Null
if(-not $?) {
throw "Could not login."
}
$ticketsResponse = Invoke-RestMethod `
-Uri "http://youtrack.crossmarket.across.lan/rest/issue?filter=for%3A+$me+updated%3AToday" `
-Method Get `
-WebSession $sess
$ids = $ticketsResponse.issueCompacts.issue.id
$allWorkItems = $ids `
| % { Invoke-RestMethod -Uri "http://youtrack.crossmarket.across.lan/rest/issue/$_/timetracking/workitem" -Method Get -WebSession $sess } `
| ? { $_.workitems } `
| % { $_.workitems.workitem }
$myWorkItems = @( $allWorkItems | ? { $_.author.login -eq $me -and (IsToday $_.date) } )
Write-Host "Tickets assigned to $me and updated $(get-date -Format d):"
$myWorkItems | %{
New-Object psobject -Property @{
Issue=GetIssueFromUrl $_.url
Duration=[timespan]::FromMinutes($_.duration) }} |
Format-Table -AutoSize -HideTableHeaders -GroupBy "Issue" -Property "Duration"
$totalDuration = ($myWorkItems.duration | measure -Sum).Sum
Write-Host "Total duration: $([timespan]::FromMinutes( $totalDuration ))"