<#
.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
    Test-MyTestFunction -Verbose
    Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>

function Compare-ITDVMwareVMTagFromCmdb {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $true, 
            ValueFromPipeline = $true,
            ParameterSetName = 'VMName'
        )]
        [string[]]
        $VMName
    )
    
    begin {
        #$Result = [System.Collections.ArrayList]@()
    }
    
    process {
        ForEach ($vname in $VMName) {
            $HostName = $vname.split('.')[0]
            Write-Verbose -Message "Looking up Cmdb Object named $HostName"
            $Cmdb = Get-ITDServiceNowRecord -Table cmdb_ci_server -Filter "name=$HostName"

            If ($null -eq $Cmdb) {
                Write-Warning -Message "No Cmdb Object found for $HostName"
                Continue
            }

            $VM = Get-VM -Name $vname | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
            $OldTags = Get-TagAssignment -Entity $VM

            $VMCompare = [PSCustomObject]@{
                VMName                = $vname;
                DTAP                  = $null;
                AppName               = $null;
                LicensingRestrictions = $null;
                DRProtection          = $null;
                SRMRecoveryType       = $null;
            }
            # DTAP
            #If ($Cmdb.environment.display_value) {
                $TagAssignmentValue = ($OldTags | Where-Object { $_.Tag.Category.Name -eq 'DTAP' }).Tag.Name
                If ($Cmdb.environment.display_value -ne $TagAssignmentValue) {
                    Write-Verbose -Message ("Setting $vname DTAP Tag to " + $Cmdb.environment.display_value)
                    #Set-ITDVMwareVMTag -ComputerName $vname -Dtap $Cmdb.environment.display_value
                    $VMCompare.DTAP = $false
                }
                Else {
                    $VMCompare.DTAP = $true
                }
            #}

            # AppName
            #If ($Cmdb.u_nd_application_svc.display_value) {
                $TagAssignmentValue = ($OldTags | Where-Object { $_.Tag.Category.Name -eq 'AppName' }).Tag.Name
                If ($Cmdb.u_nd_application_svc.display_value -ne $TagAssignmentValue) {
                    Write-Verbose -Message ("Setting $vname AppName Tag to " + $Cmdb.u_nd_application_svc.display_value)
                    #Set-ITDVMwareVMTag -ComputerName $vname -AppName $Cmdb.u_nd_application_svc.display_value
                    $VMCompare.AppName = $false
                }
                Else {
                    $VMCompare.AppName = $true
                }
            #}

            # Licensing Restrictions
            #If ($Cmdb.u_nd_licensing_restrictions.display_value) {
                $TagAssignmentValue = ($OldTags | Where-Object { $_.Tag.Category.Name -eq 'LicensingRestrictions' }).Tag.Name
                If ($Cmdb.u_nd_licensing_restrictions.display_value -ne $TagAssignmentValue) {
                    Write-Verbose -Message ("Setting $vname Licensing Restrictions Tag to " + $Cmdb.u_nd_licensing_restrictions.display_value)
                    #Set-ITDVMwareVMTag -ComputerName $vname -LicensingRestrictions $Cmdb.u_nd_licensing_restrictions.display_value
                    $VMCompare.LicensingRestrictions = $false
                }
                Else {
                    $VMCompare.LicensingRestrictions = $true
                }
            #}

            # startup priority   TBD
            # startup priority is not in Cmdb

            # SRM Recovery Type
            #If ($Cmdb.u_srm_recovery_type.display_value) {
                $TagAssignmentValue = ($OldTags | Where-Object { $_.Tag.Category.Name -eq 'SRM Recovery Type' }).Tag.Name
                If ($Cmdb.u_srm_recovery_type.display_value -ne $TagAssignmentValue) {
                    Write-Verbose -Message ("Setting $vname SRM Recovery Type Tag to " + $Cmdb.u_srm_recovery_type.display_value)
                    #Set-ITDVMwareVMTag -ComputerName $vname -SRMRecoveryType $Cmdb.u_srm_recovery_type.display_value
                    $VMCompare.SRMRecoveryType = $false
                }
                Else {
                    $VMCompare.SRMRecoveryType = $true
                }
            #} Else {
                #$TagAssignmentValue = ($OldTags | Where-Object { $_.Tag.Category.Name -eq 'SRM Recovery Type' }).Tag.Name
                #If ($null -ne $TagAssignmentValue) {
                    #Write-Verbose -Message ("Setting $vname SRM Recovery Type Tag to " + $null)
                    #$VMCompare.SRMRecoveryType = $false
                #}
                #Else {
                    #$VMCompare.SRMRecoveryType = $true
                #}
            #}
            #
            # DR Protection
            #If ( ($Cmdb.u_nd_dr_protection.display_value -replace "VMware: " -replace "RPO", "VR RPO") ) {
                #-replace "VMware: " -replace "RPO", "VR RPO"
                $TagAssignmentValue = ($OldTags | Where-Object { $_.Tag.Category.Name -eq 'DR Protection' }).Tag.Name
                If ( ($Cmdb.u_nd_dr_protection.display_value -replace "VMware: " -replace "RPO", "VR RPO") -ne $TagAssignmentValue) {
                    Write-Verbose -Message ("Setting $vname DR Protection Tag to " + ($Cmdb.u_nd_dr_protection.display_value -replace "VMware: ") )
                    #Set-ITDVMwareVMTag -ComputerName $vname -DRProtection ($Cmdb.u_nd_dr_protection.display_value -replace "VMware: " -replace "RPO", "VR RPO")
                    $VMCompare.DRProtection = $false
                }
                Else {
                    $VMCompare.DRProtection = $true
                }
            #}
            Write-Output $VMCompare
        }
    }
    
    end {
        
    }
}