Thursday, February 1, 2018

PnP Power shell script to find content types in sub webs

Hi,

I had 2 separate csv files one with site collection urls and another with content type names

Now I have to automate of checking the content types in each and every sub webs under the site collections.

After find the content type, have to display root site url,web site url, List Title, List Url, Content type and content type group name.

Also we have to generate this result as csv file output.

For this we have written the PnP PowerShell script as below

<Inputs>
<ConnectSPOnline SiteURL = "siteurl" UserName = "username" Password = "password"></ConnectSPOnline>
</Inputs>

//Contenttypesinputs.csv
contenttypes
Document

//SiteCollectionInputs.csv
sitecollections
siteurl

//ContentTypesListScript.ps1
############################################################## Logging #########################################
  
$date= Get-Date -format MMddyyyyHHmmss 
start-transcript -path .\Log_$date.doc  

################################################### Get input parameters from XML ###############################  
# Get content from XML file 
[xml]$xmlData=Get-Content ".\ContentTypeListInputs.xml" 

# ConnectSPOnline node 
[System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline 
$userName=$connectSPOnline.UserName 
$password=$connectSPOnline.Password 

#$ctypescsvinputs = Import-CSV C:\JB\DeploymentScript\Contenttypesinputs.csv
$ctypescsvinputs = Import-CSV .\Contenttypesinputs.csv

#$csvoutputfile="C:\JB\DeploymentScript\Contenttypeslist $(get-date -f yyyy-MM-dd).csv"                         
$csvoutputfile="Contenttypeslist $(get-date -f yyyy-MM-ddhh:mm:ss).csv"                          
#$csvoutputfile = Export-CSV .\Contenttypeslist.csv

$sitecollectioninputs= Import-CSV .\SiteCollectionInputs.csv


function GetCredentials() 
{  
    $Headers = 'SiteCollectionUrl,WebUrl,ListTitle,ListUrl,ContentTypeName,ContentGroup'
                Add-Content $csvoutputfile $Headers   
                foreach($siteURL in $sitecollectioninputs.sitecollections)
                {             
                                write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL 
                                # Convert password to secure string        
                                $secureStringPwd = ConvertTo-SecureString $password -AsPlainText -Force         
                               
                                # Get the credentials       
                               
                                $O365Cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($userName,(ConvertTo-SecureString -String $password -AsPlainText -Force))
                                $O365Cred | Export-Clixml .\O365Cred.xml
                               
                                #You can import the xml password hash on the same computer as the same user
                                $O365Cred = Import-Clixml .\O365Cred.xml
                               
                                # Connect to SP online site                                          
                                Connect-PnPOnline -Url $siteURL -Credentials $O365Cred                            
                                $rootlists=Get-PnPList -Includes {RootFolder.ServerRelativeUrl}
               
                                foreach($rootlist in $rootlists)
                                {
                                                $rootcontenttypes=Get-PnPContentType -List $rootlist.Title
                                                foreach($rootcontenttype in $rootcontenttypes)
                                                {
                                                               
                                                                foreach($ctypescsvinput in $ctypescsvinputs.contenttypes)
                                                                {
                                                                                #write-host $cell             
                                                                                if($ctypescsvinput -eq $rootcontenttype.Name)
                                                                                {
                                                                                               
                                                                                                $rootListTitle=$rootlist.Title                                                                       
                                                                                                $rootListUrl=$rootlist.RootFolder.ServerRelativeUrl                                                                        
                                                                                                if($rootListUrl -notmatch "_catalogs" -and $rootListUrl -notmatch "Style Library" -and $rootListUrl -notmatch "SiteAssets")
                                                                                                {
                                                                                                                write-host "Root Site Url:"$siteURL                                                                                                         
                                                                                                                write-host "List Title:"$rootListTitle                                                                                                         
                                                                                                                write-host "List Url:"$rootListUrl
                                                                                                                $rootContentTypeName=$rootcontenttype.Name
                                                                                                                write-host "Content Type Name:"$rootContentTypeName
                                                                                                                $rootContentTypeGroup=$rootcontenttype.Group
                                                                                                                write-host "Content Type Group:"$rootContentTypeGroup
                                                                                                                Add-Content $csvoutputfile "$siteURL,$siteURL,$rootListTitle,$rootListUrl,$rootContentTypeName,$rootContentTypeGroup"
                                                                                                }                                                                                             
                                                                                }
                                                                }
                                                }
                                               
                                }
                                $subwebs=Get-PnPSubWebs -Recurse
                                foreach($subweb in $subwebs)
                                {
                                                #write-host $subweb.Title                          
                                                Connect-PnPOnline -Url $subweb.Url -Credentials $O365Cred
                                                $lists=Get-PnPList -Includes {RootFolder.ServerRelativeUrl}
                                                foreach($list in $lists)
                                                {                                            
                                               
                                                                $contenttypes=Get-PnPContentType -List $list.Title
                                                                foreach($contenttype in $contenttypes)
                                                                {
                                                                                #write-host "Content Type:"$contenttype.Name
                                                                                #output to host, file or directly pipe the command above.
                                                                                foreach($ctypescsvinput in $ctypescsvinputs.contenttypes)
                                                                                {
                                                                                                #write-host $cell             
                                                                                                if($ctypescsvinput -eq $contenttype.Name)                                                                        
                                                                                                {
                                                                                                                #if($list.RootFolder.ServerRelativeUrl -contains "SiteAssets" -or $list.RootFolder.ServerRelativeUrl -contains "_catalogs" -or $list.RootFolder.ServerRelativeUrl -contains "Style")
                                                                                                                #{
                                                                                                                $subwebListTitle=$list.Title
                                                                                                                $subwebListUrl=$list.RootFolder.ServerRelativeUrl                                                                                                                                                                         
                                                                                                                if($subwebListUrl -notmatch "_catalogs" -and $subwebListUrl -notmatch "Style Library" -and $subwebListUrl -notmatch "SiteAssets")
                                                                                                                {
                                                                                                                                $subWebUrl=$subweb.Url
                                                                                                                                write-host "Sub Web Url:"$subWebUrl
                                                                                                                                               
                                                                                                                                write-host "List Title:"$subwebListTitle
                                                                                                                               
                                                                                                                                write-host "List Url:"$subwebListUrl
                                                                                                                                $subwebContentTypeName=$contenttype.Name
                                                                                                                                write-host "Content Type:"$subwebContentTypeName
                                                                                                                                $subwebContentTypeGroup=$contenttype.Group
                                                                                                                                write-host "Content Type Group:"$subwebContentTypeGroup
                                                                                                                                Add-Content $csvoutputfile "$siteURL,$subWebUrl,$subwebListTitle,$subwebListUrl ,$subwebContentTypeName,$subwebContentTypeGroup"
                                                                                                                }
                                                                                                }
                                                               
                                                                                }
                                                                }      
                                   
                                                }
                                }
               
               
                }             
               
}
#################################################################  Initiate #####################################
function Initiate() 
     write-host -ForegroundColor Green "Initiating the script.................. "  
                 GetCredentials
                Disconnect-PnPOnline 
                 write-host -ForegroundColor Green "Completed!!!!"  
}
#################################################################################################################
 
Initiate 
Stop-Transcript 
  

Output




SiteCollectionUrl,WebUrl,ListTitle,ListUrl,ContentTypeName,ContentGroup
rootsiteurl,weburl,Bibliothèque de styles,/sites/DevSite/Style Library,Document,Types de contenu de document

No comments:

Post a Comment