Pages

Tuesday 11 May 2021

Pnp Powershell Scripts

Pnp Powershell Examples:


Install Pnp Powershell:


Invoke-Expression (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/pnp/PnP-PowerShell/master/Samples/Modules.Install/Install-SharePointPnPPowerShell.ps1')



PreChecks Script:


cls

Import-Module SharePointPnPPowerShellOnline -WarningAction SilentlyContinue



#Migration Tracker

$record = Import-Csv -Path "D:\Users\Documents\scripts\test.csv"

$outCsv = "D:\UserData\Documents\scripts\SourceOutput.csv"

"SourceURL`tWebs`tList Title`tList Count`tInfoPath" > $outCsv

foreach($data in $record){

$SiteURL= $data.SourceURL

#EndRegion


Get-Date -Format “dddd dd/MM/yyyy HH:mm”

Write-Host "----------------------------------------------Source Post Checks Started------------------------------------------------------------------------"

#Function to Get Lists and Libraries of a web

Function Get-PnPListInventory([Microsoft.SharePoint.Client.Web]$Web)

{

    write-Host "-----" + $web.Title + "------------------------------------------------------------------------"

        Write-Host "---------------------------------------------------------------------------------------------------"


    


    #Region List Operations

    $ListItemCollection = @()

    

   

    #$listsOnPrem= Get-PnPList -Web $Web |Where-Object {$_.Title -ne 'Form Templates' -and $_.Title -ne 'Access Requests' -and $_.Title -ne 'appdata' -and $_.Title -ne 'Composed Looks' -and $_.Title -ne 'Content type publishing error log' -and $_.Title -ne 'Converted Forms' -and  $_.Title -ne 'Master Page Gallery' -and$_.Title -ne 'NintexSnippets' -and $_.Title -ne 'NintexTemplates' -and  $_.Title -ne 'Theme Gallery' -and $_.Title -ne 'User Information List' -and $_.Title -ne 'Web Part Gallery'}

    $listsOnPrem= Get-PnPList -Web $Web |Where-Object {$_.Title -ne 'Access Requests' -and $_.Title -ne 'appdata' -and $_.Title -ne 'Composed Looks' -and $_.Title -ne 'Content type publishing error log' -and $_.Title -ne 'Converted Forms' -and  $_.Title -ne 'Master Page Gallery' -and $_.Title -ne 'NintexSnippets' -and $_.Title -ne 'NintexTemplates' -and  $_.Title -ne 'Theme Gallery' -and $_.Title -ne 'User Information List' -and $_.Title -ne 'Web Part Gallery' -and $_.Title -ne 'Maintenance Log Library' -and $_.Title -ne 'List Template Gallery' -and $_.Title -ne 'NintexWorkflowHistory' -and $_.Title -ne 'NintexWorkflows' -and $_.Title -ne 'Reporting Metadata' -and $_.Title -ne 'Reporting Templates' -and $_.Title -ne 'Site Template Gallery' -and $_.Title -ne 'MicroFeed' -and $_.Title -ne 'appfiles'}

    Write-Host "-----Lists Count: " $listsOnPrem.count -ForegroundColor Yellow

    



    Write-Host "------------List Operations------------------------------------------------------------------------"

    #Lists Operation

        forEach ( $list in $listsOnPrem) 

        {

        

            $listTitle = $list.Title

            $listType = $list.BaseTemplate

            $listUrl = $list.DefaultViewUrl           

            $lsCount = $list.ItemCount


            $row = new-object PSObject

                 

            Add-member -inputObject $row -memberType NoteProperty -Name “List Title” -value $list.Title

            


          

             

            #Add-member -inputObject $row -memberType NoteProperty -Name “List Item Count” -value $list.ItemCount

             $NbOfFiles = 0    

            if($listType -eq 101)

            {

                #write-host "total - " $list.ItemCount

                $folderItems = Get-PnPListItem -Web $Web -List $list -Fields "FileLeafRef","FolderChildCount", "ItemChildCount"

               

                foreach ($folderitem in $folderItems) {

                    if (($folderitem.FileSystemObjectType) -eq "File") {

                        $NbOfFiles += 1

                    }

                    

                }

                Add-member -inputObject $row -memberType NoteProperty -Name “List Item Count” -value $NbOfFiles

            }

            else

            {

                Add-member -inputObject $row -memberType NoteProperty -Name “List Item Count” -value $list.ItemCount

            }  


                #Region More than 10k items        

            

                if($lsCount -ge 10000)

                {

                

                    Write-Host "10K Lists: " $listTitle -ForegroundColor Red

                }

                

                



                $currList = Get-PnPList -Web $Web $listTitle -Includes ContentTypes, Forms 

                $listForms = $currList.Forms 

                $flag = 0

                

                forEach ( $listFm in $listForms )

                {  $infoPath = ""

                  $listPath = $listFm.ServerRelativeUrl

                  if ( $listPath -like '*displayifs.aspx' -or $listPath -like '*newifs.aspx' -or $listPath -like '*FormServer.aspx') 

                  {

                      $flag = 1                          

                  }

                }

               #Region Infopath

                if($list.BaseTemplate -eq 115)

                {    $infoPath = "Found at URL " + $listUrl

                    

                    Write-Host 'InfoPath Found in Library ' $listTitle " at URL " $listUrl -ForegroundColor Red 

                   

                }

                        

                if($flag -eq 1)

                {   $infoPath = "Found at URL " + $listUrl

                    Write-Host 'InfoPath Found in List ' $listTitle " at URL " $listUrl -ForegroundColor Red 

                    

                }              

                #End Region Infopath

          

             $ListItemCollection += $row

             "$SiteURL`t$($web.Url)`t$listTitle`t$lsCount`t$infoPath" >> $outCsv

       }            

    $ListItemCollection | Format-Table 

    $ListItemCollection = @()

    Write-Host "----------------------------------------------List Operations End------------------------------------------------------------------------"

    #End Region

}


 #Region Source Pre Checks


    Connect-PnPOnline -Url $SiteURL -UseWebLogin


    $webOnPrem = Get-PnPWeb

    $OnPremSubsites= Get-PnPSubWebs -Recurse |Where-Object {$_.Title -ne 'Blogs'}

    $OnPremRootSubsites= Get-PnPSubWebs  |Where-Object {$_.Title -ne 'Blogs'}

    Write-Host "--Site URL: " $webOnPrem.Url -ForegroundColor Green

    Write-Host  "--Root subsites Count: "  $OnPremRootSubsites.Count -ForegroundColor Yellow

    Write-Host  "--All Subsites(Recursive) Count: "  $OnPremSubsites.Count -ForegroundColor Yellow

    


    Get-PnPListInventory $webOnPrem

    if($OnPremSubsites.Count -gt 0)

    {

        Foreach ($web in $OnPremSubsites)

        {

            $OnPremSubSubsites= Get-PnPSubWebs -Web $web -Recurse |Where-Object {$_.Title -ne 'Blogs'}

                Write-Host "--SubSite URL: " $web.Url -ForegroundColor Green

                Write-Host  "--Subsites Count: "  $OnPremSubSubsites.Count -ForegroundColor Yellow

            Get-PnPListInventory $web

        }            

        Disconnect-PnPOnline

        Write-Host "----------------------------------------------Next Item------------------------------------------------------------------------"

     }


 Write-Host "----------------------------------------------Success!!!------------------------------------------------------------------------"

 Get-Date -Format “dddd dd/MM/yyyy HH:mm”

 }

 #EndRegion"


Get InfoPath, Workflow, External Users:

cls

Import-Module SharePointPnPPowerShellOnline -WarningAction SilentlyContinue



#Migration Tracker

$record = Import-Csv -Path "D:\UserData\Documents\scripts\test.csv"

$outCsv = "D:\UserData\Documents\scripts\InfoPath.csv"

$outCsv1 = "D:\UserData\Documents\scripts\WorkFlows.csv"

$outCsv2 = "D:\UserData\Documents\scripts\ExternalUsers.csv"


"SourceURL`tWebs`tList Title`tList Count`tInfoPath" > $outCsv

"SourceURL`tWebURL`tList Title`tWorkflow Name`tEnabled`tModified`tInstantiationUrl" > $outCsv1

"SourceURL`tExternal User Name`tExternal User Email" > $outCsv2

foreach($data in $record){

$SiteURL= $data.SourceURL

#EndRegion


Get-Date -Format “dddd dd/MM/yyyy HH:mm”

Write-Host "----------------------------------------------Source Post Checks Started------------------------------------------------------------------------"

#Function to Get Lists and Libraries of a web

Function Get-PnPListInventory([Microsoft.SharePoint.Client.Web]$Web)

{

    write-Host "-----" + $web.Title + "------------------------------------------------------------------------"

        Write-Host "---------------------------------------------------------------------------------------------------"


    


    #Region List Operations

    $ListItemCollection = @()

    

   

    #$listsOnPrem= Get-PnPList -Web $Web |Where-Object {$_.Title -ne 'Form Templates' -and $_.Title -ne 'Access Requests' -and $_.Title -ne 'appdata' -and $_.Title -ne 'Composed Looks' -and $_.Title -ne 'Content type publishing error log' -and $_.Title -ne 'Converted Forms' -and  $_.Title -ne 'Master Page Gallery' -and$_.Title -ne 'NintexSnippets' -and $_.Title -ne 'NintexTemplates' -and  $_.Title -ne 'Theme Gallery' -and $_.Title -ne 'User Information List' -and $_.Title -ne 'Web Part Gallery'}

    $listsOnPrem= Get-PnPList -Web $Web |Where-Object {$_.Title -ne 'Access Requests' -and $_.Title -ne 'appdata' -and $_.Title -ne 'Composed Looks' -and $_.Title -ne 'Content type publishing error log' -and $_.Title -ne 'Converted Forms' -and  $_.Title -ne 'Master Page Gallery' -and $_.Title -ne 'NintexSnippets' -and $_.Title -ne 'NintexTemplates' -and  $_.Title -ne 'Theme Gallery' -and $_.Title -ne 'User Information List' -and $_.Title -ne 'Web Part Gallery' -and $_.Title -ne 'Maintenance Log Library' -and $_.Title -ne 'List Template Gallery' -and $_.Title -ne 'NintexWorkflowHistory' -and $_.Title -ne 'NintexWorkflows' -and $_.Title -ne 'Reporting Metadata' -and $_.Title -ne 'Reporting Templates' -and $_.Title -ne 'Site Template Gallery' -and $_.Title -ne 'MicroFeed' -and $_.Title -ne 'appfiles'}

    Write-Host "-----Lists Count: " $listsOnPrem.count -ForegroundColor Yellow

    

    

    Write-Host "------------List Operations------------------------------------------------------------------------"

    #Lists Operation

        forEach ( $list in $listsOnPrem) 

        {

        

            $listTitle = $list.Title

            $listType = $list.BaseTemplate

            $listUrl = $list.DefaultViewUrl           

            $lsCount = $list.ItemCount

            

            # ----------Workflow Start-------------

            #$listContext = $list.Context

            #$listContext.Load($list.WorkflowAssociations)

            #$listContext.ExecuteQuery()


            #If ($list.WorkflowAssociations -ne $null) {

            Get-PnPProperty -ClientObject $list -Property WorkflowAssociations

            foreach ($wf in $list.WorkflowAssociations) {

            $WorkflowName = $wf.Name

            "$SiteURL`t$($web.Url)`t$listTitle`t$WorkflowName`t$($wf.Enabled)`t$($wf.Modified)`t$($wf.InstantiationUrl)" >> $outCsv1


            #$workflowInstances = Get-PnPWorkflowInstance -List $listTitle

            Write-Host "--Workflow:  " $WorkflowName -ForegroundColor Red

            }

            #}

             # ----------Workflow End-------------

            $row = new-object PSObject

                 

            Add-member -inputObject $row -memberType NoteProperty -Name “List Title” -value $list.Title

            


          

             

            #Add-member -inputObject $row -memberType NoteProperty -Name “List Item Count” -value $list.ItemCount

             $NbOfFiles = 0    

            if($listType -eq 101)

            {

                #write-host "total - " $list.ItemCount

                $folderItems = Get-PnPListItem -Web $Web -List $list -Fields "FileLeafRef","FolderChildCount", "ItemChildCount"

               

                foreach ($folderitem in $folderItems) {

                    if (($folderitem.FileSystemObjectType) -eq "File") {

                        $NbOfFiles += 1

                    }

                    

                }

                Add-member -inputObject $row -memberType NoteProperty -Name “List Item Count” -value $NbOfFiles

            }

            else

            {

                Add-member -inputObject $row -memberType NoteProperty -Name “List Item Count” -value $list.ItemCount

            }  


                #Region More than 10k items        

            

                if($lsCount -ge 10000)

                {

                

                    Write-Host "10K Lists: " $listTitle -ForegroundColor Red

                }

                

                



                $currList = Get-PnPList -Web $Web $listTitle -Includes ContentTypes, Forms 

                $listForms = $currList.Forms 

                $flag = 0

                

                forEach ( $listFm in $listForms )

                {  $infoPath = ""

                  $listPath = $listFm.ServerRelativeUrl

                  if ( $listPath -like '*displayifs.aspx' -or $listPath -like '*newifs.aspx' -or $listPath -like '*FormServer.aspx') 

                  {

                      $flag = 1                          

                  }

                }

               #Region Infopath

                if($list.BaseTemplate -eq 115)

                {    $infoPath = "Found at URL " + $listUrl

                    

                    Write-Host 'InfoPath Found in Library ' $listTitle " at URL " $listUrl -ForegroundColor Red 

                   

                }

                        

                if($flag -eq 1)

                {   $infoPath = "Found at URL " + $listUrl

                    Write-Host 'InfoPath Found in List ' $listTitle " at URL " $listUrl -ForegroundColor Red 

                    

                }              

                #End Region Infopath

          

             $ListItemCollection += $row

            "$SiteURL`t$($web.Url)`t$listTitle`t$lsCount`t$infoPath" >> $outCsv

       }            

    $ListItemCollection | Format-Table 

    $ListItemCollection = @()

    Write-Host "----------------------------------------------List Operations End------------------------------------------------------------------------"

    #End Region

}


 #Region Source Pre Checks


    Connect-PnPOnline -Url $SiteURL -UseWebLogin


    $webOnPrem = Get-PnPWeb

    $OnPremSubsites= Get-PnPSubWebs -Recurse |Where-Object {$_.Title -ne 'Blogs'}

    $OnPremRootSubsites= Get-PnPSubWebs  |Where-Object {$_.Title -ne 'Blogs'}

    Write-Host "--Site URL: " $webOnPrem.Url -ForegroundColor Green

    Write-Host  "--Root subsites Count: "  $OnPremRootSubsites.Count -ForegroundColor Yellow

    Write-Host  "--All Subsites(Recursive) Count: "  $OnPremSubsites.Count -ForegroundColor Yellow


    $users = Get-PnPUser -WithRightsAssigned

    foreach($user in $users){

    $userTitle = $user.Title

    if($userTitle.Contains("(External)")){

    "$SiteURL`t$userTitle`t$($user.Email)" >> $outCsv2

    }

    }


    


    Get-PnPListInventory $webOnPrem

    if($OnPremSubsites.Count -gt 0)

    {

        Foreach ($web in $OnPremSubsites)

        {

            $OnPremSubSubsites= Get-PnPSubWebs -Web $web -Recurse |Where-Object {$_.Title -ne 'Blogs'}

                Write-Host "--SubSite URL: " $web.Url -ForegroundColor Green

                Write-Host  "--Subsites Count: "  $OnPremSubSubsites.Count -ForegroundColor Yellow

            Get-PnPListInventory $web

        }            

        Disconnect-PnPOnline

        Write-Host "----------------------------------------------Next Item------------------------------------------------------------------------"

     }


 Write-Host "----------------------------------------------Success!!!------------------------------------------------------------------------"

 Get-Date -Format “dddd dd/MM/yyyy HH:mm”

 }

 #EndRegion"


Get Site Details:

cls

Import-Module SharePointPnPPowerShellOnline -WarningAction SilentlyContinue



#Migration Tracker

$record = Import-Csv -Path "D:\UserData\Documents\scripts\siteDetailsInput.csv"

$outCsv = "D:\UserData\Documents\scripts\siteDetailsOutput.csv"

"SourceURL`tSite Title`tSite Description`tSubsites`tSize(MB)`tLCID" > $outCsv

foreach($data in $record){

$SiteURL= $data.URL


    Connect-PnPOnline -Url $SiteURL -UseWebLogin


    $site = Get-Culture

   

   

    

    $siteOnPrem = Get-PnPSite

    $siteUsage = Get-PnPProperty -ClientObject $siteOnPrem -Property Usage

    $SizeinMB = [System.Math]::Round((($siteUsage.Storage)/1MB),2)


    $webOnPrem = Get-PnPWeb

    $OnPremSubsites= Get-PnPSubWebs

    $webDescription = Get-PnPProperty -ClientObject $webOnPrem -Property Description

     

    #$webSize = Get-PnPProperty -ClientObject $webOnPrem -Property Size

    Write-Host "--Site URL:" $webOnPrem.Url

    Write-Host "--Site Title: " $webOnPrem.Title -ForegroundColor Yellow

    Write-Host  "--Site description: "  $webDescription -ForegroundColor Yellow

    Write-Host  "--Subsites(Recursive) Count: "  $OnPremSubsites.Count -ForegroundColor Yellow

    Write-Host "--Size in MB: "  $SizeinMB -ForegroundColor Yellow

    Write-Host  "--LCID: " $site.LCID

    "$SiteURL`t$($webOnPrem.Title)`t$webDescription`t$($OnPremSubsites.Count)`t$SizeinMB`t$($site.LCID)" >> $outCsv 


    }


 

 #EndRegion"