Last active
June 22, 2022 17:11
-
-
Save pankajsurti/147af4bbb12dfe4a29605a088d8f78bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
[ValidateNotNullOrEmpty()] | |
[Alias("FileName")] | |
[string]$FileName2Process | |
) | |
function Write-Log | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true)] | |
[ValidateNotNullOrEmpty()] | |
[Alias("LogContent")] | |
[string]$Message, | |
[Parameter(Mandatory=$false)] | |
[Alias('LogPath')] | |
[string]$Path='C:\Logs\PowerShellLog.log', | |
[Parameter(Mandatory=$false)] | |
[ValidateSet("Error","Warn","Info")] | |
[string]$Level="Info", | |
[Parameter(Mandatory=$false)] | |
[switch]$NoClobber | |
) | |
Begin | |
{ | |
# Set VerbosePreference to Continue so that verbose messages are displayed. | |
$VerbosePreference = 'Continue' | |
} | |
Process | |
{ | |
# If the file already exists and NoClobber was specified, do not write to the log. | |
if ((Test-Path $Path) -AND $NoClobber) { | |
Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name." | |
Return | |
} | |
# If attempting to write to a log file in a folder/path that doesn't exist create the file including the path. | |
elseif (!(Test-Path $Path)) { | |
Write-Verbose "Creating $Path." | |
$NewLogFile = New-Item $Path -Force -ItemType File | |
} | |
else { | |
# Nothing to see here yet. | |
} | |
# Format Date for our Log File | |
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
# Write message to error, warning, or verbose pipeline and specify $LevelText | |
switch ($Level) { | |
'Error' { | |
Write-Error $Message | |
$LevelText = 'ERROR:' | |
} | |
'Warn' { | |
Write-Warning $Message | |
$LevelText = 'WARNING:' | |
} | |
'Info' { | |
Write-Verbose $Message | |
$LevelText = 'INFO:' | |
} | |
} | |
# Write log entry to $Path | |
"$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append | |
## also dump to console | |
#$savedColor = $host.UI.RawUI.ForegroundColor | |
#$host.UI.RawUI.ForegroundColor = "DarkGreen" | |
Write-Output $message | |
#$host.UI.RawUI.ForegroundColor = $savedColor | |
} | |
End | |
{ | |
} | |
} | |
function Enable-DocumentLibraryVersionBySiteCollection { | |
param ( | |
$SiteUrl | |
) | |
begin | |
{ | |
Write-Log -Path $LogFileName "Begin Enable-DocumentLibraryVersionBySiteCollection" | |
} | |
process | |
{ | |
$connection = Connect-PnPOnline -Url $SiteUrl -UseWebLogin -ReturnConnection | |
Write-Log -Path $LogFileName $("Working on Type:{0} Url:{1} site." -f "Root Site", $connection.Url) | |
Enable-DocumentLibraryVersionBySite -connectionParam $connection | |
#Get the sub sites of the site collection | |
$SubWebs = Get-PnPSubWeb -Recurse -Connection $SiteConnection | |
Disconnect-PnPOnline -Connection $connection | |
if ($SubWebs -and $SubWebs.Count -gt 0) { | |
#Enumerate sub sites and add in AllSites list | |
ForEach($SubSite in $SubWebs) | |
{ | |
Write-Log -Path $LogFileName $("Working on Title:{0} Url:{1} site." -f $SubSite.Title, $SubSite.Url) | |
$connection = Connect-PnPOnline -Url $SubSite.Url -UseWebLogin -ReturnConnection | |
Enable-DocumentLibraryVersionBySite -connectionParam $connection | |
Disconnect-PnPOnline -Connection $connection | |
} | |
} | |
} | |
End | |
{ | |
Write-Log -Path $LogFileName "End of Enable-DocumentLibraryVersionBySiteCollection" | |
} | |
} | |
function Enable-DocumentLibraryVersionBySite { | |
param ( | |
$connectionParam | |
) | |
begin | |
{ | |
Write-Log -Path $LogFileName "Begin Enable-DocumentLibraryVersionBySite" | |
} | |
process | |
{ | |
$lists = Get-PnPList -Connection $connectionParam -Includes "EnableVersioning" | |
foreach ($list in $lists) { | |
if ( $list.BaseTemplate -eq 101 ) | |
{ | |
if ( ($list.EnableVersioning -eq $false) -or ($list.MajorVersionLimit -lt 500) ) | |
{ | |
Write-Log -Path $LogFileName $("Enabling major version for - List: Title={0} Template={1}." -f $list.Title, $list.BaseTemplate) | |
# first check if the version is enabled | |
Set-PnPList -Identity $list -EnableVersioning $true -MajorVersions 500 | |
} | |
else { | |
Write-Log -Path $LogFileName $("Already Major version is set with 500 max limit - List: Title={0} Template={1}." -f $list.Title, $list.BaseTemplate) | |
} | |
} | |
else { | |
Write-Log -Path $LogFileName $("Skipping as not a 101-basetemplate - List: Title={0} Template={1}." -f $list.Title, $list.BaseTemplate) | |
} | |
} | |
} | |
End | |
{ | |
Write-Log -Path $LogFileName "End of Enable-DocumentLibraryVersionBySite" | |
} | |
} | |
function Start-DocumentLibraryVersionByFile { | |
[CmdletBinding()] | |
Param | |
( | |
[string]$FileName2Process | |
) | |
begin | |
{ | |
$LogFileName = $("{0}\DocLibVersion-Log-{1}.txt" -f $env:LOG_FILE_PATH , (Get-Date -Format "yyyy-MM-dd-HH-mm-ss")) | |
Write-Log -Path $LogFileName "Begin Start-DocumentLibraryVersionByFile" | |
} | |
process | |
{ | |
try | |
{ | |
# check if the file to process is not present then exist the session. | |
if ( ! (Test-Path $FileName2Process) ) | |
{ | |
Write-Log -Path $LogFileName -Level Error $("Error: File {0} not found. Please provide the valid file." -f $FileName2Process) | |
Exit | |
} | |
Write-Log -Path $LogFileName $("Start processing the {0} file." -f $FileName2Process) | |
Import-Csv $FileName2Process | | |
ForEach-Object { | |
try | |
{ | |
Enable-DocumentLibraryVersionBySiteCollection -SiteUrl $_.Url | |
} | |
catch | |
{ | |
$ErrorMessage = $_.Exception | format-list -force | |
Write-Log -Path $LogFileName $("Exception {0}" -f $ErrorMessage); | |
} | |
finally | |
{ | |
} | |
} | |
Write-Log -Path $LogFileName $("Finished processing the {0} file." -f $FileName2Process) | |
} | |
catch | |
{ | |
$ErrorMessage = $_.Exception | format-list -force | |
Write-Log -Path $LogFileName $("Exception {0}" -f $ErrorMessage); | |
} | |
finally | |
{ | |
} | |
} | |
End | |
{ | |
Write-Log -Path $LogFileName "End of Start-DocumentLibraryVersionByFile" | |
} | |
} | |
$env:LOG_FILE_PATH = Split-Path -Parent $MyInvocation.MyCommand.Path | |
Start-DocumentLibraryVersionByFile -FileName2Process $FileName2Process | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment