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

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

switch ($PSCmdlet.ParameterSetName) {
    'ManualEntry' {
        $NewITDWindowsVmAzureParams = @{
            FQDN          = $FQDN;
        }
    }
    'FromSCTask' {
        Write-Verbose -Message ("UAJob.ComputerName = " + $UAJob.ComputerName) -Verbose
        switch ($UAJob.ComputerName) {
            "ITDWINAUTOT1" { $ServiceNowEnvironment = 'Test' }
            "ITDWINAUTOP1" { $ServiceNowEnvironment = 'Production' }
        }
        Write-Verbose -Message "New-ITDServiceNowSession" -Verbose
        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

        $FqdnFromSCTaskDescription = ($SCTask.short_description).display_value.split(' ')[7]

        $NewITDWindowsVmAzureParams = @{
            FQDN          = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).host_name );
            AppName       = ( (Get-ITDServiceNowRecord -Table cmdb_ci_service_auto -SysId ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).application_info).Name.display_value);
            VMEnvironment = ( $Ritm.customvariable.environment.value );
            }
    }
}

Write-Verbose -Message "Connect to Azure using Service Principal" -Verbose
$tenantId = '2dea0464-da51-4a88-bae2-b3db94bc0c54'
$AppId = '60244573-7130-4026-9c6d-47de73f8ca29'
$SecureStringPwd = $Secret:azure_iaasserviceprincipal
$PSCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AppId, ($SecureStringPwd | ConvertTo-SecureString -AsPlainText -Force)
Connect-AzAccount -ServicePrincipal -Credential $PSCredential -Tenant $tenantId

Write-Verbose -Message "NewITDWindowsVMAzureParams:" -Verbose
Write-Verbose -Message ("FQDN " + $NewITDWindowsVMAzureParams.FQDN) -Verbose

try {
    New-ITDWindowsVmAzureStep2 @NewITDWindowsVmAzureParams -Credential $Secret:ndgov_svcitdiaasauto -Verbose
    Write-Warning -Message "New-ITDWindowsVmAzureStep2 function completed"
    $Step2Complete = $true
}
catch {
    Write-Error -Message $error[0]
    $Step2Complete = $false
    throw
}

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