<#
.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-ITDSNowVMTaskDescription {
    [CmdletBinding()]
    param (
        
    )
    
    begin {
        
    }
    
    process {
        <# Server Builds
        Write-Verbose -Message "Start Server Builds"
        $Filter = @('assignment_group', '-like', 'NDIT-Server Build Automation'), '-and', @('short_description', '-eq', 'Automated Server Build Task for Windows Virtual Machine'), '-and', @('state', '-eq', '2')
        $OpenTasks = Get-ServiceNowRecord -Table 'Catalog Task' -Filter $Filter | Sort-Object Number

        ForEach ($OpenTask in $OpenTasks) {
            # get SCTask, Ritm
            $SCTaskNum = $OpenTask.number
            $SCTask = Get-ServiceNowRecord -Table 'Catalog Task' -ID $SCTaskNum
            $RitmNum = $SCTask.request_item.display_value
            $Ritm = Get-ServiceNowRecord -Table 'Requested Item' -ID $RitmNum -IncludeCustomVariable -WarningAction SilentlyContinue
            
            $ComputerName = ($Ritm.CustomVariable | Where-Object Name -EQ host_name).Value
            $OperatingSystem = ($Ritm.CustomVariable | Where-Object Name -EQ operating_system).Value
            switch (($Ritm.CustomVariable | Where-Object Name -EQ target_platform).Value) {
                'azure' { $target_platform = "Azure" }
                'vmware' { $target_platform = "VMware" }
            }

            # update short description
            $shortdescription = "$target_platform $OperatingSystem VM Build for $ComputerName"
            If ( ($RITM.CustomVariable | Where-Object Name -EQ dr_protection).Value -ne 'No DR') {
                $shortdescription += ", with SRM protection"
            }
            Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = $shortdescription; }
        }
        Write-Verbose -Message "End Server Builds"
#>
        <# VM Modifications
        Write-Verbose -Message "Start VM Modifications"
        $Filter = @('assignment_group', '-like', 'NDIT-Server Build Automation'), '-and', @('short_description', '-eq', 'Upgrade/Code Deployment'), '-and', @('state', '-eq', '1')
        $OpenTasks = Get-ServiceNowRecord -Table 'Catalog Task' -Filter $Filter | Sort-Object Number
        
        ForEach ($OpenTask in $OpenTasks) {
            Write-Verbose -Message "Start $OpenTask.number"
            # get SCTask, Ritm
            $SCTaskNum = $OpenTask.number
            $SCTask = Get-ServiceNowRecord -Table 'Catalog Task' -ID $SCTaskNum
            $RitmNum = $SCTask.request_item.display_value
            $Ritm = Get-ServiceNowRecord -Table 'Requested Item' -ID $RitmNum -IncludeCustomVariable
            
            $ComputerName = ($Ritm.CustomVariable | Where-Object Name -EQ server_name).Value.tolower()

            # update short description
            $shortdescription = "VM Modification for $ComputerName"
            Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = $shortdescription; }
            Write-Verbose -Message "End $OpenTask.number"
        }
        Write-Verbose -Message "End VM Modifications"
#>
        # VM removal / retire
        Write-Verbose -Message "Start Remove Server description updates"
        #$Filter = @('assignment_group', '-like', 'NDIT-Server Build Automation'), '-and', @('short_description', '-like', 'Retire Server'), '-and', @('state', '-eq', '1')
        $Filter = "active=true^short_descriptionSTARTSWITHRemove Server: "
        #$OpenTasks = Get-ServiceNowRecord -Table 'Catalog Task' -Filter $Filter | Sort-Object Number
        $OpenTasks = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Filter $Filter -IncludeTotalCount | Sort-Object Number
        $AllRitms = [System.Collections.ArrayList]@()

        ForEach ($OpenTask in $OpenTasks) {
            Write-Verbose -Message ("Start " + $OpenTask.number)
            # get SCTask, Ritm
            $SCTaskNum = $OpenTask.number.value
            $SCTask = $OpenTask
            If ($AllRitms | Where-Object sys_id -EQ $SCTask.request_item.value) {
                $Ritm = $AllRitms | Where-Object sys_id -EQ $SCTask.request_item.display_value
            }
            Else {
                $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $SCTask.request_item.display_value -IncludeVariableSet
                $null = $AllRitms.Add($Ritm)
            }
            
            $short_description_hostname = $SCTask.short_description.display_value.split(' ')[2]
            $Ci = Get-ITDServiceNowRecord -Table cmdb_ci -Filter ("name=" + $short_description_hostname)

            $HostName = $Ci.Name.display_value
            $FQDN = $Ci.FQDN.display_value

            # determine if vmware, azure, or physical
            switch ($Ci.model_id.display_value) {
                { $_ -like "*VMware*" } { $hardware_platform = "VMware"; $hardware_type = 'Virtual Machine' }
                { $_ -like "*Microsoft Virtual Machine*" } { $hardware_platform = "Azure"; $hardware_type = 'Virtual Machine' }
                { $_ -like "*HP*" } { $hardware_platform = 'HPE'; $hardware_type = 'Physical' }
                default { $hardware_platform = 'Unknown'; $hardware_type = 'Other' }
            }

            # determine if windows, rhel, or other
            switch ($Ci.sys_class_name.display_value) {
                'Linux Server' { $OS = 'Linux' }
                'Windows Server' { $OS = 'Windows' }
                'Default' { $OS = 'Other' }
            }

            $short_description_new = "$hardware_platform $OS Removal for "
            If ($FQDN) {
                $short_description_new += $FQDN
            }
            Else {
                $short_description_new += $ComputerName
            }
            #Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = $shortdescription; }
            Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{short_description = $short_description_new}
            Write-Verbose -Message ("End " + $OpenTask.number.display_value)
        }
        Write-Verbose -Message "End VM removal/retire"

        <# Other Server Requests
        Write-Verbose -Message "Start Other Server"
        $Filter = @('assignment_group', '-like', 'NDIT-Server Build Automation'), '-and', @('short_description', '-eq', 'Other Server Request'), '-and', @('state', '-eq', '1')
        $OpenTasks = Get-ServiceNowRecord -Table 'Catalog Task' -Filter $Filter | Sort-Object Number

        ForEach ($OpenTask in $OpenTasks) {
            Write-Verbose -Message "Start $OpenTask.number"
            # get SCTask, Ritm
            $SCTaskNum = $OpenTask.number
            $SCTask = Get-ServiceNowRecord -Table 'Catalog Task' -ID $SCTaskNum
            $RitmNum = $SCTask.request_item.display_value
            $Ritm = Get-ServiceNowRecord -Table 'Requested Item' -ID $RitmNum -IncludeCustomVariable
            
            $ComputerName = ($Ritm.CustomVariable | Where-Object Name -EQ server_name).Value
            $OperatingSystem = ($Ritm.CustomVariable | Where-Object Name -EQ operating_system).Value

            # update short description
            $shortdescription = "Other Server Request for $ComputerName"
            Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = $shortdescription; }
            Write-Verbose -Message "End $OpenTask.number"
        }
        Write-Verbose -Message "End Other Server"
        #>
    }
    
    end {
        <#
        $postParams = [PSCustomObject]@{
            AutomationName = "Infra-VMware";
            Action         = 'Change';
            Units          = 3;
            Platform       = 'ServiceNow-Overhead';
        }
    
        Invoke-RestMethod -Uri http://itdnettools.nd.gov/services/automation-tracking.py -Method POST -Body ($postParams | ConvertTo-Json)
#>
        
    }
}