<#
.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 Set-ITDVMwareVMTagFromCmdb {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $true, 
            ValueFromPipeline = $true,
            ParameterSetName = 'VMName'
        )]
        [string]
        $VMName
    )
    
    begin {
        #$Result = [System.Collections.ArrayList]@()
    }
    
    process {
        $HostName = $VMName.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"
            return
        }

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

        $obj = [PSCustomObject]@{
            VMName                = $VMName;
            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 $VMName DTAP Tag to " + $Cmdb.environment.display_value)
                Set-ITDVMwareVMTag -ComputerName $VMName -Dtap $Cmdb.environment.display_value
            }
            Else {
                
            }
        }

        # 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 $VMName AppName Tag to " + $Cmdb.u_nd_application_svc.display_value)
                Set-ITDVMwareVMTag -ComputerName $VMName -AppName $Cmdb.u_nd_application_svc.display_value
            }
            Else {

            }
        }

        # 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 $VMName Licensing Restrictions Tag to " + $Cmdb.u_nd_licensing_restrictions.display_value)
                Set-ITDVMwareVMTag -ComputerName $VMName -LicensingRestrictions $Cmdb.u_nd_licensing_restrictions.display_value
            }
            Else {

            }
        }

        # 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 $VMName SRM Recovery Type Tag to " + $Cmdb.u_srm_recovery_type.display_value)
                Set-ITDVMwareVMTag -ComputerName $VMName -SRMRecoveryType $Cmdb.u_srm_recovery_type.display_value
            }
            Else {

            }
        }
        #
        # 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 $VMName DR Protection Tag to " + ($Cmdb.u_nd_dr_protection.display_value -replace "VMware: ") )
                Set-ITDVMwareVMTag -ComputerName $VMName -DRProtection ($Cmdb.u_nd_dr_protection.display_value -replace "VMware: " -replace "RPO", "VR RPO")
            }
            Else {

            }
        }
    }
    
    end {
        
    }
}