<#
.SYNOPSIS
    A short one-line action-based description, e.g. 'Tests if a function is valid'
.DESCRIPTION
    A longer description of the function, its purpose, common use cases, etc.
.NOTES
    Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
    Specify a URI to a help page, this will show when Get-Help -Online is used.
.EXAMPLE
    $params = @{
        HostName = "itdzmtest999.nd.gov";
        LicensingRestrictions = "No Licensing Restrictions";
        DataCenter = 'Bismarck';
        Environment = 'Test';
        StartupPriority = '5';
        OperatingSystem = 'Windows Server 2022 Datacenter (64-Bit)'
        DR_Protection = 'None';
        CPU = 1;
        MemoryGB = 4;
        Disk1 = 50;
        Disk2 = 2;
        Disk3 = 3;

    }

    New-ITDVMwareSharePointVMRecord @params
#>

function New-ITDVMwareSharePointVMRecord {
    [CmdletBinding()]
    param (
        [string]
        $HostName,

        [string]
        $ReplacesVM,

        [string]
        $LicensingRestrictions,

        <#[string]
        $AgencyName,#>

        [string]
        $SupportHours,

        [string]
        $DataCenter,

        [string]
        $Environment,

        [int]
        $StartupPriority,

        [string]
        $DR_Protection,

        [string]
        $OperatingSystem,

        [int]
        $CPU,

        [int]
        $MemoryGB,

        [string]
        $CIDRBlock,

        [int]
        $Disk1,
        
        [int]
        $Disk2,
        
        [int]
        $Disk3,
        
        [int]
        $Disk4,
        
        [int]
        $Disk5,
        
        [int]
        $Disk6,
        
        [int]
        $Disk7,
        
        [int]
        $Disk8,
        
        [int]
        $Disk9,
        
        [int]
        $Disk10,
        
        [int]
        $Disk11,
        
        [int]
        $Disk12,
        
        [int]
        $Disk13,
        
        [int]
        $Disk14,
        
        [int]
        $Disk15,
        
        [int]
        $Disk16
    )
    
    begin {
        $UrlContextInfo = "https://share.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/contextinfo"
        $InvokeWebRequestParams = @{
            Uri             = $UrlContextInfo;
            Method          = "Post";
            UseBasicParsing = $true;
        }
        If ($Credential) { $InvokeWebRequestParams += @{Credential = $Credential } }
        Else { $InvokeWebRequestParams += @{UseDefaultCredentials = $true } }
        #$RequestDigest = Invoke-RestMethod -Uri $UrlContextInfo -Method Post -UseDefaultCredentials
        $RequestDigest = Invoke-RestMethod @InvokeWebRequestParams
        $RequestDigest = $RequestDigest.GetContextWebInformation.FormDigestValue
        
        $UrlList = "https://share.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VM Guests')"
        $InvokeWebRequestParams = @{
            Uri             = $UrlList;
            UseBasicParsing = $true;
        }
        If ($Credential) { $InvokeWebRequestParams += @{Credential = $Credential } }
        Else { $InvokeWebRequestParams += @{UseDefaultCredentials = $true } }
        #$List = Invoke-RestMethod -uri $UrlList -UseDefaultCredentials
        $List = Invoke-RestMethod @InvokeWebRequestParams
        $ListItemEntityTypeFullName = $list.entry.content.properties.ListItemEntityTypeFullName

        $UrlListItems = "https://share.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VM Guests')/items"

        $header = @{
            "accept"          = "application/json;odata=verbose"
            "X-RequestDigest" = $RequestDigest
        }
    }
    
    process {
        [PSCustomObject]$NewRecord = @{
            "__metadata" = @{type = $ListItemEntityTypeFullName } 
            Title        = $HostName
        }
        write-host $PSBoundParameters
        switch($PSBoundParameters.Keys){
            # '' {$NewRecord += @{ = }}
            'LicensingRestrictions' {$NewRecord += @{LicensingRestrictions = $LicensingRestrictions}}
            'DataCenter' {$NewRecord += @{DataCenter = $DataCenter}}
            'Environment' {$NewRecord += @{Environment = $Environment}}
            'StartupPriority' {$NewRecord += @{StartupPriority = [string]$StartupPriority}}
            'OperatingSystem' {$NewRecord += @{OS = $OperatingSystem}}
            'DR_Protection' {$NewRecord += @{DR_Protection = $DR_Protection}}
            'CPU' {$NewRecord += @{Processors = $CPU}}
            'MemoryGB' {$NewRecord += @{RAM = $MemoryGB}}
            'Disk1' {$NewRecord += @{Disk_x0020_C_x003a_ = $Disk1}}
            'Disk2' {$NewRecord += @{Disk2_x002d_SwapDisk = $Disk2}}
            'Disk3' {$NewRecord += @{Disk_x0020_D_x003a_ = $Disk3}}
        }

        $body = $NewRecord | ConvertTo-Json
        
        $InvokeWebRequestParams = @{
            Uri             = $UrlListItems;
            Method          = "Post";
            Body            = $body;
            ContentType     = "application/json;odata=verbose";
            Headers         = $header;
            UseBasicParsing = $true;
        }
        If ($Credential) { $InvokeWebRequestParams += @{Credential = $Credential } }
        Else { $InvokeWebRequestParams += @{UseDefaultCredentials = $true } }
        #Invoke-RestMethod -Method Post -Uri $UrlListItems -Body $body -ContentType "application/json;odata=verbose" -Headers $header -UseDefaultCredentials        
        Invoke-RestMethod @InvokeWebRequestParams
    }
    
    end {
        
    }
}