<#
******-----------------------------------------------------------------------******
Author -> Shiv Mangal Singh
Date -> 27 th July - 2017
Description -> This Script will generate the calculated columns details with HTML tags across List for all SharePoint online site collection at Tenant Level
Path of csv file --> $FileUrl ="D:\shiv\Powershell\SiteOwner_Tenant_9thSMSthApril.csv"
--> $currentLogPath ="D:\shiv\Powershell\testlog.csv"
Tenant Site URL --> $AdminURL = "https://Office365.connect.contoso"
******-----------------------------------------------------------------------******
#>
#Import-Module ‘C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell’ -DisableNameChecking
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll'
Add-Type -Path "D:\shiv\Powershell\DLL\Microsoft.SharePoint.Client.UserProfiles.dll"
#Required Parameters
$AdminURL = "https://office365.connect.contoso"
$sUsername = "abc@contoso.com"
$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
#Connect to SharePoint Online, Declared SPO Client Object Model Context
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
$spoCtx.Credentials = $spoCredentials
$spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($spoCtx)
$spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true)
$spoCtx.Load($spoTenantSiteCollections)
$spoCtx.ExecuteQuery()
# File Directory for Calculated Reports and logs
$FileUrl ="D:\shiv\Powershell\calculatedColumns_Report_Across_TenantLevel_SPEXE_26july17.csv"
$currentLogPath ="D:\shiv\Powershell\calculatedlog_Across_TenantLevel_SPEXE_26july17.csv"
# Headers for the data in CSV file
“Site URL `t Site Owner `t Owner Email `t List Url `t List `t Field Name `t Field Type `t HTML Tag” | Out-File $FileUrl
# function for Logs and prints messages
function LogMessage([String] $Msg)
{
Write-Host $Msg -ForegroundColor Cyan
Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") Message: $Msg" | Out-File -FilePath $currentLogPath -Append -Force
}
#function for Logs and prints error messages
function LogError([String] $ErrorMessage, [String]$ErrorDetails, [String]$ErrorPosition)
{
Write-Host $ErrorMessage -foregroundcolor red
$fullErrorMessage = $ErrorMessage + $ErrorDetails + ". " + $ErrorPosition
Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") ERROR: $fullErrorMessage" | Out-File -FilePath $currentLogPath -Append -Force
}
#Get all the Site Collection from Tenant
foreach($site in $spoTenantSiteCollections)
{
try
{
# Skip Mydrive site from Report
if($site.Url -ne "https://mydrive.connect.contoso/")
{
Write-Host $site.Url
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url)
$ctx.Credentials = $spoCredentials
$rootsite= $ctx.Web
$ctx.Load($rootsite)
$spWebs = $rootsite.Webs
$ctx.Load($spWebs)
$ctx.ExecuteQuery()
# Check Owners group
$GroupSiteOwneruser = @()
# Get all Groups from the site collection
$groupSiteOwner =$rootsite.SiteGroups
$ctx.Load($groupSiteOwner)
$ctx.ExecuteQuery()
$SiteOwners=@()
$OwnersEmail=@()
Write-Host "Connected !" -foregroundcolor black -backgroundcolor Green
# Get lists of Site collection
$lists =$rootsite.Lists
$ctx.Load($lists)
$ctx.ExecuteQuery()
# Iterate Groups
foreach($group in $groupSiteOwner)
{
if($group.Title -like "*owner*")
{
Write-Host $group.Title
$ctx.Load($group.Users)
$ctx.ExecuteQuery()
foreach($user in $group.Users)
{
$SiteOwners += $user.Title + ";"
$OwnersEmail+= (($user.Email) + ";").TrimStart(";")
}
}
}
# Iterate site collection lists
foreach($list in $lists)
{
$fields = $list.Fields
$ctx.Load($fields)
$ctx.ExecuteQuery()
$ctx.Load($list.RootFolder)
$ctx.ExecuteQuery()
# Foreach loop for site collection list's fields
foreach($field in $fields)
{
# Get calculated columns and HTML tag through $field.formula and field.FieldTypeKind -eq "Calculated"
$formula=$field.formula
if(($field.FieldTypeKind -eq "Calculated") -and ($formula -like "*<*>*</*>*") -or ($formula -like "*<*>*") )
{
# Sending data in to CSV Calculated Report.
"$($rootsite.Url) `t $($SiteOwners) `t $($OwnersEmail) `t $($list.RootFolder.ServerRelativeUrl) `t $($list.Title) `t $($field.Title) `t $($field.FieldTypeKind) `t $($formula)" | Out-File $FileUrl -Append
}
} # $fields closing loop
} # $list closing loop
#########********************* Closing Site collection $list field data||||||*********Started for all sub sites ***********************##################################
# Iterating all sub sites
foreach($subsite in $spWebs)
{
try
{
$ctx.Load($subsite)
$ctx.Load($subsite.Webs)
$ctx.ExecuteQuery()
Write-Host $subsite.Url
# get Sub SItes Owners users name
$SubsiteOwners =@()
$SubsiteOwnersEmail =@()
$groupsubsite =$subsite.SiteGroups
$ctx.Load($groupsubsite)
$ctx.ExecuteQuery()
# Check Owners group
foreach($grpSubsite in $groupsubsite)
{
if($grpSubsite.Title -like "*owner*")
{
# Write-Host $grpSubsite.Title
$ctx.Load($grpSubsite.Users)
$ctx.ExecuteQuery()
foreach($userSubsite in $grpSubsite.Users)
{
$SubsiteOwners += $userSubsite.Title + ";"
$SubsiteOwnersEmail+= ($userSubsite.Email + ";").TrimStart(";")
}
}
} # closing $grpSubsite
#
$listsubsites =$subsite.Lists
$ctx.Load($listsubsites)
$ctx.ExecuteQuery()
foreach($listSubSite in $listsubsites)
{
$fields = $listSubSite.Fields
$ctx.Load($fields)
$ctx.ExecuteQuery()
$ctx.Load($listSubSite.RootFolder)
$ctx.ExecuteQuery()
# Foreach loop for fields
foreach($field in $fields)
{
$formula=$field.formula
if(($field.FieldTypeKind -eq "Calculated") -and ($formula -like "*<*>*</*>*") -or ($formula -like "*<*>*") )
{
"$($subsite.Url) `t $($SubsiteOwners) `t $($SubsiteOwnersEmail) `t $($listSubSite.RootFolder.ServerRelativeUrl) `t $($listSubSite.Title) `t $($field.Title) `t $($field.FieldTypeKind) `t $($formula)" | Out-File $FileUrl -Append
}
}
}
foreach($subsubsite in $subsite.Webs)
{
try
{
GetInnerSubsite($subsubsite)
}
catch
{
if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
{
LogMessage("You need permission to access this site: "+ $subsubsite.Url)
}
else
{
LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
}
}
} # foreach $subsubsite loop
} # end try
catch
{
# Exception handling in log file
if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
{
LogMessage("You need permission to access this site: "+ $subsite.Url)
}
else
{
LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
}
} #end catch
} # $sub site closing
# Recursive loop for child sub sites
function GetInnerSubsite($subsubsite)
{
$ctx.Load($subsubsite)
$ctx.Load($subsubsite.Webs)
$ctx.ExecuteQuery()
Write-Host $subsubsite.Url
$listinnercollection =$subsubsite.Lists
$ctx.Load($listinnercollection)
$ctx.ExecuteQuery()
$groupInnerSubsite =$subsubsite.SiteGroups
$ctx.Load($groupInnerSubsite)
$ctx.ExecuteQuery()
$InnerSubsiteOwners =@()
$InnerSubsiteOwnersEmail =@()
# Check Owners group
foreach($grpInnerSubsite in $groupInnerSubsite)
{
if($grpInnerSubsite.Title -like "*owner*")
{
# Write-Host $grpInnerSubsite.Title
$ctx.Load($grpInnerSubsite.Users)
$ctx.ExecuteQuery()
foreach($userInnerSubsite in $grpInnerSubsite.Users)
{
$InnerSubsiteOwners += $userInnerSubsite.Title + ";"
$InnerSubsiteOwnersEmail+= ($userInnerSubsite.Email + ";").TrimStart(";")
}
}
}
foreach($listinner in $listinnercollection)
{
$innerfields = $listinner.Fields
$ctx.Load($innerfields)
$ctx.ExecuteQuery()
$ctx.Load($listinner.RootFolder)
$ctx.ExecuteQuery()
# Foreach loop for fields
foreach($innerfield in $innerfields)
{
$innerformula=$innerfield.formula
if(($innerfield.FieldTypeKind -eq "Calculated") -and ($innerformula -like "*<*>*</*>*") -or ($innerformula -like "*<*>*") )
{
"$($subsubsite.Url) `t $($InnerSubsiteOwners) `t $($InnerSubsiteOwnersEmail) `t $($listinner.RootFolder.ServerRelativeUrl) `t $($listinner.Title) `t $($innerfield.Title) `t $($innerfield.FieldTypeKind) `t $($innerformula)" | Out-File $FileUrl -Append
}
}
}
foreach($subsubinnersite in $subsubsite.Webs)
{
Write-Host $subsubinnersite.Url
GetInnerSubsite($subsubinnersite)
}
}
# End recrusive loop
}
} # End try of site collection
catch
{
# Exception handling in log file
if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
{
LogMessage("You need permission to access this site: "+ $site.Url)
}
else
{
LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
}
} #catch: $site closing loop
} # closing $site foreach loop
<#*** The END (Users Permission list across Tenant level) #>
******-----------------------------------------------------------------------******
Author -> Shiv Mangal Singh
Date -> 27 th July - 2017
Description -> This Script will generate the calculated columns details with HTML tags across List for all SharePoint online site collection at Tenant Level
Path of csv file --> $FileUrl ="D:\shiv\Powershell\SiteOwner_Tenant_9thSMSthApril.csv"
--> $currentLogPath ="D:\shiv\Powershell\testlog.csv"
Tenant Site URL --> $AdminURL = "https://Office365.connect.contoso"
******-----------------------------------------------------------------------******
#>
#Import-Module ‘C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell’ -DisableNameChecking
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll'
Add-Type -Path "D:\shiv\Powershell\DLL\Microsoft.SharePoint.Client.UserProfiles.dll"
#Required Parameters
$AdminURL = "https://office365.connect.contoso"
$sUsername = "abc@contoso.com"
$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
#Connect to SharePoint Online, Declared SPO Client Object Model Context
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
$spoCtx.Credentials = $spoCredentials
$spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($spoCtx)
$spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true)
$spoCtx.Load($spoTenantSiteCollections)
$spoCtx.ExecuteQuery()
# File Directory for Calculated Reports and logs
$FileUrl ="D:\shiv\Powershell\calculatedColumns_Report_Across_TenantLevel_SPEXE_26july17.csv"
$currentLogPath ="D:\shiv\Powershell\calculatedlog_Across_TenantLevel_SPEXE_26july17.csv"
# Headers for the data in CSV file
“Site URL `t Site Owner `t Owner Email `t List Url `t List `t Field Name `t Field Type `t HTML Tag” | Out-File $FileUrl
# function for Logs and prints messages
function LogMessage([String] $Msg)
{
Write-Host $Msg -ForegroundColor Cyan
Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") Message: $Msg" | Out-File -FilePath $currentLogPath -Append -Force
}
#function for Logs and prints error messages
function LogError([String] $ErrorMessage, [String]$ErrorDetails, [String]$ErrorPosition)
{
Write-Host $ErrorMessage -foregroundcolor red
$fullErrorMessage = $ErrorMessage + $ErrorDetails + ". " + $ErrorPosition
Write-Output "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") ERROR: $fullErrorMessage" | Out-File -FilePath $currentLogPath -Append -Force
}
#Get all the Site Collection from Tenant
foreach($site in $spoTenantSiteCollections)
{
try
{
# Skip Mydrive site from Report
if($site.Url -ne "https://mydrive.connect.contoso/")
{
Write-Host $site.Url
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site.Url)
$ctx.Credentials = $spoCredentials
$rootsite= $ctx.Web
$ctx.Load($rootsite)
$spWebs = $rootsite.Webs
$ctx.Load($spWebs)
$ctx.ExecuteQuery()
# Check Owners group
$GroupSiteOwneruser = @()
# Get all Groups from the site collection
$groupSiteOwner =$rootsite.SiteGroups
$ctx.Load($groupSiteOwner)
$ctx.ExecuteQuery()
$SiteOwners=@()
$OwnersEmail=@()
Write-Host "Connected !" -foregroundcolor black -backgroundcolor Green
# Get lists of Site collection
$lists =$rootsite.Lists
$ctx.Load($lists)
$ctx.ExecuteQuery()
# Iterate Groups
foreach($group in $groupSiteOwner)
{
if($group.Title -like "*owner*")
{
Write-Host $group.Title
$ctx.Load($group.Users)
$ctx.ExecuteQuery()
foreach($user in $group.Users)
{
$SiteOwners += $user.Title + ";"
$OwnersEmail+= (($user.Email) + ";").TrimStart(";")
}
}
}
# Iterate site collection lists
foreach($list in $lists)
{
$fields = $list.Fields
$ctx.Load($fields)
$ctx.ExecuteQuery()
$ctx.Load($list.RootFolder)
$ctx.ExecuteQuery()
# Foreach loop for site collection list's fields
foreach($field in $fields)
{
# Get calculated columns and HTML tag through $field.formula and field.FieldTypeKind -eq "Calculated"
$formula=$field.formula
if(($field.FieldTypeKind -eq "Calculated") -and ($formula -like "*<*>*</*>*") -or ($formula -like "*<*>*") )
{
# Sending data in to CSV Calculated Report.
"$($rootsite.Url) `t $($SiteOwners) `t $($OwnersEmail) `t $($list.RootFolder.ServerRelativeUrl) `t $($list.Title) `t $($field.Title) `t $($field.FieldTypeKind) `t $($formula)" | Out-File $FileUrl -Append
}
} # $fields closing loop
} # $list closing loop
#########********************* Closing Site collection $list field data||||||*********Started for all sub sites ***********************##################################
# Iterating all sub sites
foreach($subsite in $spWebs)
{
try
{
$ctx.Load($subsite)
$ctx.Load($subsite.Webs)
$ctx.ExecuteQuery()
Write-Host $subsite.Url
# get Sub SItes Owners users name
$SubsiteOwners =@()
$SubsiteOwnersEmail =@()
$groupsubsite =$subsite.SiteGroups
$ctx.Load($groupsubsite)
$ctx.ExecuteQuery()
# Check Owners group
foreach($grpSubsite in $groupsubsite)
{
if($grpSubsite.Title -like "*owner*")
{
# Write-Host $grpSubsite.Title
$ctx.Load($grpSubsite.Users)
$ctx.ExecuteQuery()
foreach($userSubsite in $grpSubsite.Users)
{
$SubsiteOwners += $userSubsite.Title + ";"
$SubsiteOwnersEmail+= ($userSubsite.Email + ";").TrimStart(";")
}
}
} # closing $grpSubsite
#
$listsubsites =$subsite.Lists
$ctx.Load($listsubsites)
$ctx.ExecuteQuery()
foreach($listSubSite in $listsubsites)
{
$fields = $listSubSite.Fields
$ctx.Load($fields)
$ctx.ExecuteQuery()
$ctx.Load($listSubSite.RootFolder)
$ctx.ExecuteQuery()
# Foreach loop for fields
foreach($field in $fields)
{
$formula=$field.formula
if(($field.FieldTypeKind -eq "Calculated") -and ($formula -like "*<*>*</*>*") -or ($formula -like "*<*>*") )
{
"$($subsite.Url) `t $($SubsiteOwners) `t $($SubsiteOwnersEmail) `t $($listSubSite.RootFolder.ServerRelativeUrl) `t $($listSubSite.Title) `t $($field.Title) `t $($field.FieldTypeKind) `t $($formula)" | Out-File $FileUrl -Append
}
}
}
foreach($subsubsite in $subsite.Webs)
{
try
{
GetInnerSubsite($subsubsite)
}
catch
{
if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
{
LogMessage("You need permission to access this site: "+ $subsubsite.Url)
}
else
{
LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
}
}
} # foreach $subsubsite loop
} # end try
catch
{
# Exception handling in log file
if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
{
LogMessage("You need permission to access this site: "+ $subsite.Url)
}
else
{
LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
}
} #end catch
} # $sub site closing
# Recursive loop for child sub sites
function GetInnerSubsite($subsubsite)
{
$ctx.Load($subsubsite)
$ctx.Load($subsubsite.Webs)
$ctx.ExecuteQuery()
Write-Host $subsubsite.Url
$listinnercollection =$subsubsite.Lists
$ctx.Load($listinnercollection)
$ctx.ExecuteQuery()
$groupInnerSubsite =$subsubsite.SiteGroups
$ctx.Load($groupInnerSubsite)
$ctx.ExecuteQuery()
$InnerSubsiteOwners =@()
$InnerSubsiteOwnersEmail =@()
# Check Owners group
foreach($grpInnerSubsite in $groupInnerSubsite)
{
if($grpInnerSubsite.Title -like "*owner*")
{
# Write-Host $grpInnerSubsite.Title
$ctx.Load($grpInnerSubsite.Users)
$ctx.ExecuteQuery()
foreach($userInnerSubsite in $grpInnerSubsite.Users)
{
$InnerSubsiteOwners += $userInnerSubsite.Title + ";"
$InnerSubsiteOwnersEmail+= ($userInnerSubsite.Email + ";").TrimStart(";")
}
}
}
foreach($listinner in $listinnercollection)
{
$innerfields = $listinner.Fields
$ctx.Load($innerfields)
$ctx.ExecuteQuery()
$ctx.Load($listinner.RootFolder)
$ctx.ExecuteQuery()
# Foreach loop for fields
foreach($innerfield in $innerfields)
{
$innerformula=$innerfield.formula
if(($innerfield.FieldTypeKind -eq "Calculated") -and ($innerformula -like "*<*>*</*>*") -or ($innerformula -like "*<*>*") )
{
"$($subsubsite.Url) `t $($InnerSubsiteOwners) `t $($InnerSubsiteOwnersEmail) `t $($listinner.RootFolder.ServerRelativeUrl) `t $($listinner.Title) `t $($innerfield.Title) `t $($innerfield.FieldTypeKind) `t $($innerformula)" | Out-File $FileUrl -Append
}
}
}
foreach($subsubinnersite in $subsubsite.Webs)
{
Write-Host $subsubinnersite.Url
GetInnerSubsite($subsubinnersite)
}
}
# End recrusive loop
}
} # End try of site collection
catch
{
# Exception handling in log file
if($_.Exception.Message -like '*(401) Unauthorized*' -or $_.Exception.Message -like '*Access denied*')
{
LogMessage("You need permission to access this site: "+ $site.Url)
}
else
{
LogError $_.Exception.Message $_.Exception.GetType().FullName $_.InvocationInfo.PositionMessage
}
} #catch: $site closing loop
} # closing $site foreach loop
<#*** The END (Users Permission list across Tenant level) #>
No comments:
Post a Comment