eTask_Powershell.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 59621109-aabe-4545-aaae-c0594c466257
 
.AUTHOR sungnguyen@appvity.com
 
.COMPANYNAME APPVITY
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Test
 
#>
 

Param()


#
Add-Type -AssemblyName System.Windows.Forms


# Get cookie
function Get-GraphOauthCookie {
    param
    (
        [Parameter(Mandatory = $false,
                   ValueFromPipelineByPropertyName = $true)]
        [Alias('URL')]
        [string]$BaseURL = 'https://teams-stag.appvity.com/login?returnCookie=1'
    )
    Process {
        Write-Verbose "URL: '$BaseURL'"
        $Params = @{
            TypeName = 'System.Windows.Forms.Form'
            Property = @{
                Width = 440
                Height = 640
            }
        }
        $Form = New-Object @Params
        $Params = @{
            TypeName = 'System.Windows.Forms.WebBrowser'
            Property = @{
                Width = 420
                Height = 600
                Url = $BaseURL
            }
        }
        $Web = New-Object @Params
        $DocumentCompleted_Script = {
            #Write-Host '-------------Url.AbsoluteUri------------------'
            #Write-Host $web.Url.AbsoluteUri
            if ($web.Url.AbsoluteUri -match "error=[^&]*|c=[^&]*"){
                $form.Close()
            }
        }
        # ScriptErrorsSuppressed must be $false or AD FS tenants will fail on Windows Integrated Authentication pages
        $web.ScriptErrorsSuppressed = $false
        $web.Add_DocumentCompleted($DocumentCompleted_Script)
        $form.Controls.Add($web)
        $form.Add_Shown({ $form.Activate() })
        [void]$form.ShowDialog()

        $QueryOutput = $web.Url.Query #[System.Web.HttpUtility]::ParseQueryString($web.Url.Query)#
        $QueryOutput = $QueryOutput.replace("?c=","")#graphNodeCookie=

        [void]$form.Close()
        [void]$Web.Dispose()
        [void]$Form.Dispose()

        return $QueryOutput
    }
}

function Get-Projects {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$Cookie
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }
    $Url = $Url.TrimEnd('/') + '/odata/projects'

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json

    return $Content.value
}

function Get-Phases {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$SourceId,
        [string]$Cookie
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }
    if(!$SourceId){
        Write-Host "SourceId not found." -F red
        return
    }
    

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $ret = Get-Projects -Url $Url -Cookie $cookie
    $SourceName = $ret | Where {$_._id -eq $SourceId} | Select source

    $Url = $Url.TrimEnd('/') + '/api/tasks/getPhase/' + $SourceName.source + '/' + $SourceId

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json
    #$Content.value | Format-List
    #Write-Host $Content.value | ConvertFrom-Json

    return $Content
}

function Get-Bucket {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$SourceId,
        [string]$Cookie,
        [string]$PhaseId
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }
    if(!$SourceId){
        Write-Host "SourceId not found." -F red
        return
    }
    

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $ret = Get-Projects -Url $Url -Cookie $cookie
    $SourceName = $ret | Where {$_._id -eq $SourceId} | Select source

    $Url = $Url.TrimEnd('/') + '/api/tasks/getBucket/' + $SourceName.source + '/' + $SourceId

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json
    #$Content.value | Format-List
    #Write-Host $Content.value | ConvertFrom-Json

    if($PhaseId) {
        return $Content | Where {$_.sprintId -eq $PhaseId}
    }
    return $Content
}

function Get-Users {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$SourceId,
        [string]$Cookie,
        [string]$Search
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }
    if(!$SourceId){
        Write-Host "SourceId not found." -F red
        return
    }
    if(!$Search){
        Write-Host "Search not found." -F red
        return
    }
    

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $ret = Get-Projects -Url $Url -Cookie $cookie
    $SourceName = $ret | Where {$_._id -eq $SourceId} | Select source

    $Url = $Url.TrimEnd('/') + '/api/tasks/getUsers/' + $SourceName.source + '/' + $SourceId +'?search=' + $Search

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json
    #$Content.value | Format-List
    #Write-Host $Content.value | ConvertFrom-Json

    return $Content
}

# Task
function Get-Tasks {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$t, 
        [bool]$count,
        [int]$top,
        [string]$orderby,
        [string]$Title,
        [string]$Phase,
        [string]$Bucket,
        [string]$Status,
        [string]$Cookie
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }

    $Url = $Url.TrimEnd('/') + '/api/tasks?'
    $Url = $Url + 't=' + $t
    if($count){
        $Url = $Url + '&$count=' + $count
    }
    if($top){
        $Url = $Url + '&$top=' + $top
    }
    if($orderby){
        $Url = $Url + '&$orderby=' + $orderby
    }
    if($Title){
        $Url = $Url + '&$Title=' + $Title
    }
    if($Phase){
        $Url = $Url + '&$Phase=' + $Phase
    }
    if($Bucket){
        $Url = $Url + '&$Bucket=' + $Bucket
    }
    if($Status){
        $Url = $Url + '&$Status=' + $Status
    }

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json
    #$Content.value | Format-List
    #Write-Host $Content.value | ConvertFrom-Json

    return $Content.value
}

function Get-TaskDetails {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$Cookie,
        [string]$Id
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }

    $Url = $Url.TrimEnd('/') + '/api/tasks/' + $Id + '/details'

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        Write-Host '-------------cookie------------------'
        Write-Host $cookie
    }

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json
    #$Content.value | Format-List
    #Write-Host $Content.value | ConvertFrom-Json

    return $Content
}

function Create-Task {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        $BodyObject,
        [string]$Title,
        [string]$AssignedTo,
        [string]$Attachments,
        [string]$Body,
        [string]$Bucket,
        [string]$Complete,
        [string]$CompletedDate,
        [string]$DueDate,
        [string]$Effort,
        [string]$Name,
        [string]$Owner,
        [string]$Phase,
        [string]$Priority,
        [string]$RelatedItems,
        [string]$StartDate,
        [string]$Status,
        [string]$Source,
        [string]$Cookie
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }

    if($BodyObject) {
        $object = $BodyObject | ConvertFrom-Json

        if($object.Title) {$Title = $object.Title}
        if($object.AssignedTo) {$AssignedTo = $object.AssignedTo}
        if($object.Attachments) {$Attachments = $object.Attachments}
        if($object.Body) {$Body = $object.Body}
        if($object.Bucket) {$Bucket = $object.Bucket}
        if($object.Complete) {$Complete = $object.Complete}
        if($object.CompletedDate) {$CompletedDate = $object.CompletedDate}
        if($object.DueDate) {$DueDate = $object.DueDate}
        if($object.Effort) {$Effort = $object.Effort}
        if($object.Name) {$Name = $object.Name}
        if($object.Owner) {$Owner = $object.Owner}
        if($object.Phase) {$Phase = $object.Phase}
        if($object.Priority) {$Priority = $object.Priority}
        if($object.RelatedItems) {$RelatedItems = $object.RelatedItems}
        if($object.StartDate) {$StartDate = $object.StartDate}
        if($object.Status) {$Status = $object.Status}
        if($object.Source) {$Source = $object.Source}
    }
    
    #validation
    if(!$Title) {
        Write-Host "Title not found." -F red
        return
    }
    if(!$Source) {
        Write-Host "Source not found." -F red
        return
    }
    if(!$Priority) {
        Write-Host "Priority not found." -F red
        return
    }
    if(!$Status) {
        Write-Host "Status not found." -F red
        return
    }



    $Arr = @{
        name = $Title
        status = $Status
        body = $Body
        source = $Source
        priority = $Priority
        startDate = $StartDate
        dueDate = $DueDate
        attachments = @()
        bucket = ''
        bucketName = ''
        complete = '0'
        completedDate = ''
        effort = ''
        owner = ''
        phase = ''
        phaseName = ''
        projectId = '5d15bd83d9570032862cafe6'
        relatedItems = @()
        assignedTo = @()

    } | ConvertTo-Json

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Url = $Url.TrimEnd('/') + '/odata/tasks'

    $Params = @{
        Uri = $Url
        Method = 'POST'
        Headers = $hd
        Body = $Arr
    }

    try {
        Invoke-WebRequest @Params -WebSession $session
    }
    catch{
        Write-Error $_.Exception.Message
    }
}

function Update-Task {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$t,
        [string]$ProjectId,
        $Id,
        $BodyObject,
        [string]$Title,
        [string]$AssignedTo,
        [string]$Attachments,
        [string]$Body,
        [string]$Bucket,
        [string]$Complete,
        [string]$CompletedDate,
        [string]$DueDate,
        [string]$Effort,
        [string]$Name,
        [string]$Owner,
        [string]$Phase,
        [string]$Priority,
        [string]$RelatedItems,
        [string]$StartDate,
        [string]$Status,
        [string]$Source,
        [string]$Cookie
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #get task detail
    $taskDetail = Get-TaskDetails -Url $Url -t $t -Id $Id

    $BucketName = ''
    $PhaseName = ''

    if(!$BodyObject) {
        if(!$Title) {$Title = $taskDetail.name}
        if(!$AssignedTo) {$AssignedTo = $taskDetail.assignedTo}
        if(!$Attachments) {$Attachments = $taskDetail.attachments}
        if(!$Body) {$Body = $taskDetail.body}
        if(!$Bucket) {$Bucket = $taskDetail.bucket}
        if(!$Complete) {$Complete = $taskDetail.complete}
        if(!$CompletedDate) {$CompletedDate = $taskDetail.completedDate}
        if(!$DueDate) {$DueDate = $taskDetail.dueDate}
        if(!$Effort) {$Effort = $taskDetail.effort}
        if(!$Owner) {$Owner = $taskDetail.owner}
        if(!$Phase) {$Phase = $taskDetail.phase}
        if(!$Priority) {$Priority = $taskDetail.priority}
        if(!$RelatedItems) {$RelatedItems = $taskDetail.relatedItems}
        if(!$StartDate) {$StartDate = $taskDetail.startDate}
        if(!$Status) {$Status = $taskDetail.status}
        if(!$Source) {$Source = $taskDetail.source}
    }

    if($BodyObject) {
        $object = $BodyObject | ConvertFrom-Json

        if($object.Title) {$Title = $object.Title}
        if($object.AssignedTo) {$AssignedTo = $object.AssignedTo}
        if($object.Attachments) {$Attachments = $object.Attachments}
        if($object.Body) {$Body = $object.Body}
        if($object.Bucket) {$Bucket = $object.Bucket}
        if($object.Complete) {$Complete = $object.Complete}
        if($object.CompletedDate) {$CompletedDate = $object.CompletedDate}
        if($object.DueDate) {$DueDate = $object.DueDate}
        if($object.Effort) {$Effort = $object.Effort}
        if($object.Name) {$Name = $object.Name}
        if($object.Owner) {$Owner = $object.Owner}
        if($object.Phase) {$Phase = $object.Phase}
        if($object.Priority) {$Priority = $object.Priority}
        if($object.RelatedItems) {$RelatedItems = $object.RelatedItems}
        if($object.StartDate) {$StartDate = $object.StartDate}
        if($object.Status) {$Status = $object.Status}
        if($object.Source) {$Source = $object.Source}
    }
    
    <#
    #validation
    if(!$Title) {
        Write-Host "Title not found." -F red
        return
    }
    if(!$Source) {
        Write-Host "Source not found." -F red
        return
    }
    if(!$Priority) {
        Write-Host "Priority not found." -F red
        return
    }
    if(!$Status) {
        Write-Host "Status not found." -F red
        return
    }
    #>



    $Arr = @{
        name = $Title
        status = $Status
        body = $Body
        source = $Source
        priority = $Priority
        startDate = $StartDate
        dueDate = $DueDate
        attachments = $Attachments
        bucket = $Bucket
        bucketName = $BucketName
        complete = $Complete
        completedDate = $CompletedDate
        effort = $Effort
        owner = $Owner
        phase = $Phase
        phaseName = $PhaseName
        projectId = '5d15bd83d9570032862cafe6'
        relatedItems = @()
        assignedTo = @()

    } | ConvertTo-Json

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Url = $Url.TrimEnd('/') + '/odata/tasks(' + $Id + ')'

    $Params = @{
        Uri = $Url
        Method = 'PATCH'
        Headers = $hd
        Body = $Arr
    }

    try {
        Invoke-WebRequest @Params -WebSession $session
    }
    catch{
        Write-Error $_.Exception.Message
    }
}

function Delete-Task {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$Id, 
        [string]$Cookie
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }
    if(!$Id){
        Write-Host "Id not found." -F red
        return
    }

    $Url = $Url.TrimEnd('/') + '/odata/tasks(' + $Id + ')'

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Params = @{
        Uri = $Url
        Method = 'DELETE'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json

    return $Content
}
#

#Bug
function Get-Bugs {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$t, 
        [bool]$count,
        [int]$top,
        [string]$orderby,
        [string]$Title,
        [string]$Phase,
        [string]$Bucket,
        [string]$Status
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }
    if(!$t){
        Write-Host "t not found." -F red
        return
    }

    $Url = $Url.TrimEnd('/') + '/api/bugs?'
    $Url = $Url + 't=' + $t
    if($count){
        $Url = $Url + '&$count=' + $count
    }
    if($top){
        $Url = $Url + '&$top=' + $top
    }
    if($orderby){
        $Url = $Url + '&$orderby=' + $orderby
    }
    if($Title){
        $Url = $Url + '&$Title=' + $Title
    }
    if($Phase){
        $Url = $Url + '&$Phase=' + $Phase
    }
    if($Bucket){
        $Url = $Url + '&$Bucket=' + $Bucket
    }
    if($Status){
        $Url = $Url + '&$Status=' + $Status
    }

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #cookie
    $cookie = Get-GraphOauthCookie
    #Write-Host '-------------cookie------------------'
    #Write-Host $cookie

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json
    #$Content.value | Format-List
    #Write-Host $Content.value | ConvertFrom-Json

    return $Content.value
}

function Get-BugDetails {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$Cookie,
        [string]$Id
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }

    $Url = $Url.TrimEnd('/') + '/api/bugs/' + $Id + '/details'

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Params = @{
        Uri = $Url
        Method = 'GET'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json
    #$Content.value | Format-List
    #Write-Host $Content.value | ConvertFrom-Json

    return $Content
}

function Delete-Bug {
    param( 
        [string]$Url,
        [string]$ChannelId = "19:e2599066fc7f4faaada1ffa4632adf52@thread.skype",
        [string]$EntityId = "eTask.ea217f5c-2e1f-4406-671b-194321dfb776",
        [string]$GroupId = "7243322b-db76-422e-a964-ae2bef25eee0",
        [string]$TeamId = "19:ca7641c904da4fdf92820722ff6489fc@thread.skype",
        [string]$Domain = "teams-stag.appvity.com",
        [string]$Id, 
        [string]$Cookie
    )

    if(!$Url){
        Write-Host "Url not found." -F red
        return
    }
    if(!$Id){
        Write-Host "Id not found." -F red
        return
    }

    $Url = $Url.TrimEnd('/') + '/odata/bugs(' + $Id + ')'

    #header
    $hd = New-Object 'System.Collections.Generic.Dictionary[String,String]'
    $hd.Add("x-appvity-channelId",$ChannelId)
    $hd.Add("x-appvity-entityId",$EntityId)
    $hd.Add("x-appvity-groupId",$GroupId)
    $hd.Add("x-appvity-teamid",$TeamId)
    $hd.Add("Content-Type","application/json")

    #cookie
    $cookie = $Cookie
    if(!$cookie){
        $cookie = Get-GraphOauthCookie
        #Write-Host '-------------cookie------------------'
        #Write-Host $cookie
    }

    #session
    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
    $ck = New-Object System.Net.Cookie 
    $ck.Name = "graphNodeCookie"
    $ck.Value = $cookie
    $ck.Path = "/"
    $ck.Domain = $Domain
    $session.Cookies.Add($ck);

    $Params = @{
        Uri = $Url
        Method = 'DELETE'
        Headers = $hd
    }
    $Result = Invoke-WebRequest @Params -WebSession $session
    $Content = $Result.Content | ConvertFrom-Json

    return $Content
}
#

#---------------------------------------------------------------------------------------------------------------------------------------

#Write-Host '-------------Projects------------------'
#$ret = Get-Projects -Url 'https://teams-stag.appvity.com'
#Format-Table -AutoSize -InputObject $ret

#Write-Host '-------------phase------------------'
#$ret = Get-Phases -Url 'https://teams-stag.appvity.com' -SourceId '5d15bd83d9570032862cafe6'
#Format-Table -AutoSize -InputObject $ret

#Write-Host '-------------bucket------------------'
#$ret = Get-Bucket -Url 'https://teams-stag.appvity.com' -SourceId '5d15bd83d9570032862cafe6' -PhaseId '103'
#Format-Table -AutoSize -InputObject $ret

#Write-Host '-------------users------------------'
#$ret = Get-Users -Url 'https://teams-stag.appvity.com' -SourceId '5d15bd83d9570032862cafe6' -Search 'hi'
#Format-Table -AutoSize -InputObject $ret

#Write-Host '-------------Create Task------------------'
#Create-Task -Url 'https://teams-stag.appvity.com' -Title 'task 2' -Status 'Not Started' -Source 'Jira' -Priority 'Normal'

#Write-Host '-------------Update Task------------------'
#Update-Task -Url 'https://teams-stag.appvity.com' -Title 'task 2 test'

#Write-Host '-------------Delete Task------------------'
#$ret = Delete-Task -Url 'https://teams-stag.appvity.com' -Id '5d9ea223a0cf4de4be90aac0'

#Write-Host '-------------Tasks------------------'
#$ret = Get-Tasks -Url 'https://teams-stag.appvity.com'
#Format-Table -AutoSize -InputObject $ret -Property name,status,_id

#Write-Host '-------------Task detail------------------'
#$ret = Get-TaskDetails -Url 'https://teams-stag.appvity.com' -Id '5d9ea223a0cf4de4be90aac0'
#$ret
#Format-Table -AutoSize -InputObject $ret -Property name,status,_id,bucket



#Write-Host '-------------Bugs------------------'
#$ret = Get-Bugs -Url 'https://teams-stag.appvity.com' -t '1569835500838'
#Format-Table -AutoSize -InputObject $ret -Property name,status,_id

#Write-Host '-------------Bug detail------------------'
#$ret = Get-BugDetails -Url 'https://teams-stag.appvity.com' -Id '5d95bc0a7662876213f246bf'
#$ret
#Format-Table -AutoSize -InputObject $ret -Property name,status,_id,bucket

#Write-Host '-------------Delete Bug------------------'
#$ret = Delete-Bug -Url 'https://teams-stag.appvity.com' -Id '5d95bc0a7662876213f246bf'