param(
    [string]
    $SCTaskNum
)

#New-ServiceNowSession -Url 'northdakota.service-now.com' -Credential $Secret:SNowVMCred -Verbose
New-ITDServiceNowSession -Environment Production -Credential $Secret:SNowVMCred

#$Filter = @('assignment_group', '-like', 'NDIT-Server Build Automation'), '-and', @('short_description', '-like', 'VMware Windows Removal for'), '-and', @('state', '-eq', '1')
$Filter = "active=true^short_descriptionLIKEWindows Removal for "
#sysparm_query%3Dactive=true^short_descriptionLIKEWindows Removal for
#$OpenTasks = Get-ServiceNowRecord -Table 'Catalog Task' -Filter $Filter -WarningAction SilentlyContinue | Sort-Object Number

$OpenTasks = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Filter $Filter -IncludeTotalCount | Sort-Object {$_.Number.value}

If ($PSBoundParameters.ContainsKey("SCTaskNum")) {
    Write-Verbose -Message "SCTaskNum parameter found, value is $SCTaskNum"
    $OpenTasks = $OpenTasks | Where-Object {$_.number.value -EQ $SCTaskNum}
}
$AllRitms = [System.Collections.ArrayList]@()
Write-Verbose -Message ("Number of OpenTasks is " + @($OpenTasks).count) -Verbose

Connect-ITDvCenter -Credential $Secret:svcitdiaasauto

ForEach ($OpenTask in $OpenTasks) {
    # get SCTask, Ritm
    $SCTask = $OpenTask
    $SCTaskNum = $OpenTask.number.display_value
    Write-Verbose -Message ("Start " + $SCTaskNum) -Verbose
    
    $short_description = $SCTask.short_description.display_value
    $short_description_hostname = $short_description.split(' ')[4]

    $RitmNum = $SCTask.request_item.display_value
    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)
    }

    #$Ci = Get-ITDServiceNowRecord -Table cmdb_ci -Filter ("name=" + $short_description_hostname)
    #$Ci = Get-ITDServiceNowRecord -Table cmdb_ci -SysId ($Ritm.VariableSet | Where-Object .host_name_ref) -ErrorAction Stop

    Write-Verbose -Message "Gathering VariableSet data from $RitmNum"
    $MatchFound = $false
    ForEach ($Row in $Ritm.VariableSet) {
        $TempCi = Get-ITDServiceNowRecord -Table cmdb_ci -SysId ($Row.host_name_ref) -ErrorAction Stop
        If ($short_description_hostname -eq $TempCi.FQDN.display_value) {
            $Ci = $TempCi
            $MatchFound = $true
        }
    }

    If ($MatchFound -eq $false) {
        Write-Error -Message "ComputerName $ComputerName was not found in VariableSet for $RitmNum" -ErrorAction Stop
    }

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

    Write-Verbose -Message ("Ci Name " + $Ci.Name.display_value) -Verbose
    Write-Verbose -Message ("Ci FQDN " + $Ci.FQDN.display_value) -Verbose

    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' }
    }
    
    try {
        Write-Verbose -Message "Start Removal of $FQDN, $hardware_platform $hardware_type" -Verbose
        Remove-ITDWindowsServer -ComputerName $FQDN -SCTaskNum $SCTaskNum -Credential $Secret:svcitdiaasauto -Verbose # $Secret:svcitdiaasauto
        #$short_description_new = "$hardware_platform $hardware_type $FQDN is ready for removal."
        #Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = $short_description; }
        #Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{short_description = $short_description_new}
        Write-Verbose -Message "End Removal of $FQDN" -Verbose
    }
    catch {
        Write-Error $error[0]
    }
    Write-Verbose -Message "End $SCTasknum" -Verbose
}
Disconnect-ITDvCenter