<#
.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 Get-ITDVMwareVMTagReport {
    [CmdletBinding()]
    param (
        [Parameter(
            ValueFromPipeline = $true,
            ParameterSetName = 'VMName'
        )]
        [string]
        $VMName
    )
    
    begin {
        #$Result = [System.Collections.ArrayList]@()
    }
    
    process {
        If ($PSBoundParameters.ContainsKey('VMName')) {
            # do nothing
        }
        Else {
            $VMName = Get-VM | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
        }
        
        ForEach ($vn in $VMName) {
            $HostName = $vn.split('.')[0]
            Write-Verbose -Message "Looking up Cmdb Object named $HostName"
            $Cmdb = Get-ITDServiceNowRecord -Table cmdb_ci_server -Filter "name=$HostName"
            $VM = Get-VM -Name $vn | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
            $OldTags = Get-TagAssignment -Entity $VM

            $obj = [PSCustomObject]@{
                VMName                = $vn;
                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 $vn DTAP Tag to " + $Cmdb.environment.display_value)
                    $obj.DTAP = $false
                }
                Else {
                    $obj.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 $vn AppName Tag to " + $Cmdb.u_nd_application_svc.display_value)
                    $obj.AppName = $false
                }
                Else {
                    $obj.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 $vn Licensing Restrictions Tag to " + $Cmdb.u_nd_licensing_restrictions.display_value)
                    $obj.LicensingRestrictions = $false
                }
                Else {
                    $obj.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 $vn SRM Recovery Type Tag to " + $Cmdb.u_srm_recovery_type.display_value)
                    $obj.SRMRecoveryType = $false
                }
                Else {
                    $obj.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 $vn DR Protection Tag to " + ($Cmdb.u_nd_dr_protection.display_value -replace "VMware: ") )
                    $obj.DRProtection = $false
                }
                Else {
                    $obj.DRProtection = $true
                }
            }
        }
    }
    
    end {
        Write-Output $obj
    }
}