2017-11-29 22:41:16

SharePoint Missing Setup Files Webpages

As like from my last post covers any missing webparts, that won't actually remove any webpages the SharePoing Admins created along the way.
So here's the next solution should you need to remove those web pages, if you don't wish to re-install the third party solution.

EXample Error Message:

Category        : MissingSetupFile
Error           : True
UpgradeBlocking : False
Message         : File [Features\K2WebDesignerV2\PageTemplates\K2WebDesigner.aspx] is referenced [4] times in the database 
                  [WSS_Content_OpsCentre], but is not installed on the current farm. Please install any feature/solution which contains 
                  this file.
Remedy          : One or more setup files are referenced in the database [WSS_Content_OpsCentre], but are not installed on the current 
                  farm. Please install any feature or solution which contains these files.
Locations       : 
Here's my script, just as effective and vulgur as my last one. :D
########################################################################################################################## 
# Author: Zewwy (Aemilianus Kehler)
# Date:   Oct 28, 2017
# Script: Delete-SPWebFiles
# This script allows to remove Web Parts from the Web Parts Gallery.
# Cudos to Phil Childs from get-spscripts.com
# Required parameters: 
#   A valid  SharePoint Site Collection URL and a string (case insensitive :D) for the particular SP File.
#   Best to be run from a SharePoint Mgmt Console with an account that has collection admin on the Web URL 
##########################################################################################################################

##########################################################################################################################
#   Variables
##########################################################################################################################
#The Varible used to display wrong URL provided, or not found
$BadURL = @("Sorry, the string you entered ","is not a valid Site Collection.")
#File not found from SQL query response
$BadfileQry = "Sorry mate but it seems the dingo ate your file."
#MyLogoArray
$MylogoArray = @("#####################################","# This script is brought to you by: #","#                                   #","#             Zewwy                 #","#                                   #","#####################################"," ")
#Static Variables
$ScriptName = "Delete-SPWebFiles; cause some SharePoint files just suck ass.`n"
$SQLServer = "Server\instance"
$DB = "WSS_Content"

$pswheight = (get-host).UI.RawUI.MaxWindowSize.Height
$pswwidth = (get-host).UI.RawUI.MaxWindowSize.Width

##########################################################################################################################
#   Functions
##########################################################################################################################

#function takes in a name to alert confirmation of deletion of a web part, returns true or false
function confirm($name)
{
    #function variables, generally only the first two need changing
    $title = "Confirm SharePoint File Deletion!"
    $message = "You are about to delete SharePoint File: $name"

    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "This means Yes"
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "This means No"

    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

    $result = $host.ui.PromptForChoice($title, $message, $Options, 0)
    Write-Host " "
    Switch ($result)
        {
              0 { Return $true }
              1 { Return $false }
        }
}

#Function to Centeralize Write-Host Output, Just take string variable parameter and pads it
#Nerd Level over 9000!!! Ad-hoc Polymorphic power time!!
function Centeralize()
{
  param(
  [Parameter(Position=0,Mandatory=$true)]
  [string]$S,
  [Parameter(Position=1,Mandatory=$false,ParameterSetName="color")]
  [string]$C
  )
    $sLength = $S.Length
    $padamt =  "{0:N0}" -f (($pswwidth-$sLength)/2)
    $PadNum = $padamt/1 + $sLength #the divide by one is a quick dirty trick to covert string to int
    $CS = $S.PadLeft($PadNum," ").PadRight($PadNum," ") #Pad that shit
    if ($C) #if variable for color exists run below
    {    
        Write-Host $CS -ForegroundColor $C #write that shit to host with color
    }
    else #need this to prevent output twice if color is provided
    {
        $CS #write that shit without color
    }
}

#Function provided by phil to check the content database via the sharepoint front end server
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]
}

#Start actual script by posting and asking user for responses
foreach($L in $MylogoArray){Centeralize $L "green"}
Centeralize $ScriptName "White"
#Notify User to enter the Site Collection URL then check if it exits.
Centeralize "Please enter a SP Site Collection URL`n"
Write-host "SharePoint Site Collection URL: " -ForegroundColor Magenta -NoNewline
$SPSitecolstr = Read-Host
Write-Host " "
Centeralize "Verifying SharePoint Site Collection URL, Please Wait...`n" "White"
if ($SPSiteCol=Get-SPSite $SPSitecolstr -EA SilentlyContinue)
{
    $DB = $SPSiteCol.ContentDatabase.Name
    $SQLServer = $SPSiteCol.ContentDatabase.Server
    Centeralize "Phhhh, ok good guess, that is a site collection here's what the systems got:`n" "Cyan"
    Centeralize "SqlSever\Instance: $SQLServer Using Database: $DB `n" "White"
    Write-host "What file you lookin' for boy?: " -ForegroundColor Magenta -NoNewline
    $StupidFile = Read-Host
    Write-Host " "
    $Shitfile = "'%"+$StupidFile+"%'"
    try
    {
        [System.Collections.ArrayList]$ShitArray = Run-SQLQuery -SqlServer $SQLServer -SqlDatabase $DB -SqlQuery "SELECT * from AllDocs where SetupPath LIKE $Shitfile" -ErrorAction Stop
    }
    catch
    {
        $BadfileQry += " This `"$StupidFile`""
        Centeralize "$BadFileQry" "Red"
        Break
    }
    $ShitArray.RemoveAt(0) #For some reason the query's first result (or more to say the first element in the object array) was the result amount, so remove it from the array
    #$ShitArray | select Id, SiteId, DirName, LeafName, WebId, ListId | Format-List
    #$ShitArray.Count #entered for testing purposes, should be removed
    foreach($ShittyFile in $ShitArray)
    {
        $SPWeb = $SPSiteCol | Get-SPWeb -limit all | where {$_.ID -eq $ShittyFile.WebId}
        $SPWURL = $SpWeb.Url
        if(!$SPWURL.EndsWith("/")){$SPWURL+="/"}
        $file = $SPWeb.GetFile([Guid]$ShittyFile.ID)
        $fn = $file.Name
        Centeralize "File Found: $SPWURL$fn" "Yellow"
        if (confirm $SPWURL$fn)
                {
                    Centeralize "Deleting Web File: $SPWURL$fn`n" "Red"
                    $file.Delete() 
                } 
    }   
}
else
{
    $SPSitecolstr = "`""+$SPSitecolstr+"`" "
    $BadURL = $BadURL[0] + $SPSitecolstr + $BadURL[1]
    foreach($str in $BadURL){Centeralize $str "red"}
}

Posted by Aemilianus Kehler | Permanent link