<#
.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 Sync-ITDVMwareVMTagsFromCmdb {
    [CmdletBinding()]
    param (
        
    )
    
    begin {
        
    }
    
    process {
        $GetITDServiceNowRecordParams = @{
            #Filter            = "model_idLIKEVMware"
            Table             = 'cmdb_ci_server';
            IncludeTotalCount = $true;
            Fields            = @(
                'name',
                'fqdn',
                'environment',
                'u_nd_type',
                'model_id',
                'os',
                'os_version',
                'u_nd_application_svc',
                'u_nd_licensing_restrictions',
                'u_nd_dr_protection',
                'u_srm_recovery_type'
            )
        }

        Write-Verbose -Message 'Retrieve all CMDB CIs'
        $AllCmdb = Get-ITDServiceNowRecord @GetITDServiceNowRecordParams
        $AllCmdbValues = $AllCmdb | Select-Object @{n = 'Name'; e = { $_.Name.display_value } },
            @{n = 'fqdn'; e = { $_.fqdn.display_value } },
            @{n = 'DTAP'; e = { $_.environment.display_value } },
            @{n = 'environment'; e = { $_.u_nd_type.display_value } },
            @{n = 'type'; e = { $_.u_nd_type.display_value } },
            @{n = 'model_id'; e = { $_.model_id.display_value } },
            @{n = 'os'; e = { $_.os.display_value } },
            @{n = 'os_version'; e = { $_.os_version.display_value } },
            @{n = 'u_nd_application_svc'; e = { $_.u_nd_application_svc.display_value } },
            @{n = 'u_nd_dr_protection'; e = { $_.u_nd_dr_protection.display_value } },
            @{n = 'u_nd_licensing_restrictions'; e = { $_.u_nd_licensing_restrictions.display_value } },
            @{n = 'u_srm_recovery_type'; e = { $_.u_srm_recovery_type.display_value } }

        # DTAP from all unique options from the cmdb_ci_server table field name "Environment"
        Write-Verbose -Message "Start Environment Tag sync"
        $CmdbValues = ($AllCmdbValues | Select-Object -Unique DTAP | Select-Object -ExpandProperty DTAP)
        $TagValues = (Get-Tag -Category DTAP -Server itdvmvc1.nd.gov).Name
        $Compare = Compare-Object -ReferenceObject $CmdbValues -DifferenceObject $TagValues
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '<=')) {
            Write-Verbose -Message ("Create DTAP tag for " + $c.InputObject)
            New-Tag -Category DTAP_SNow -Name $c.InputObject -Server 'itdvmvc1.nd.gov'
        }
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '=>')) {
            Write-Verbose -Message ("Remove DTAP tag for " + $c.InputObject)
            Get-Tag -Category DTAP_SNow -Name $c.InputObject -Server 'itdvmvc1.nd.gov' | Remove-Tag -Confirm:$false
        }
        $TagValues = $null
        $Compare = $null
        Write-Verbose -Message "End Environment Tag sync"

        # AppName, get list from ServiceNow table cmdb_ci_service
        Write-Verbose -Message "Start AppName Tag sync"
        $AllAppNames = (Get-ITDServiceNowRecord -Table 'cmdb_ci_service' -Filter "operational_status=1" -IncludeTotalCount -Fields name).name.display_value | Where-Object { $_ -like "*-*" }
        $TagValues = (Get-Tag -Category AppName -Server itdvmvc1.nd.gov).Name
        $Compare = Compare-Object -ReferenceObject $AllAppNames -DifferenceObject $TagValues
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '<=')) {
            Write-Verbose -Message ("Create AppName tag for " + $c.InputObject)
            New-Tag -Category AppName -Name $c.InputObject -Server 'itdvmvc1.nd.gov'
        }
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '=>')) {
            Write-Verbose -Message ("Remove AppName tag for " + $c.InputObject)
            Get-Tag -Category AppName -Name $c.InputObject -Server 'itdvmvc1.nd.gov' | Remove-Tag -Confirm:$false
        }
        $TagValues = $null
        $Compare = $null
        Write-Verbose -Message "End AppName Tag sync"

        # Licensing Restrictions
        Write-Verbose -Message "Start Licensing Restrictions Tag sync"
        $AllLicensingRestrictions = ($AllCmdbValues | Select-Object -Unique u_nd_licensing_restrictions | Select-Object -ExpandProperty u_nd_licensing_restrictions)
        $TagValues = (Get-Tag -Category LicensingRestrictions -Server itdvmvc1.nd.gov).Name
        $Compare = Compare-Object -ReferenceObject $AllLicensingRestrictions -DifferenceObject $TagValues
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '<=')) {
            Write-Verbose -Message ("Create AppName tag for " + $c.InputObject)
            New-Tag -Category LicensingRestrictions -Name $c.InputObject -Server 'itdvmvc1.nd.gov'
        }
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '=>')) {
            Write-Verbose -Message ("Remove AppName tag for " + $c.InputObject)
            Get-Tag -Category LicensingRestrictions -Name $c.InputObject -Server 'itdvmvc1.nd.gov' | Remove-Tag -Confirm:$false
        }

        $TagValues = $null
        $Compare = $null

        # Startup Priority
        # Startup Priority is not in CMDB

        # DR_Protection
        Write-Verbose -Message "Start DR Protection Tag sync"
        $AllDRProtection = ($AllCmdbValues | Select-Object -Unique u_nd_dr_protection | Select-Object -ExpandProperty u_nd_dr_protection) -replace "VMware: " -replace "RPO", "VR RPO" | Sort-Object
        $TagValues = (Get-Tag -Category "DR Protection" -Server itdvmvc1.nd.gov).Name
        $Compare = Compare-Object -ReferenceObject $AllDRProtection -DifferenceObject $TagValues
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '<=')) {
            Write-Verbose -Message ("Create DR Protection tag for " + $c.InputObject)
            New-Tag -Category "DR Protection" -Name $c.InputObject -Server 'itdvmvc1.nd.gov'
        }
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '=>')) {
            Write-Verbose -Message ("Create Protection tag for " + $c.InputObject)
            Get-Tag -Category "DR Protection" -Name $c.InputObject -Server 'itdvmvc1.nd.gov' | Remove-Tag -Confirm:$false
        }

        $TagValues = $null
        $Compare = $null
        Write-Verbose -Message "End DR Protection Tag sync"
        
        # SRM Recovery Type
        Write-Verbose -Message "Start SRM Recovery Type Tag sync"
        $AllSRMRecoveryType = ($AllCmdbValues | Select-Object -Unique u_srm_recovery_type | Select-Object -ExpandProperty u_srm_recovery_type)
        $TagValues = (Get-Tag -Category "SRM Recovery Type" -Server itdvmvc1.nd.gov).Name
        $Compare = Compare-Object -ReferenceObject $AllSRMRecoveryType -DifferenceObject $TagValues | Where-Object InputObject -NE "Placeholder"
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '<=')) {
            Write-Verbose -Message ("Create SRM Recovery Type tag for " + $c.InputObject)
            New-Tag -Category LicensingRestrictions -Name $c.InputObject -Server 'itdvmvc1.nd.gov'
        }
        ForEach ($c in ($Compare | Where-Object SideIndicator -EQ '=>')) {
            Write-Verbose -Message ("Remove SRM Recovery Type tag for " + $c.InputObject)
            Get-Tag -Category LicensingRestrictions -Name $c.InputObject -Server 'itdvmvc1.nd.gov' | Remove-Tag -Confirm:$false
        }

        $TagValues = $null
        $Compare = $null
        Write-Verbose -Message "End SRM Recovery Type Tag sync"
    }
    
    end {
        
    }
}