-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.ps1
97 lines (89 loc) · 4.05 KB
/
run.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Copy Files from Source Folder to Target
Function Copy-Files($SourceFolder, $TargetFolder)
{
write-host "Copying Files from:$($SourceFolder.URL) to $($TargetFolder.URL)"
#Get Each File from the Source
$SourceFilesCollection = $SourceFolder.Files
#Iterate through each item from the source
Foreach($SourceFile in $SourceFilesCollection)
{
$CountFileVersions = $SourceFile.Versions.Count
#Get the created by and created
$CreatedBy = $SourceFile.Author;
#Convert the "TimeCreated" property to local time
$CreatedOn = $SourceFile.TimeCreated.ToLocalTime();
#Loop Through Each File Version
for ($i = 0; $i -le $CountFileVersions; $i++){
#Initialize variables
$SourceProp;
$FileStream;
$ModifiedBy;
$ModifiedOn;
$VersionComment = "";
$MajorVer = $False;
#If Index is not the Last Published Version
if ($i -lt $CountFileVersions){
#Get all versions file, history, properties, createdBy, checkInComment
$fileSourceVer = $SourceFile.Versions[$i];
$SourceProp = $fileSourceVer.Properties;
$ModifiedBy = If ($i -eq 0) {$CreatedBy} ELSE {$fileSourceVer.CreatedBy};
$ModifiedOn = $fileSourceVer.Created.ToLocalTime();
$VersionComment = $fileSourceVer.CheckInComment;
$MajorVer = If ($fileSourceVer.VersionLabel.EndsWith("0")) {$True} Else {$False}
$FileStream = $fileSourceVer.OpenBinaryStream();
}
else {
#Get current versions file, history, properties, createdBy, checkInComment
$ModifiedBy = $SourceFile.ModifiedBy;
$ModifiedOn = $SourceFile.TimeLastModified;
$SourceProp = $SourceFile.Properties;
$VersionComment = $SourceFile.CheckInComment;
$MajorVer = If ($SourceFile.MinorVersion -eq 0) {$True} Else {$False}
$FileStream = $SourceFile.OpenBinaryStream();
}
#URL library destination
$DestFileURL = $TargetFolder.URL + "/" + $SourceFile.Name;
#Add initial File to destination library
$DestFile = $TargetFolder.Files.Add($DestFileURL, $FileStream, $SourceProp, $CreatedBy, $ModifiedBy, $CreatedOn, $ModifiedOn, $VersionComment, $True);
#If Major Version Publish it
if ($MajorVer){
$DestFile.Publish($strVerComment);
}
else{
#Update all previous file versions
$itmNewVersion = $DestFile.Item;
$itmNewVersion["Created"] = $dateCreatedOn;
$itmNewVersion["Modified"] = $dateModifiedOn;
$itmNewVersion.UpdateOverwriteVersion();
}
}
Write-host "File:"$SourceFile.Name ." is uploaded successfully with version count:" $countVersions
}
#Process SubFolders
Foreach($SubFolder in $SourceFolder.SubFolders)
{
if($SubFolder.Name -ne "Forms")
{
#Check if Sub-Folder exists in the Target Library!
$NewTargetFolder = $TargetFolder.ParentWeb.GetFolder($SubFolder.Name)
if ($NewTargetFolder.Exists -eq $false)
{
#Create a Folder
$NewTargetFolder = $TargetFolder.SubFolders.Add($SubFolder.Name)
}
#Call the function recursively
Copy-Files $SubFolder $NewTargetFolder
}
}
}
#Variables for Processing
$WebURL="http://Weburl/"
$SourceLibrary ="SourceLibrary"
$TargetLibrary = "DestinationLibrary"
#Get Objects
$Web = Get-SPWeb $WebURL
$SourceFolder = $Web.GetFolder($SourceLibrary)
$TargetFolder = $Web.GetFolder($TargetLibrary)
#Call the Function to Copy All Files
Copy-Files $SourceFolder $TargetFolder