Find Content Types In Docs

FindContentTypesInDocs.ps1

# FindContentTypesInDocs.ps1
# Find all Documents that use a given Content Type and change those documents
# Just display the URL of the document that uses the content type.
# M. Cox 3/22/2013
# Takes, as input, the URL of a Site Collection and the path to an XML file that lists all the Content Types.
# Syntax example:
# .\FindContentTypesInDocs.ps1 http://bmphub.int/sites/it "D:\Scripts\ContentTypes.xml"
param(
    [string] $siteUrl = $(Throw "-- You must specify the URL of a site collection as parameter 1."), #required parameter
 [string] $xmlFilePath = $(Throw "-- You must specify the pathname of an XML file as parameter 2.") #required parameter
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
Write-host "FindContentTypesInDocs: Processing Site Colleciton: " $siteUrl -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$site = get-spsite $siteURL
# get the content types XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))

foreach ( $web in $site.allwebs)
{
    Write-host "  Processing subsite " $web.url -ForegroundColor DarkBlue
   
    $webScope = Start-SPAssignment
   
    foreach ( $list in $web.lists )
    {
        if ($list.BaseType -ne 'DocumentLibrary') {continue}
       
        write-host "    Processing list " $list.Title -ForegroundColor DarkGreen
   
        # loop over content types listed in the input XML file
        foreach( $ctElement in $XmlFileInput.ContentTypes.ContentType)
        {
            $contentTypeToRemove = $ctElement.Name
           
            # write-host "      Checking for Content Type " $contentTypeToRemove -ForegroundColor Gray
           
            if ($web.AvailableContentTypes[$contentTypeToRemove] -eq $null ){continue} # web does not use Content Type
               
            if ($list.ContentTypes[$contentTypeToRemove] -eq $null ){continue} # list does not use Content Type
       
            write-host "      Found " $contentTypeToRemove " in list " $list.Title -ForegroundColor DarkGray
           
            $list.items | ForEach-Object {
                if ($_.ContentType.Name -eq $contentTypeToRemove)  # does item have the content?
                {
                    try
                    {
                        write-host "        Found content '" $contenttypeToRemove "' used in file at URL '" $_.Url "'" -BackgroundColor Yellow -ForegroundColor Black
                        # write-host "           File CheckOutType = '" $_.File.CheckOutType "'"
                    }
                    Catch
                    {
                        Write-host "Web       :" $Web.url -ForegroundColor Red
                        Write-host "List      :" $list.title -ForegroundColor Red
                        Write-host "Exception :" $_.exception.message -ForegroundColor Red                           
                    }
                }
            }   
        } # foreach $contenttypeInXmlFile
    } # foreach $list
    Stop-SPAssignment $webScope
} # foreach $web 
$site.dispose
Stop-SPAssignment $siteScope

No comments:

Post a Comment