<#
.SYNOPSIS
    A short one-line action-based description, e.g. 'Tests if a function is valid'
.DESCRIPTION
    A longer description of the function, its purpose, common use cases, etc.
.NOTES
    Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
    Specify a URI to a help page, this will show when Get-Help -Online is used.
.EXAMPLE
    Test-MyTestFunction -Verbose
    Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>

function Update-ITDSNowVMTask {
    [CmdletBinding()]
    param (
        [string]
        $SCTaskNum,

        [string]
        $CommentsToAdd,

        [switch]
        $CloseTask
    )
    
    begin {
        
    }
    
    process {
        # get current user, SCTask, Ritm
        $assignTo = Get-ServiceNowRecord -Table 'User' -Filter @('email', '-eq', ($env:username + "@nd.gov"))
        $SCTask = Get-ServiceNowRecord -Table 'Catalog Task' -ID $SCTaskNum
        $RitmNum = $SCTask.request_item.display_value
        $Ritm = Get-ServiceNowRecord -Table 'Requested Item' -ID $RitmNum -IncludeCustomVariable

        $Hostname = ($Ritm.CustomVariable | Where-Object Name -EQ 'server_name').Value

        # update SCTask description and assignment for humans
        Write-Warning "$SCTaskNum assigned to $env:username"
        Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = "VMware VM modification for $HostName"; assigned_to = $assignTo.name }

        If ($CommentsToAdd) {
            Update-ServiceNowRecord -ID $SCTask.number -Values @{comments = $CommentsToAdd } # enter comments into SCTASK
            Update-ServiceNowRecord -ID $RitmNum -Values @{comments = $CommentsToAdd } # enter comments into RITM
        }
        If ($CloseTask) {
            Update-ServiceNowRecord -ID $SCTask.number -Values @{close_notes = $CommentsToAdd; state = "Closed Complete" }
        }
    }
    
    end {
        }
}