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

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

    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
<<<<<<< HEAD
    [string]
    $VMSizeOverride,
<<<<<<< HEAD
<#
=======
    <#
>>>>>>> main
    [Parameter(Mandatory = $true, ParameterSetName = 'ManualEntry')]
=======
>>>>>>> 828a9d5a994531efc47fe9ca78c93cfb076c6ba4
    [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]
    $Environment,
        
    [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 = @{
            ComputerName          = $ComputerName;
            AppName               = $AppName;
<<<<<<< HEAD
<<<<<<< HEAD
            VMSizeOverride = $VMSizeOverride;
=======
            VMSizeOverride        = $VMSizeOverride;
>>>>>>> main
            #CPU                   = $CPU;
            #MemoryGB              = $MemoryGB;
=======
            CPU                   = $CPU;
            MemoryGB              = $MemoryGB;
>>>>>>> 828a9d5a994531efc47fe9ca78c93cfb076c6ba4
            DiskOsGB              = $DiskOsGB;
            DiskDataGB            = $DiskDataGB;
            Subnet                = $Subnet;
            OS                    = $OS;
            Environment           = $Environment;
            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 = @{
            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 );
            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 );
            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 $ResourceGroupNameOverride"
            $NewITDWindowsVMAzureParams += @{ AvailabilityZone = $AvailabilityZone } 
            }
        }
    }
}

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 ("ComputerName " + $NewITDWindowsVMAzureParams.ComputerName) -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 ("Environment" + $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