<#
.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-ITDVMwareVMTag {
    [CmdletBinding()]
    param
    (
        [string]
        $ComputerName,
        
        [string]
        $AppName,

        [string]
        $Dtap,

        <#[string]
        $OperatingSystem,#>

        [string]
        $StartupPriority,
        
        [string]
        $LicensingRestrictions,

        [string]
        $DRProtection,

        [string]
        $SRMRecoveryType
    )
    
    begin {
    }
    
    process {

        $VMs = Get-VM -Name $ComputerName | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
        If ($VMs) {
            ForEach ($VM in $VMs) {
                #AppName Tag
                If ($AppName) {
                    Write-Verbose ($VM.Name + ": AppName Tag Start")
                    $OldTag = Get-TagAssignment -Category AppName -Entity $VM
                    $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                    $OldTagName = $OldTag.tag.name
                    $NewTagName = $AppName

                    If ($OldTagName -ne $NewTagName) {
                        Write-Verbose ($VM.Name + " AppName old and new tags different")
                        Write-Verbose ("Old Tag " + $OldTagName)
                        Write-Verbose ("New Tag " + $NewTagName)
                            
                        If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$false -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }  #Errors with Remove-TagAssignment : Cannot bind argument to parameter 'TagAssignment' because it is null. --- but still works
                            
                        #Get-Tag -Category AppName -Name $NewTagName -Server $VIServer
                        New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category AppName -Name $NewTagName) -Server $VIServer
                            
                        Write-Verbose ($VM.Name + " tag updated " + $VIServer)#
                    }
                    Write-Verbose ($VM.Name + ": AppName Tag End")
                }

                $OldTag = $null
                $VIServer = $null
                $OldTagName = $null
                $NewTagName = $null
                
                #DTAP Tag
                If ($DTAP) {
                    Write-Verbose ($VM.Name + ": DTAP Tag Start")
                    $OldTag = Get-TagAssignment -Category DTAP -Entity $VM
                    $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                    $OldTagName = $OldTag.tag.name
                    $NewTagName = $DTAP

                    If ($OldTagName -ne $NewTagName) {
                        Write-Verbose ($VM.Name + " DTAP old and new tags different")
                        Write-Verbose ("Old Tag " + $OldTagName)
                        Write-Verbose ("New Tag " + $NewTagName)
                            
                        If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$false -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }  #Errors with Remove-TagAssignment : Cannot bind argument to parameter 'TagAssignment' because it is null. --- but still works
                            
                        #Get-Tag -Category AppName -Name $NewTagName -Server $VIServer
                        New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category DTAP -Name $NewTagName) -Server $VIServer
                            
                        Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                    }
                    Write-Verbose ($VM.Name + ": DTAP Tag End")
                }

                $OldTag = $null
                $VIServer = $null
                $OldTagName = $null
                $NewTagName = $null

                #Startup Priority
                If ($StartupPriority) {
                    Write-Verbose ($VM.Name + ": StartupPriority Tag Start")
                    $OldTag = Get-TagAssignment -Category 'StartupPriority' -Entity $VM
                    $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                    $OldTagName = $OldTag.tag.name
                    $NewTagName = $StartupPriority

                    If ($OldTagName -ne $NewTagName) {
                        Write-Verbose ($VM.Name + " StartupPriority old and new tags different")
                        Write-Verbose ("Old Tag " + $OldTagName)
                        Write-Verbose ("New Tag " + $NewTagName)
                            
                        If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$false -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }  #Errors with Remove-TagAssignment : Cannot bind argument to parameter 'TagAssignment' because it is null. --- but still works
                            
                        #Get-Tag -Category AppName -Name $NewTagName -Server $VIServer
                        New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category 'StartupPriority' -Name $NewTagName) -Server $VIServer
                            
                        Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                    }
                    Write-Verbose ($VM.Name + ": StartupPriority Tag End")
                }
                $OldTag = $null
                $VIServer = $null
                $OldTagName = $null
                $NewTagName = $null
                        
                <# OS Tag
                If ($OperatingSystem) {
                    Write-Verbose ($VM.Name + ": OS Tag Start")
                    $OldTag = Get-TagAssignment -Category "Operating System" -Entity $VM
                    $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                    $OldTagName = $OldTag.tag.name
                    $NewTagName = $OperatingSystem

                    If ($OldTagName -ne $NewTagName) {
                        Write-Verbose ($VM.Name + " OS old and new tags different")
                        Write-Verbose ("Old Tag " + $OldTagName)
                        Write-Verbose ("New Tag " + $NewTagName)

                        If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$False -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }
                        If ($NewTagName -ne "None") {
                            New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category 'Operating System' -Name $NewTagName) -Server $VIServer
                            Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                        }
                        else {
                            Write-Verbose ($VM.Name + " tag invalid or None " + $VIServer)
                        }
                    }

                    

                    Write-Verbose ($VM.Name + ": OS Tag End")
                }

                $OldTag = $null
                $VIServer = $null
                $OldTagName = $null
                $NewTagName = $null
                #>

                # Licensing Tag
                If ($LicensingRestrictions) {
                    Write-Verbose ($VM.Name + ": Licensing Restrictions Tag Start")
                    $OldTag = Get-TagAssignment -Category "LicensingRestrictions" -Entity $VM
                    $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                    $OldTagName = $OldTag.tag.name
                    $NewTagName = $LicensingRestrictions

                    If ($OldTagName -ne $NewTagName) {
                        Write-Verbose ($VM.Name + " Licensing old and new tags different")
                        Write-Verbose ("Old Tag " + $OldTagName)
                        Write-Verbose ("New Tag " + $NewTagName)

                        If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$False -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }
                        If ($NewTagName -ne "None") {
                            New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category 'LicensingRestrictions' -Name $NewTagName) -Server $VIServer
                            Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                        }
                        else {
                            Write-Verbose ($VM.Name + " tag invalid or None " + $VIServer)
                        }
                    }
                    Write-Verbose ($VM.Name + ": Licensing Tag End")
                }
                $OldTag = $null
                $VIServer = $null
                $OldTagName = $null
                $NewTagName = $null

                # SRM Recovery Type Tag
                If ($SRMRecoveryType) {
                    Write-Verbose ($VM.Name + ": SRM Recovery Type Tag Start")
                    $OldTag = Get-TagAssignment -Category "SRM Recovery Type" -Entity $VM
                    $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                    $OldTagName = $OldTag.tag.name
                    $NewTagName = $SRMRecoveryType

                    If ($OldTagName -ne $NewTagName) {
                        Write-Verbose ($VM.Name + " SRM Recovery Type old and new tags different")
                        Write-Verbose ("Old Tag " + $OldTagName)
                        Write-Verbose ("New Tag " + $NewTagName)

                        If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$False -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }
                        If ($NewTagName -ne "None") {
                            New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category 'SRM Recovery Type' -Name $NewTagName) -Server $VIServer
                            Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                        }
                        else {
                            Write-Verbose ($VM.Name + " tag invalid or None " + $VIServer)
                        }
                    }
                    Write-Verbose ($VM.Name + ": SRM Recovery Type Tag End")
                }
                $OldTag = $null
                $VIServer = $null
                $OldTagName = $null
                $NewTagName = $null

                # DR Protection
                If ($DRProtection) {
                    Write-Verbose ($VM.Name + ": DR Protection Tag Start")
                    $OldTag = Get-TagAssignment -Category "DR Protection" -Entity $VM
                    $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                    $OldTagName = $OldTag.tag.name
                    $NewTagName = $DRProtection

                    If ($OldTagName -ne $NewTagName) {
                        Write-Verbose ($VM.Name + " DR Protection old and new tags different")
                        Write-Verbose ("Old Tag " + $OldTagName)
                        Write-Verbose ("New Tag " + $NewTagName)

                        If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$False -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }
                        If ($NewTagName -ne "None") {
                            New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category 'DR Protection' -Name $NewTagName) -Server $VIServer
                            Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                        }
                        else {
                            Write-Verbose ($VM.Name + " tag invalid or None " + $VIServer)
                        }
                    }
                    Write-Verbose ($VM.Name + ": DR Protection Tag End")
                }
                $OldTag = $null
                $VIServer = $null
                $OldTagName = $null
                $NewTagName = $null


            }

            <# SRM Recovery Type Tags
                Write-Verbose ($VM.Name + ": SRM Recovery Type Tag Start")
                $OldTag = Get-TagAssignment -Category "SRM Recovery Type" -Entity $VM
                $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                $OldTagName = $OldTag.tag.name
                If ($VM.ExtensionData.summary.config.ManagedBy.Type -eq "placeholderVm" ) {
                    #If VM is placeholder
                    $NewTagName = "Placeholder"   
                }
                Else {
                    $NewTagName = $SPItem.SRM_RecoveryVMtype
                }

                If ($OldTagName -ne $NewTagName) {
                    Write-Verbose ($VM.Name + " SRM Recovery Type old and new tags different")
                    Write-Verbose ("Old Tag " + $OldTagName)
                    Write-Verbose ("New Tag " + $NewTagName)

                    If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$False -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }
                    If ($NewTagName -ne "None") {
                        New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category 'SRM Recovery Type' -Name $NewTagName) -Server $VIServer
                        Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                    }
                    else {
                        Write-Verbose ($VM.Name + " tag invalid or None " + $VIServer)
                    }
                }

                Write-Verbose ($VM.Name + ": SRM Recovery Type Tag End")#>

            $OldTag = $null
            $VIServer = $null
            $OldTagName = $null
            $NewTagName = $null

            <#VR RPO Tag
                Write-Verbose ($VM.Name + ": SRM Tag Start")
                $OldTag = Get-TagAssignment -Category 'VR RPO' -Entity $VM
                $VIServer = $VM.Uid.split('@')[1].split(':')[0]
                $OldTagName = $OldTag.tag.name
                $NewTagName = $SPItem.DR_Protection -replace "VMware: "
                        
                If ($OldTagName -ne $NewTagName) {
                    Write-Verbose ($VM.Name + " SRM old and new tags different")
                    Write-Verbose ("Old Tag " + $OldTagName)
                    Write-Verbose ("New Tag " + $NewTagName)
                            
                    If ($OldTag) { $OldTag | Remove-TagAssignment -Confirm:$false -ErrorAction SilentlyContinue; Write-Host ($VM.Name + " Tag Removed") }  #Errors with Remove-TagAssignment : Cannot bind argument to parameter 'TagAssignment' because it is null. --- but still works
                    #Get-Tag -Category AppName -Name $NewTagName -Server $VIServer
                    If ($NewTagName -ne "None") {
                        New-TagAssignment -Entity (Get-VM $VM.Name -Server $VIServer -OutVariable VM) -Tag (Get-Tag -Server $VIServer -Category 'VR RPO' -Name $NewTagName) -Server $VIServer
                        Write-Verbose ($VM.Name + " tag updated " + $VIServer)
                    }                            
                    else {
                        Write-Verbose ($VM.Name + " tag invalid or None " + $VIServer)
                    }
                            
                }#>
        }
        Else {
                
        }
        #}
        <#catch {
                Write-Error ($VM.Name + " tag errored")
                Write-Error $error[-1]
            }#>
    }
    end {
        
    }
}