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

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

    [Parameter(ParameterSetName = 'FromSCTask')]
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [string]
    $VMSizeOverride,
    <#
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $CPU = 1,
        
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $MemoryGB = 4,
#>
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
    [int]
    $DiskOsGB = 128,

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

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

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

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

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

    [string]
    $ResourceGroupNameOverride,

    [ValidateSet('1', '2', '3')]
    [int]
    $AvailabilityZone
)

switch ($PSCmdlet.ParameterSetName) {
    'ManualEntry' {
        $NewITDWindowsVmAzureParams = @{
            FQDN                  = $FQDN;
            AppName               = $AppName;
            VMSizeOverride        = $VMSizeOverride;
            #CPU                   = $CPU;
            #MemoryGB              = $MemoryGB;
            DiskOsGB              = $DiskOsGB;
            DiskDataGB            = $DiskDataGB;
            Subnet                = $Subnet;
            OS                    = $OS;
            VMEnvironment         = $VMEnvironment;
            LicensingRestrictions = $LicensingRestrictions;
        }
        switch ($PSBoundParameters.Keys) {
            'ResourceGroupNameOverride' { $NewITDWindowsVmAzureParams += @{ ResourceGroupNameOverride = $ResourceGroupNameOverride } }
            'AvailabilityZone' { $NewITDWindowsVmAzureParams += @{ AvailabilityZone = $AvailabilityZone } }
        }
    }
    '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 );
            DiskOsGB              = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).disk_1_os );
            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 );
            VMEnvironment         = ( $Ritm.customvariable.environment.value );
            AppName               = ( Get-ITDServiceNowRecord -Table 'cmdb_ci_service' -SysId ($Ritm.VariableSet | Where-Object { $_.host_name -eq "$FqdnFromSCTaskDescription" }).application_info).name.display_value;
            LicensingRestrictions = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).licensing_restrictions );
        }
        switch ($PSBoundParameters.Keys) {
            'ResourceGroupNameOverride' {
                Write-Warning -Message "ResourceGroupNameOverride found $ResourceGroupNameOverride"
                $NewITDWindowsVMAzureParams += @{ ResourceGroupNameOverride = $ResourceGroupNameOverride } 
            }
            'AvailabilityZone' {
                Write-Warning -Message "ResourceGroupNameOverride found $AvailabilityZone"
                $NewITDWindowsVMAzureParams += @{ AvailabilityZone = $AvailabilityZone } 
            }
            'VMSizeOverride' {
                Write-Warning -Message "VMSizeOverride found $VMSizeOverride"
                $NewITDWindowsVMAzureParams += @{ VMSizeOverride = $VMSizeOverride } 
            }
        }
        If ($NewITDWindowsVmAzureParams.VMSizeOverride) {
            # do nothing
        } Else {
            $NewITDWindowsVMAzureParams += @{ CPU = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).processors );}
            $NewITDWindowsVMAzureParams += @{ MemoryGB = ( ($Ritm.VariableSet | Where-Object { $_.host_name -eq $FqdnFromSCTaskDescription }).memory_gb );}
        }
    }
}

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
Write-Verbose -Message ("CPU " + $NewITDWindowsVMAzureParams.CPU) -Verbose
Write-Verbose -Message ("MemoryGB " + $NewITDWindowsVMAzureParams.MemoryGB) -Verbose
Write-Verbose -Message ("DiskOsGB" + $NewITDWindowsVMAzureParams.DiskOsGB) -Verbose
Write-Verbose -Message ("DiskDataGB " + $NewITDWindowsVMAzureParams.DiskDataGB) -Verbose
Write-Verbose -Message ("Subnet " + $NewITDWindowsVMAzureParams.Subnet) -Verbose
Write-Verbose -Message ("OS" + $NewITDWindowsVMAzureParams.OS) -Verbose
Write-Verbose -Message ("VMEnvironment" + $NewITDWindowsVMAzureParams.Environment) -Verbose
Write-Verbose -Message ("AppName" + $NewITDWindowsVMAzureParams.AppName) -Verbose
Write-Verbose -Message ("LicensingRestrictions" + $NewITDWindowsVMAzureParams.LicensingRestrictions) -Verbose

New-ITDWindowsVmAzureStep1 @NewITDWindowsVmAzureParams -Credential $Secret:ndgov_svcitdiaasauto -Verbose