O365 Ecosystem

OneDrive for business is one of the most talked about parts of the O365 ecosystem and migration is one of the most requested subjects. We see clients every day that would like to move away from file shares to ODFB but not sure how to do so given that ODFB is a web based interface. There are lots of different migration tools that could be used for this, we happen to use ShareGate and they have a PowerShell interface that helps to get the job done. If you have read my blog posts in the past you will know I am a fan of PowerShell PnP but in this instance ShareGate seems to work better in that it does not need any logic to avoid duplicate files being uploaded. The program takes care of that within the construct of the PowerShell commands.

I write ODFB scripts therefor I am

I have recently written a PowerShell script that may be of interest if you are intending to move your users shares to ODFB. So lets get to the PoSH;

$servershare = “\\serverA\RootShare\”
$csvfile = “C:\temp\ODFBsites.csv”
$credPath = “C:\Temp\SecureCredentials.xml”
$Creds = “john.wayne@tallinthesaddle.com”
#====================CHANGE VARIABLES ABOVE======================#
$fileexists = Test-Path $credPath
if($fileexists -eq $false){
$MyCredentials = GET-CREDENTIAL –Credential $Creds | EXPORT-CLIXML $credPath
$cred = Import-Clixml $credPath
#======CREDS ABOVE==============#
$replacevar = $servershare -replace “\\”, “/”
Import-Module Sharegate
Import-Csv -Path $csvfile | foreach{
$site = $_.target
$usershare = $_.usershare
$source = $servershare + $usershare + “\”
$dstSite = Connect-Site -Url $site -Credential $cred
$dstList = Get-List -Site $dstSite -name Documents
$userfolders = Import-Document -DestinationList $dstList -SourceFolder $source
$srcfolders = Get-Folder -Path $source
$folders = Get-ChildItem -Path $srcfolders.Address.LocalPath -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName, Name
foreach($folder in $folders){
$newsrc = $folder.FullName -replace “\\”, “/”
$newtrg = $newsrc -replace “$replacevar$usershare/”, $dstList.Address
$src = $folder.FullName
$result = Import-Document -DestinationList $dstList -DestinationFolder $newtrg -SourceFolder $src

As you can see it is not a very long script but does what I want it to do. There is however, also a csv file that needs to be shown as well so let me post that then we can talk about what the script does and why;

Now lets talk about what the script does and we will reference back to the csv file. The first 4 lines down to the line that says “CHANGE VARIABLES ABOVE” are the specifics of what you want to connect to and where you want to save data. Specifically $servershare should be the root share that you create your users shares under which is the method that Microsoft has used with user shares on the local domain.

CSV file for migration

The $csvfile variable should point to the csv file that you will create. So lets talk about the csv file, it should have the same headers as shown above. In order to output the specifics of the ODFB urls that are needed for this you will go into ShareGate and run a report then export the report as an excel file filtering for only the ODFB sites. If you then save that as a csv file that should get the right data in and you can then add to it and change the headers to match what is above. Each user that you want to migrate should have a user share added in the usershare column otherwise the script will throw errors.

Back to the Script

The first non-variable line of the script tests for the securecredentials.xml file. This part of the script is optional in that if you LIKE typing your username and password in EVERY time you run the script then by all means do so. I, personally, prefer to capture the creds one time in an encrypted hashed xml file and just reuse it every time. All of the lines down to “CREDS ABOVE” are part of the writing and reading the credentials and after the first time of running the script you should not have to re-input your creds.

The $replacevar variable uses the replace parameter to change the back-slashes to forward-slashes in the servershare for use later. We do the Import-Module Sharegate so that we can then use the SG cmdlets.

We then import the csv file and do a for each on the rows which allows us to reference the data as we do with the next 2 lines setting the site and user share. The $source variable gets concatenated with a backslash to allow us to connect via UNC. The next 2 lines allow us to connect to the site ($dstSite) and to the Document library in the ODFB site ($dstList).

The next line,

$userfolders = Import-Document -DestinationList $dstList -SourceFolder $source

Is important in that it does the initial pass on the ODFB site to set up the source folders that we will add folders to later in the script. It also imports any documents in the root of the share to the ODFB site.

$srcfolders = Get-Folder -Path $source sets up the list of source folders for the next line that then iterates through the folders using  Get-ChildItem with the parameters directory (we only want folders) and recurse to give us all of the subfolders. We pipe that out and select FullName which is the full path of the folder, and the Name which is just the name of the folder.

We then do a foreach on the folders to get a list and address them one at a time. We take the FullName and replace the slashes in $newsrc then use that to replace the servershare with the destination address. The final line creates any subfolders and imports any files that exist in the current directory.

After all of that what happens?

Well it is not a very long script but I wanted you to see what I was doing in it. When you run the script it will loop through the user shares and create all of the folders and import the files at the same time giving you that pleasant feeling of accomplishment. This script can be improved by adding a mapping file to it and as I move forward with it I will update the post to include the user mappings but in the interim you can read about how to create a user mapping file here.

I hope that this helps your organization to move forward with migration of ODFB. And of course, if you need technical assistance with ODFB or SharePoint the technical resources at Planet are here to help, just contact Mark Quickel to request a meeting.

Live long and Prosper,




Hits: 1083