Tuesday, April 18, 2017

Sharepoint 2016 – Migration from Sharepoint 2015 – Upgrade Fail - Resolve Issue For–MissingWebPart , MissingAssembly , MissingSetupFile , MissingFeature

 

Add-PsSnapin Microsoft.SharePoint.PowerShell

$SqlServer = "DBServerName";
$SqlDatabase = "ContentDB";
$WebAppURL = "WebAppUrl";

Function MissingWebPart($Message)
        {
        $webparid = ((($Message -split "\[")[1]) -split "\]")[0];
        $webparid
        $testvalue = Run-SQLQuery -SqlServer $SqlServer -SqlDatabase $SqlDatabase -SqlQuery "SELECT Id, SiteId, DirName, LeafName, WebId, ListId, tp_ZoneID, tp_DisplayName from AllDocs inner join AllWebParts on AllDocs.Id = AllWebParts.tp_PageUrlID where AllWebParts.tp_WebPartTypeID = '$webparid'"
      
        Foreach($test in $testvalue )
        {
       
        if($test.SiteId)
        {
        $test
        $site = Get-SPSite -Limit all | where { $_.Id -eq  $test.SiteId }
        $web = $site | Get-SPWeb -Limit all | where { $_.Id -eq $test.WebId }
        $web.Url
        $file = $web.GetFile([Guid]$test.Id)
        $file.ServerRelativeUrl
        $file.delete()
        }
        }
        }
       
        Function MissingSetupFile($Message)
        {
        $SetupFileid = ((($Message -split "\[")[1]) -split "\]")[0];
        $SetupFileid
        $testvalue = Run-SQLQuery -SqlServer $SqlServer -SqlDatabase $SqlDatabase -SqlQuery "SELECT  Id,SiteId,DirName,LeafName,WebId,ListId from AllDocs where SetupPath = '$SetupFileid'"
        Foreach($test in $testvalue )
        {
        if($test.SiteId)
        {
        $test

        $site = Get-SPSite -Limit all | where { $_.Id -eq  $test.SiteId }
        $web = $site | Get-SPWeb -Limit all | where { $_.Id -eq $test.WebId }
        $web.Url
        $file = $web.GetFile([Guid]$test.Id)
        $file.ServerRelativeUrl
        $file.delete()
        }
        }
        }
       
        Function MissingFeature($Message)
        {
        $Featureid = ((($Message -split "\[")[2]) -split "\]")[0];
        $Featureid
        Remove-SPFeatureFromContentDB -ContentDB $SqlDatabase -FeatureId $Featureid
       
        }

        Function MissingAssembly($Message)
        {
        $Assemblyid = ((($Message -split "\[")[1]) -split "\]")[0];
        $Assemblyid
        $testvalue = Run-SQLQuery -SqlServer $SqlServer -SqlDatabase $SqlDatabase -SqlQuery "SELECT Id, Name, SiteId, WebId, HostId, HostType from EventReceivers where Assembly = '$Assemblyid'"
        #$testvalue
        Foreach($test in $testvalue )
        {
        if($test.SiteId)
        {
        $test
        $site = Get-SPSite -Limit all | where { $_.Id -eq  $test.SiteId }
        $web = $site | Get-SPWeb -Limit all | where { $_.Id -eq $test.WebId }
        $web.Url
        $list = $web.Lists | where {$_.Id -eq $test.HostId}
        $er = $list.EventReceivers | where {$_.Id -eq $test.Id}
        $er.Delete()
        #$file.delete()
        }
        }
        }

        function Remove-SPFeatureFromContentDB($ContentDb, $FeatureId, [switch]$ReportOnly)
        {
            $db = Get-SPDatabase | where { $_.Name -eq $ContentDb }
            [bool]$report = $false
            if ($ReportOnly) { $report = $true }
            $db.Sites | ForEach-Object {
                Remove-SPFeature -obj $_ -objName "site collection" -featId $FeatureId -report $report
                $_ | Get-SPWeb -Limit all | ForEach-Object {
                    Remove-SPFeature -obj $_ -objName "site" -featId $FeatureId -report $report
                }
            }
        }

        function Remove-SPFeature($obj, $objName, $featId, [bool]$report)
        {
            $feature = $obj.Features[$featId]
            if ($feature -ne $null) {
                if ($report) {
                    write-host "Feature found in" $objName ":" $obj.Url -foregroundcolor Red
                }
                else
                {
                    try {
                        $obj.Features.Remove($feature.DefinitionId, $true)
                        write-host "Feature successfully removed from" $objName ":" $obj.Url -foregroundcolor Red
                    }
                    catch {
                        write-host "There has been an error trying to remove the feature:" $_
                    }
                }
            }
            else {
                #write-host "Feature ID specified does not exist in" $objName ":" $obj.Url
            }
        }

            function Run-SQLQuery ($SqlServer, $SqlDatabase, $SqlQuery)
        {
            $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
            $SqlConnection.ConnectionString = "Server =" + $SqlServer + "; Database =" + $SqlDatabase + "; Integrated Security = True"
            $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
            $SqlCmd.CommandText = $SqlQuery
            $SqlCmd.Connection = $SqlConnection
            $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
            $SqlAdapter.SelectCommand = $SqlCmd
            $DataSet = New-Object System.Data.DataSet
            $SqlAdapter.Fill($DataSet)
            $SqlConnection.Close()
            $DataSet.Tables[0]
        }


Test-SPContentDatabase -Name $SqlDatabase -WebApplication $WebAppURL |
    ForEach-Object {
        $Category = $_.Category
        $Message = $_.Message
 
   if($Category -contains "MissingWebPart")
   {
   write-host "MissingWebPart"
   MissingWebPart $Message;
   }
   elseif ($Category -contains "MissingAssembly")
   {
    write-host "MissingAssembly"
    MissingAssembly $Message;
   }
   elseif ($Category -contains "MissingSetupFile")
   {
    write-host "MissingSetupFile"
    MissingSetupFile $Message;
   }

   elseif ($Category -contains "MissingFeature")
   {
    write-host "MissingFeature"
    MissingFeature $Message;
   }

    }

1 comment: