MyITGuy
5/14/2019 - 1:34 PM

Clear-AppVO365DocToIdMappingSearchQueries

#region Clear-AppVO365DocToIdMappingSearchQueries
function Clear-AppVO365DocToIdMappingSearchQueries {
    [CmdletBinding()]
    PARAM(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
        [System.Guid[]]
        $PackageId = @("EB51A3D4-B87F-4676-AD01-5D583ABE053C")
        ,
        [switch]
        $Analyze
    )

    begin {
        Write-Verbose $MyInvocation.MyCommand
    }

    process {
        foreach ($Id In $PackageId) {
            Write-Verbose "Id: $($Id)"
            try {
                $SID = (New-Object System.Security.Principal.NTAccount($env:USERNAME)).Translate([System.Security.Principal.SecurityIdentifier]).Value
                Write-Verbose "SID: $($SID)"
                $ProfileNamesRegistryPath = "Registry::HKEY_CURRENT_USER\Software\Microsoft\AppV\Client\Packages\$($Id)\REGISTRY\USER\$($SID)\Software\Microsoft\Office\16.0\Common\Identity\Profiles"
                Write-Verbose "ProfileNamesRegistryPath: $($ProfileNamesRegistryPath)"
                $ProfileNamesRegistryPathExists = Test-Path -Path $ProfileNamesRegistryPath
                Write-Verbose "ProfileNamesRegistryPathExists: $($ProfileNamesRegistryPathExists)"
                if ($ProfileNamesRegistryPathExists -eq $true) {
                    $ProfileNames = Get-ChildItem -Path $ProfileNamesRegistryPath | Where-Object { $_.Name.EndsWith('_ADAL') } | Select-Object -ExpandProperty PSChildName
                    Write-Verbose "ProfileNames: $($ProfileNames -join ',')"
                    foreach ($ProfileName In $ProfileNames) {
                        $Properties = @{
                            PackageId                      = $Id
                            SID                            = $SID
                            ProfileNamesRegistryPath       = $ProfileNamesRegistryPath
                            ProfileNamesRegistryPathExists = $ProfileNamesRegistryPathExists
                            ProfileName                    = $ProfileName
                            ProfileNameRegistryPath        = "Registry::HKEY_CURRENT_USER\Software\Microsoft\AppV\Client\Packages\$($Id)\REGISTRY\USER\$($SID)\Software\Microsoft\Office\16.0\Common\Identity\DocToIdMapping\$($ProfileName)"
                            ProfileNameRegistryPathExists  = $null
                            ItemProperties                 = $null
                            ItemPropertiesCount            = $null
                            Removed                        = $null
                        }
                        $Properties.ProfileNameRegistryPathExists = Test-Path -Path $Properties.ProfileNameRegistryPath
                        if ($Properties.ProfileNameRegistryPathExists) {
                            $Properties.ItemProperties = Get-ItemProperty -Path $Properties.ProfileNameRegistryPath -Name '*&entitytypes=text,people' -ErrorAction SilentlyContinue | Get-Member | Where-Object { $_.MemberType -eq 'NoteProperty' -and $_.Name.EndsWith('&entitytypes=text,people') } | Select-Object -ExpandProperty Name
                            $Properties.ItemPropertiesCount = $Properties.ItemProperties | Measure-Object | Select-Object -ExpandProperty Count
                        }
                        if ($Properties.ItemProperties -and !$Analyze) {
                            try {
                                Remove-ItemProperty -Path $Properties.ProfileNameRegistryPath -Name '*&entitytypes=text,people'
                                $Properties.Removed = $true
                            }
                            catch {
                                $Properties.Removed = $false
                            }
                        }
                        $obj = New-Object -TypeName psobject -Property $Properties
                        Write-Output -InputObject $obj
                    }    
                }
            }
            catch {
                Write-Host $_.Exception.Message
            }
        }
    }

    end {
    }
}
#endregion Clear-AppVO365DocToIdMappingSearchQueries