Forum Discussion
Upload of Network Share to Sharepoint Document Library
This script will give you the highlevel attributes such as Created time and Modified time. Please modify the script with the required attributes.
You need to create a csv file with columns Sitename (Site url) and UserFolder (Parent folder path).
Downlaod the file 'uploadfiles.psm1' from the attachment and place it in a folder. Replace the Import-Module path with the path in which the psm1 file was placed and run the script.
FYI: This script can also be used to copy files to multiple sites by mapping the site urls in the csv.
#$cred= Get-Credential
$csv = Import-Csv 'C:\csv\users3.csv'
$User = "Admin@tenantname.com"
$Password = "password"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location)
Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location)
Import-Module C:\uploadfiles.psm1
Foreach($Channel in $csv)
{
$SiteURL = $Channel.Sitename
$Folder = $Channel.UserFolder
$rootfolder=$folder.Replace("\","\\")
$libraryName = "Documents"
#Bind to site collection
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$(convertto-securestring $Password -asplaintext -force))
$ctx.Credentials = $Creds
$Web = $ctx.Web
$ctx.Load($Web)
$ctx.ExecuteQuery()
#Retrieve list
$ParentFolder = $ctx.Web.Lists.GetByTitle($libraryName).RootFolder
$ctx.Load($ParentFolder)
$ctx.ExecuteQuery()
Function UploadFiles($web,$path,$docLibrary)
{
$files = Get-ChildItem $path
foreach ($file in $files)
{
if($file.GetType().Name -eq "DirectoryInfo")
{
$foldername=$file
$parent=$file.Parent.FullName
$parent1=$parent+"\"
If($parent1 -eq $Folder)
{
$folder = $ParentFolder.Folders.Add($foldername)
$ParentFolder.Context.Load($folder)
$ParentFolder.Context.ExecuteQuery()
$CreationTime= $foldername.CreationTime
$LastAccessTime=$foldername.LastWriteTime
$item1 = $folder.ListItemAllFields
$item1["Modified"] =$LastAccessTime
$item1["Created"] =$CreationTime
$item1.Update()
$ctx.ExecuteQuery()
$CreationTime=$null
$LastAccessTime=$null
}
Else
{
$Urlpath1= ($parent -split "$rootfolder" )[1]
$foldernames1=$Urlpath1
if($foldernames1 -like "*\*")
{
$foldername1=$foldernames1.Replace("\","/")
}
Else
{
$foldername1=$foldernames1
}
$ServerRelativeUrlOfRootFolder = $ParentFolder.ServerRelativeUrl
$uploadFolderUrl= $ServerRelativeUrlOfRootFolder+"/"+$foldername1
$targetFolder = $ctx.Web.GetFolderByServerRelativeUrl($uploadFolderUrl)
$Upload = $targetFolder.Folders.Add($foldername);
$targetFolder.Context.Load($Upload)
$targetFolder.Context.ExecuteQuery()
$CreationTime= $foldername.CreationTime
$LastAccessTime=$foldername.LastWriteTime
$item = $Upload.ListItemAllFields
$item["Modified"] =$LastAccessTime
$item["Created"] =$CreationTime
$item.Update()
$ctx.ExecuteQuery()
$CreationTime=$null
$LastAccessTime=$null
}
$Folderpath1=$file.FullName
UploadFiles $web $Folderpath1 $docLibrary
}
Else
{
$filepath= [System.IO.Path]::GetDirectoryName($file.FullName)
$Urlpath= ($filepath -split "$rootfolder" )[1]
$foldernames=$Urlpath
if($foldernames -like "*\*")
{
$foldername=$foldernames.Replace("\","/")
}
Else
{
$foldername=$foldernames
}
$CreationTime= $file.CreationTime
$LastAccessTime=$file.LastWriteTime
$test=UploadFileInSlice -ctx $web -libraryName $docLibrary -foldername $foldername -fileName $File.FullName -CreationTime $CreationTime -LastAccessTime $LastAccessTime
$foldername=$null
$CreationTime=$null
$LastAccessTime=$null
}
}
}
UploadFiles -web $ctx -path $Folder -docLibrary $libraryName
}
- Dec 20, 2016
Does anyone have a PowerShell script that just inventories the fileshare? Captures filenames and paths and file sizes and identifies ones that that might cause issues with migration to SharePoint or OneDrive?
I agree with the other comment elsewhere in this thread, that a pure lift & shift misses many of the adavanages of OneDrive and SharePoint. However, having a way to quantify the volume of content to be considerd for migration and being able to see a list of all the files in a spreadsheet seems like it would be very helpful.