param(
    [string]
    $SCTaskNum
)

New-ITDServiceNowSession -Environment Production -Credential $Secret:snow_vmcred

$Filter = "active=true^short_descriptionLIKEWindows Removal for "
$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

# retrieve ndgov\svcitdiaasauto password to be used for Remove-ITDWindowsServer function
$svcitdiaasauto = Get-ITDPassword -Title "IaaS Automation Account" -UserName "ndgov\svcitdiaasauto"

Write-Verbose -Message "Connect to vCenter" -Verbose
Connect-ITDvCenter

ForEach ($OpenTask in $OpenTasks) {
    # get SCTask, Ritm
    $SCTask = $OpenTask
    $SCTaskNum = $OpenTask.number.display_value
    Write-Verbose -Message ("Start " + $SCTaskNum) -Verbose

    If ($SCTask.work_notes.display_value -like "*needs human review*") {
        Write-Verbose -Message ($SCTaskNum + " flagged for human review, skipping...") -Verbose
    }
    Else {
        $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) {
            Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{
                work_notes = 'error during Windows decommission, needs human review'
            }
            Write-Warning -Message "ComputerName $ComputerName was not found in VariableSet for $RitmNum" -WarningAction Continue
        }

        $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 $svcitdiaasauto -Verbose
            Write-Verbose -Message "End Removal of $FQDN" -Verbose
        }
        catch {
            Write-Error $error[0]
        }
        Write-Verbose -Message "End $SCTasknum" -Verbose
    }
}

Write-Verbose -Message "Disconnect to vCenter" -Verbose
Disconnect-ITDvCenter