Hi,
We had a requirement of deleting one of the content type in all the site collections under a Web Application.
Below is the script where inputs have been provided in csv file
//DeleteContentType.bat
cd /d %~dp0
@echo off
powershell.exe -noexit .\PSDeleteContentType.ps1 .\DeleteContentType.csv
//DeleteContentType.csv
WebAppUrl, ContentType
https://webappurl/, content type name
//PSDeleteContentType.ps1
#///<summary>
#/* Script Name : PSDeleteContentType
# * Purpose : Used to delete the content type from the web application
#///</summary>
#-----Input parameters to the script
param($inputFile)
#------To check if Sharepoint cmdlets are registered are registered
if ((Get-PSSnapin -Name Microsoft.SharePoint. Powershell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint. Powershell
}
#----------------------------- ------------------------------ ------------------------------ ----------
#----------------------------- ------------Start Logging----------------------- ----------------------
$filepath = $MyInvocation.MyCommand. Definition
$directorypath = [System.IO.Path]:: GetDirectoryName($filepath)
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\DeleteContentType_Log_$ LogTime.txt"
Start-Transcript -Path $LogFile -Force
#----------------------------- ------------------------------ ------------------------------ ----------
#----------------------------- ------------------------------ ------------------------------ ----------
# Function to delete the content type
function DeleteContentType($webAppUrl, $contentType)
{
$wa = Get-SPWebApplication $webAppUrl
if($wa -ne $null)
{
$allSites = $wa | Get-SPSite -Limit all
foreach($spsite in $allSites)
{
}
}
else
{
Write-Host 'Not a valid Web application url...'
}
}
#----------------------------- ------------------------------ ------------------------------ ----------
#----------------------------- ------------------------------ ------------------------
#loop through a csv file
Import-Csv $inputFile | ForEach{
$webAppUrl = $_.WebAppUrl
$contentType = $_.ContentType
try
{
write-host "Delete the Content Type $contentType from $webAppUrl..."
DeleteContentType $webAppUrl $contentType
write-host "Completed..."
}
catch
{
write-host "Completed with errors..."
Write-Output $_
}
}
#----------------------------- ------------------------------ ------------------------------ ----------
#----------------------------- ------------End Logging----------------------- ------------------------
Stop-Transcript
#----------------------------- ------------------------------ ------------------------------ ----------
No comments:
Post a Comment