Wednesday, August 7, 2024
Site Analytics reports are empty after migrations.
Monday, July 22, 2024
Script to export the running workflow instances into an Excel file
# Load SharePoint snap-in if not already loaded
if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
# Define the site URL, list name, and item ID
$siteUrl = "http://cssvc.corning.com/nafta/otc"
$listName = "OrderRelease"
$itemId = 55695 # Specify the ID of the item whose workflows you want to cancel
# Get the site, web, and list objects
#$site = Get-SPweb $siteUrl
$web = Get-SPweb $siteUrl
$list = $web.Lists[$listName]
# Get the list item
$item = $list.GetItemById($itemId)
# Get the workflow manager
$workflowManager = $web.Site.WorkflowManager
# Get all running workflows for the list item
$workflows = $workflowManager.GetItemActiveWorkflows($item)
# Cancel running workflows
foreach ($workflow in $workflows) {
if ($workflow.InternalState -eq [Microsoft.SharePoint.Workflow.SPWorkflowState]::Running) {
Write-Host "Cancelling workflow instance ID: $($workflow.InstanceId) for item ID: $itemId"
#$workflowManager.RemoveWorkflow($workflow)
}
}
# Clean up
$web.Dispose()
#$site.Dispose()
Write-Host "All running workflows for item ID $itemId have been cancelled."
Wednesday, May 15, 2024
Script to get specific list data from multiple sites in SharePoint 2013/2016
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# Import site URLs and titles from CSV file
$siteurls = Import-Csv -Path "D:\Sathyam\IPP\IPP_Subsites.csv"
# Array to hold all results from all sites
$AllListItemCollection = @()
# Iterate through each site in the CSV
foreach($webs in $siteurls) {
$web = $null
try {
# Get the web object for the site
$web = Get-SPWeb -Identity $webs.SiteURL #Siteurl is the column name in the excel file.
$siteTitle = $webs.SiteTitle
# Get the target list
$list = $web.Lists["Listname"]
Write-Host $web.Url
# Loop through each item in the list
$list.Items | foreach {
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -name "SiteURL" -value $web.Url
$ExportItem | Add-Member -MemberType NoteProperty -name "Decision Type" -value $_["Milestone_x0020_Type1"] # Milestone_x0020_Type1 is column internal Name.
$ExportItem | Add-Member -MemberType NoteProperty -name "Key Deliverable?" -value $_["Key_x0020_Milestone_x003F_"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Title" -value $_["Title"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Item Status" -value $_["Item_x0020_Status"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Rejected" -value $_["Rejected"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Keyword(s)" -value $_["Keywords"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Created" -value $_["Created"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Created By" -value $_["Author"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Modified" -value $_["Modified"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Modified By" -value $_["Editor"]
# Add the object to the all results array
$AllListItemCollection += $ExportItem
}
} catch {
Write-Host "Error accessing site or list: $($_.Exception.Message)" -ForegroundColor Red
} finally {
# Dispose of the web object if it was created
if ($web) {
$web.Dispose()
}
}
}
# Export the aggregated result array to a single CSV file
$AllListItemCollection | Export-CSV "D:\Sathyam\IPP\output\AllSitesOutput.csv" -NoTypeInformation
Thursday, May 9, 2024
PowerShell script to get InfoPath Lists reports from a Sharepoint 2013
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration parameters
#$WebAppURL="https://intranet.crescent.com"
$ReportOutput="D:\Sathyam\InfoPath-ListForms.csv"
#Array to Hold Result - PSObjects
$ResultColl = @()
$webApps = Get-SPWebApplication
foreach ($webApp in $webApps) {
#Get All Webs of the Web Application
$WebsColl = Get-SPWebApplication $WebApp | Get-SPSite -Limit All | Get-SPWeb -Limit All
#Iterate through each web
Foreach($Web in $WebsColl)
{
#Get All Lists with InfoPath List Forms in use
Foreach ($List in $web.Lists | Where { $_.ContentTypes[0].ResourceFolder.Properties["_ipfs_infopathenabled"]})
{
Write-Host "Found an InfoPath Form at: $($Web.URL), $($List.Title)"
$Result = new-object PSObject
$Result | add-member -membertype NoteProperty -name "Site URL" -Value $web.Url
$Result | add-member -membertype NoteProperty -name "List Name" -Value $List.Title
$Result | add-member -membertype NoteProperty -name "List URL" -Value "$($Web.Url)/$($List.RootFolder.Url)"
$Result | add-member -membertype NoteProperty -name "Template" -Value $list.ContentTypes[0].ResourceFolder.Properties["_ipfs_solutionName"]
$ResultColl += $Result
}
}
}
#Export Results to a CSV File
$ResultColl | Export-csv $ReportOutput -notypeinformation
Write-Host "InfoPath Lists Forms Report has been Generated!" -f Green
Sharepoint workflows report
Add-PSSnapin microsoft.sharepoint.powershell
Function global:Get-SPWebApplication($WebAppURL)
{
return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}
#Function to Get the workflow inventory for the entire web application
function Get-WorkflowInventory([string] $WebAppURL)
{
#Get the Web Application URL
$WebApp = Get-SPWebApplication $WebAppURL
#Iterate through each site collection
foreach ($Site in $WebApp.Sites)
{
#Loop through each site
foreach ($Web in $Site.AllWebs)
{
#Loop through each list
foreach ($List in $Web.Lists)
{
# Leave hidden Lists and Libraries
if($List.Hidden -eq $false)
{
foreach ($WorkflowAssociation in $List.WorkflowAssociations)
{
#Leave the "Previous Versions"
if($WorkflowAssociation.Name.Contains("Previous Version") -eq $false)
{
$data = @{
"Site" = $Site.Rootweb.Title
"Web" = $Web.Title
"Web URL" = $Web.Url
"List Name" = $List.Title
"List URL" = $Web.Url+"/"+$List.RootFolder.Url
"Workflow Name" = $WorkflowAssociation.Name
"Running Instances" = $WorkflowAssociation.RunningInstances
}
#Create an object
New-Object PSObject -Property $data
}
}
}
}
$Web.Dispose()
}
$Site.Dispose()
}
}
#call the function
Get-WorkflowInventory "https://SPwebapplication.contoso.com/" | Export-Csv -NoTypeInformation -Path D:\Sathyam\SPWebappname_WorkflowInventory.csv
write-host "Workflows Inventory report has been generated successfully!"
Tuesday, March 19, 2024
Site usage report script
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
function ExportRollupAnalyticsData {
Param(
[string]$RootSiteUrl,
[string]$Scope,
[switch]$IncludeSites,
[switch]$IncludeWebs,
[string]$OutputFilePath
)
# Delete CSV file if existing
If (Test-Path $OutputFilePath) {
Remove-Item $OutputFilePath
}
# Write header row to CSV File
$OutputHeader = "Scope,Name,URL,Most Recent Day with Usage,Hits - All Time,Unique Users - All Time,Hits - Most Recent Day with Usage,Unique Users - Most Recent Day with Usage,Current Date"
$OutputHeader | Out-File $OutputFilePath -Append
# Get Web Application for Root Site
$RootSite = Get-SPSite $RootSiteUrl
$WebApp = $rootSite.WebApplication
# Get Search Service Application
$SearchApp = Get-SPEnterpriseSearchServiceApplication
# Loop through all Site Collections in Web Application
ForEach($Site in $WebApp.Sites) {
# Export Site analtyics if -IncludeSites flag is present
If ($IncludeSites.IsPresent) {
$Scope = "Site"
$SiteTitle = $Site.RootWeb.Title.Replace(",", "")
$SiteUrl = $Site.Url
$UsageData = $SearchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$Site.ID,[System.Guid]::Empty)
$LastProcessingTime = $UsageData.LastProcessingTime
$CurrentDate = $UsageData.CurrentDate
$TotalHits = $UsageData.TotalHits
$TotalUniqueUsers = $UsageData.TotalUniqueUsers
$LastProcessingHits = $UsageData.LastProcessingHits
$LastProcessingUniqueUsers = $UsageData.LastProcessingUniqueUsers
# Write Web details to CSV File
$OutputString = $Scope + "," + $SiteTitle + "," + $SiteUrl + "," + $LastProcessingTime + "," + $TotalHits + "," + $TotalUniqueUsers + "," + $LastProcessingHits + "," + $LastProcessingUniqueUsers + "," + $CurrentDate
$OutputString | Out-File $OutputFilePath -Append
}
# Export Web analtyics if -IncludeWebs flag is present
If ($IncludeWebs.IsPresent) {
# Loop through all Webs in Site Collection
ForEach($Web in $Site.AllWebs) {
$Scope = "Web"
$WebTitle = $Web.Title.Replace(",", "")
$WebUrl = $Web.Url
$UsageData = $SearchApp.GetRollupAnalyticsItemData(1,[System.Guid]::Empty,$Site.ID,$Web.ID)
$LastProcessingTime = $UsageData.LastProcessingTime
$CurrentDate = $UsageData.CurrentDate
$TotalHits = $UsageData.TotalHits
$TotalUniqueUsers = $UsageData.TotalUniqueUsers
$LastProcessingHits = $UsageData.LastProcessingHits
$LastProcessingUniqueUsers = $UsageData.LastProcessingUniqueUsers
# Write Web details to CSV File
$OutputString = $Scope + "," + $WebTitle + "," + $WebUrl + "," + $LastProcessingTime + "," + $TotalHits + "," + $TotalUniqueUsers + "," + $LastProcessingHits + "," + $LastProcessingUniqueUsers + "," + $CurrentDate
$OutputString | Out-File $OutputFilePath -Append
}
}
# Dispose Site Collection Object
$Site.Dispose()
}
}
# Sample Usage: Export both SPSite and SPWeb analytics
ExportRollupAnalyticsData -RootSiteUrl "https://Webapp1.sharepoint.com" -OutputFilePath "D:\Sathyam\UsageReports\Webapp1-analytics-export-sites-webs.csv" -IncludeSites -IncludeWebs
# Sample Usage: Export only SPSite analytics
ExportRollupAnalyticsData -RootSiteUrl "https://Webapp1.sharepoint.com" -OutputFilePath "D:\Sathyam\UsageReports\Webapp1-analytics-export-sites-only.csv" -IncludeSites
# Sample Usage: Export only SPWeb analytics
ExportRollupAnalyticsData -RootSiteUrl "https://Webapp1.sharepoint.com" -OutputFilePath "D:\Sathyam\UsageReports\Webapp1-analytics-export-webs-only.csv" -IncludeWebs
#Get-SPWebApplication
Monday, March 27, 2023
Script to get all subsites size from Sharepoint farm
Add-PSSnapin "*Sharepoint*"
#Get Size of all Sub-sites in a sharepoint Farm
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# Function to calculate folder size
Function CalculateFolderSize($Folder)
{
[long]$FolderSize = 0
foreach ($File in $Folder.Files)
{
#Get File Size
$FolderSize += $file.TotalLength;
#Get the Versions Size
foreach ($FileVersion in $File.Versions)
{
$FolderSize += $FileVersion.Size
}
}
#Iterate through all subfolders
foreach ($SubFolder in $Folder.SubFolders)
{
#Call the function recursively
$FolderSize += CalculateFolderSize $SubFolder
}
return $FolderSize
}
$sizelog = "D:\Stage_subsites_Size.csv"
Add-Content -Path $sizelog -Value "WebappURL,Sitecollection_URL,WebUrl,Size"
$webapps = Get-SPWebApplication
foreach($WebApp in $webapps)
{
Write-Host "Running Web application" $WebApp.Url
$Sites = Get-SPSite -WebApplication $WebApp -Limit All
Write-Host "Site collections" $Sites
foreach ($site in $Sites)
{
Write-Host "Running site" $site
#$Site1 = new-object Microsoft.SharePoint.SPSite($site)
foreach($Web in $site.AllWebs)
{
Write-Host "Running web" $web
#Call function to calculate Folder Size
[long]$WebSize = CalculateFolderSize($Web.RootFolder)
#Get Recycle Bin Size
foreach($RecycleBinItem in $Web.RecycleBin)
{
$WebSize += $RecycleBinItem.Size
}
$Size = [Math]::Round($WebSize/1MB, 2)
Write-Host $web.Url ":`t" $Size "MB"
$CSVoutput = $webapp.url + "*" + $site.url +"*"+ $web.Url + "*" + $Size
$csvoutput | Out-File $sizelog -Append
}}}