[CmdletBinding()]
param (
    [Parameter(Mandatory = $true, ParameterSetName = 'FromSCTask')]
    $SCTaskNum,

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $FQDN,

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $AppName,

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $VMEnvironment
)

switch ($PSCMdlet.ParameterSetName) {
    'ManualEntry' {
        $NewITDWindowsVmVMwareStep2Params = @{
            FQDN          = $FQDN;
            AppName       = $AppName;
            VMEnvironment = $VMEnvironment;
        }
    }
    'FromSCTask' {
        Write-Verbose -Message ("UAJob.ComputerName = " + $UAJob.ComputerName) -Verbose
        switch ($UAJob.ComputerName) {
            "ITDWINAUTOT1" { $ServiceNowEnvironment = 'Test' }
            
            "ITDWINAUTOP1" { $ServiceNowEnvironment = 'Production' }
        }
        New-ITDServiceNowSession -Environment $ServiceNowEnvironment -Credential $Secret:snow_vmcred

        $SCTask = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum
        $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -SysId ($SCTask.request_item.value) -IncludeVariableSet -IncludeCustomVariable
        Write-Verbose -Message ("Ritm: " + $Ritm.Number) -Verbose

        $FqdnFromSCTaskDescription = ($SCTask.short_description).display_value.split(' ')[7]
        Write-Verbose -Message ("FqdnFromSCTaskDescription: " + ($SCTask.short_description).display_value.split(' ')[7]  ) -Verbose
        $NewITDWindowsVmVMwareStep2Params = @{
            FQDN = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).host_name );
            VMEnvironment  = ( ($Ritm.CustomVariable).environment.value );
            AppName      = ( (Get-ITDServiceNowRecord -Table cmdb_ci_service_auto -SysId ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).application_info).Name.display_value);
        }

        Write-Verbose -Message ("Params FQDN: " + $NewITDWindowsVmVMwareStep2Params.FQDN) -Verbose
    }
}


Write-Verbose -Message "Connect to vCenter" -Verbose
Connect-ITDvCenter -Credential $Secret:ndgov_svcitdiaasauto -Verbose

Write-Verbose -Message "Attempt VMware step 2" -Verbose
Write-Host $NewITDWindowsVmVMwareStep2Params
try {
    New-ITDWindowsVmVMwareStep2 @NewITDWindowsVmVMwareStep2Params -Credential $Secret:ndgov_svcitdiaasauto -Verbose
    Write-Warning -Message "New-ITDWindowsVmVMwareStep2 function completed"
    $Step2Complete = $true
}
catch {
    Write-Error -Message $error[0]
    $Step2Complete = $false
    throw
}

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

switch ($PSCmdlet.ParameterSetName) {
    'FromSCTask' {
        switch ($Step2Complete) {
            $true {
                Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{
                    work_notes = ("VM build step 2 complete.  `nPSU Job Id #" + $UAJob.Id);
                }
            }
            $false {
                Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{
                    work_notes = ("VMware build step 2 errored, needs human review.  `nPSU Job Id #" + $UAJob.Id)
                }
            }
        }
    }
}