Param(
    [string]
    $SCTaskNum
)

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


$Filter = 'active=true^short_descriptionSTARTSWITHAutomated Server Build Task for Windows Machine'
$OpenTasks = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Filter $Filter | Sort-Object Number
If ($PSBoundParameters.ContainsKey("SCTaskNum")) {
    Write-Verbose -Message "SCTaskNum parameter found, value is $SCTaskNum" -Verbose
    $OpenTasks = $OpenTasks | Where-Object { $_.number.value -EQ $SCTaskNum }
}

$AllRitms = [System.Collections.ArrayList]@()

Write-Verbose -Message ("OpenTasks found: " + @($OpenTasks).Count) -Verbose

ForEach ($OpenTask in $OpenTasks) {
    $PSUJob = $null
    $SCTask = $null
    $shortdescription = $null
    $shortdescription_hostname = $null
    $WorkNotesMsg = $null

    $SCTaskNum = $OpenTask.number.Value
    Write-Verbose -Message "Start $SCTaskNum" -Verbose

    try {
        $SCTask = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum
        $shortdescription = $SCTask.short_description.display_value
        $shortdescription_hostname = $shortdescription.split(' ')[7]
        $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $SCTask.request_item.display_value -IncludeVariableSet
    <#
        If ($AllRitms | Where-Object { $_.number.display_value -EQ $SCTask.request_item.display_value }) {
            Write-Verbose -Message ("Ritm already in memory") -Verbose
            $Ritm = $AllRitms | Where-Object sys_id -EQ $SCTask.request_item.display_value
        }
        Else {
            Write-Verbose -Message "Ritm is not in memory, retrieve it" -Verbose
            $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $SCTask.request_item.display_value -IncludeVariableSet
            $null = $AllRitms.Add($Ritm)
        }
#>
        # check for step messages in SCTask work_notes and determine next step
        switch ($SCTask.work_notes.display_value) {
            Default {
                # execute Step 1
                Write-Verbose -Message "No step messages found, starting Step 1" -Verbose
                # Determine if VMware or Azure and run appropriate build function
                switch ( ($Ritm.VariableSet | Where-Object host_name -EQ $shortdescription_hostname).target_platform ) {
                    'azure' {
                        $target_platform = "Azure"
                        Write-Verbose "Invoking PSUScript for Azure Step 1" -Verbose
                        #$PSUJob = Invoke-PSUScript -Script "New-ITDWindowsVmAzure_Step1.ps1" -SCTaskNum $SCTaskNum
                        $WorkNotesMsg = ("Azure build Step 1 started.`nPSU Job Id #" + $PSUJob.Id)
                    }
                    'vmware' {
                        $target_platform = "VMware"
                        Write-Verbose "Invoking PSUScript for VMware Step 1" -Verbose
                        $PSUJob = Invoke-PSUScript -Script "Get-HelloWorld.ps1" -SCTaskNum $SCTaskNum
                        $WorkNotesMsg = ("VMware build Step 1 started.`nPSU Job Id #" + $PSUJob.Id)
                    }
                }
                Break
            }
        }

        
    }
    catch {
        Write-Error -Message $error[0]
    }

    Write-Verbose -Message "End $SCTaskNum" -Verbose
}