Param(
    [string]
    $SCTaskNum
)

New-ServiceNowSession -Url 'northdakota.service-now.com' -Credential $Secret:SNowVMCred
Connect-ITDvCenter -Credential $Secret:svcitdvmvcauto

$Filter = @('assignment_group', '-like', 'NDIT-Server Build Automation'), '-and', @('short_description', '-like', 'Windows Guest OS complete.  Hardware team review.'), '-and', @('state', '-eq', '2') # 2 = 'work in progress'
$OpenTasks = Get-ServiceNowRecord -Table 'Catalog Task' -Filter $Filter | Sort-Object Number
If ($PSBoundParameters.ContainsKey("SCTaskNum")) {
    Write-Verbose -Message "SCTaskNum parameter found, value is $SCTaskNum"
    $OpenTasks = $OpenTasks | Where-Object Number -EQ $SCTaskNum
}

Write-Verbose -Message ("OpenTasks found: " + $OpenTasks.count) -Verbose
ForEach ($OpenTask in $OpenTasks) {
    Write-Verbose -Message $OpenTasks.Number -Verbose
}

ForEach ($OpenTask in $OpenTasks) {
    $Ci = $null
    $BuildComplete = $null

    # get SCTask, Ritm
    $SCTaskNum = $OpenTask.number
    Write-Verbose -Message "Start $SCTasknum" -Verbose
    $SCTask = Get-ServiceNowRecord -Table 'Catalog Task' -ID $SCTaskNum
    $shortdescription = $SCTask.short_description
    $RitmNum = $SCTask.request_item.display_value
    $Ritm = Get-ServiceNowRecord -Table 'Requested Item' -ID $RitmNum -IncludeCustomVariable -WarningAction SilentlyContinue

    switch (($Ritm.CustomVariable | Where-Object Name -EQ target_platform).Value) {
        'azure' { $target_platform = "Azure" }
        'vmware' { $target_platform = "VMware" }
    }

    $FormFQDN = ($RITM.CustomVariable | Where-Object Name -EQ "host_name").value
    $FormHostName = $FormFQDN.split('.')[0]

    $Ci = Get-ServiceNowRecord -Table cmdb_ci -Filter @('name', '-eq', $FormHostName)

    If ($Ci) {
        Write-Verbose -Message ("Ci found, sys_id = " + $Ci.sys_id + ", name = " + $Ci.name + ", fqdn = " + $Ci.fqdn) -Verbose
    }
    switch ($target_platform) {
        { $_ -like "*VMware*" } {
            Connect-ITDvCenter -Credential $Secret:svcitdvmvcauto
            Write-Verbose -Message ("$FormFQDN is a VMware VM.  Determine if SRM was requested.") -Verbose
            $hardware_platform = "VMware";
            $hardware_type = 'Virtual Machine'
                
            If ( ($Ritm.CustomVariable | Where-Object Name -EQ 'dr_protection').Value -eq 'No DR') {
                Write-Verbose -Message ("$FormFQDN dr_protection equals 'No DR'") -Verbose
                Approve-ITDVMNewBuild -SCTaskNum $SCTaskNum -CloseTask -Verbose
            }
            Else {
                Write-Verbose -Message ("$FormFQDN dr_protection is requested") -Verbose
                Write-Warning -Message ("SRM is requested, task will not auto close. -- ZM") -Verbose
                Approve-ITDVMNewBuild -SCTaskNum $SCTaskNum
            }
            Disconnect-ITDvCenter
        }
        { $_ -like "*Microsoft Virtual Machine*" } {
            Write-Verbose -Message ("$FormFQDN is an Azure VM.  DR is not an option, proceed.") -Verbose
            $hardware_platform = "Azure";
            $hardware_type = 'Virtual Machine'                
            Write-Warning -Message ("Final close task is commented out until testing can occur. -- ZM") -Verbose
            #Approve-ITDVMNewBuild -SCTaskNum $SCTaskNum
        }
        { $_ -like "*HP*" } {
            Write-Verbose -Message ("$FormFQDN is an HPE device.") -Verbose
            $hardware_platform = 'HPE';
            $hardware_type = 'Physical'
            Write-Warning -Message ("Final close task is commented out until testing can occur. -- ZM") -Verbose
        }
        default {
            $hardware_platform = 'Other'
            Write-Warning -Message ("Ci found, but unavailable to determine hardware platform.")
        }
    }
        
}

Disconnect-ITDvCenter