[CmdletBinding()]
Param(
    [string]$ScriptName
)

$AllPSUScripts = Get-PSUScript
If ($ScriptName){
    $AllPSUScripts = $AllPSUScripts | Where-Object Name -eq $ScriptName
}
$Folders = $AllPSUScripts | Group-Object Folder

ForEach ($Folder in $Folders) {
    Write-Verbose -Message ("Start folder " + $Folder.Name) -Verbose
    $ScriptsInFolder = $null
    $TagNamesEnforced = $null
    $TagNamesExisting = $null
    $TagNamesToAssign = $null

    switch ($Folder.Name) {
        { $_ -like "Infra-VMware*" } { $TagNamesEnforced = 'VMware-Admin_Modify' }
        Default { Write-Verbose -Message ("Folder " + $Folder.Name + " does not have enforced tags")}
    }

    If ($TagNamesEnforced) {
        $ScriptsInFolder = $AllPSUScripts | Where-Object Folder -EQ $Folder.Name
        ForEach ($PSUScript in $ScriptsInFolder) {
            $TagNamesExisting = $null
            $TagNamesToAssign = $null
            Write-Verbose -Message ("Start " + $PSUScript.Name) -Verbose
            If($PSUScript.Tag.Name -notcontains $TagNamesEnforced){
                Write-Verbose -Message ($PSUScript.Name + " enforced tag missing, attempting to fix") -Verbose
                $TagNamesExisting = $PSUScript.Tag.Name
                #Get-PSUScript -Name $PSUScript.Name | Set-PSUScript -Tag @('VMware-Admin_Execute','Network-Engineer_Execute')  #### WORKING EXAMPLE
                [string[]]$TagNamesToAssign = $TagNamesEnforced
                If ($TagNamesExisting){$TagNamesToAssign += $TagNamesExisting}                
                Get-PSUScript -Name $PSUScript.Name | Set-PSUScript -Tag $TagNamesToAssign
            }
            Write-Verbose -Message ("End " + $PSUScript.Name) -Verbose
        }
    }
    Write-Verbose -Message "End $Folder" -Verbose
}
