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

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

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $CPU = 1,
    
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $MemoryGB = 4,

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $DiskOsGB = 50,

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $DiskSwapGB = ($MemoryGB + 1),

    [Parameter(ParameterSetName = 'ManualEntry')]
    [int]
    $DiskDataGB,

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $Subnet = '10.11.12.0/23',

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $OS = 'Windows Server 2022 Datacenter',

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $Environment = 'Test',
    
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $Datacenter = 'Mandan',

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $AppName = 'ITD-POC-zmeier',

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $StartupPriority = 5,

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $LicensingRestrictions = "No Licensing Restrictions"
)

switch ($PSCMdlet.ParameterSetName) {
    'ManualEntry' {
        $NewITDWindowsVmVMwareParams = @{
            ComputerName          = $ComputerName;
            CPU                   = $CPU;
            MemoryGB              = $MemoryGB;
            DiskOsGB              = $DiskOsGB;
            DiskSwapGB            = $DiskSwapGB;
            DiskDataGB            = $DiskDataGB;
            Subnet                = $Subnet;
            OS                    = $OS;
            Environment           = $Environment;
            Datacenter            = $Datacenter;
            AppName               = $AppName;
            StartupPriority       = $StartupPriority;
            LicensingRestrictions = $LicensingRestrictions;
        }
    }
    '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

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

        $NewITDWindowsVmVMwareParams = @{
            ComputerName          = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).host_name );
            CPU                   = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).processors );
            MemoryGB              = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).memory_gb );
            DiskOsGB              = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).disk_1_os );
            DiskSwapGB            = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).disk_2_swap_disk );
            DiskDataGB            = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).disk_3 );
            Subnet                = ( Get-ITDServiceNowRecord -Table 'cmdb_ci_ip_network' -SysId ($Ritm.VariableSet | Where-Object { $_.host_name -eq "$FqdnFromSCTaskDescription" }).cidr_block).name.display_value;
            OS                    = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).target_os_version_windows );
            Environment           = ( $Ritm.customvariable.environment.value );
            Datacenter            = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).data_center );
            AppName               = ( Get-ITDServiceNowRecord -Table 'cmdb_ci_service' -SysId ($Ritm.VariableSet | Where-Object { $_.host_name -eq "$FqdnFromSCTaskDescription" }).application_info).name.display_value;
            StartupPriority       = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).startup_priority );
            LicensingRestrictions = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).licensing_restrictions );
        }
    }
}

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

Write-Verbose -Message "Attempt server provisioning" -Verbose
Write-Host $NewITDWindowsVmVMwareParams
New-ITDWindowsVmVMware @NewITDWindowsVmVMwareParams -Credential $Secret:ndgov_svcitdiaasauto -Verbose
Write-Warning -Message "New-ITDWindowsVmVMware function completed"

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