Scripting AzCopy for automated upload

Today we will discuss how we can use powershell & script the azcopy to upload the files to azure.

This can be used for office 365 pst imports or other work that you do.

Here is the Code Snippet from one of my office 365 scripts to upload archives & also do error checking.

#################Azure Upload#################
$log1= $log[1]
Write-Host “Channging directory to azcopy” -ForegroundColor Green
cd “C:\Program Files (x86)\Microsoft SDKs\Azure\azcopy”
$azcopy = .\AzCopy.exe /Source:$source /Dest:$SASurl /V:$log1 2>&1
Write-Host “Azcopy finsihed – check logs” -foregroundcolor Green
ProgressBar -Title “Azcopy finsihed – check logs” -Timer 10
$azcount = select-string -Pattern “Total files transferred” $log1
$az=$azcount -split “: ”
if ($az[1] -ne $count)
{
Write-Host “$az[1] and $count are not equal” -ForegroundColor Red
Add-Content $log[1] “$az[1] and $count are not equal Check WI for further instructions”
ProgressBar -Title “$az[1] and $count are not equal Check WI for further instructions – Exit” -Timer 10
exit
}
############################################

First I am changing the directory to azcopy 

Write-Host “Channging directory to azcopy” -ForegroundColor Green
cd “C:\Program Files (x86)\Microsoft SDKs\Azure\azcopy”

Next step is to capture the output in the variable $azcopy by using  2>&1 redirection

$azcopy = .\AzCopy.exe /Source:$source /Dest:$SASurl /V:$log1 2>&1

Now extracting the Total files transferred using Select-String method so that we can verify that

total number of files that are transferred are equal to what you expect.

$azcount = select-string -Pattern “Total files transferred” $log1 (see the log file below you will get more understanding what is captured)

Split the extracted string to get the number/integer out of that

$az=$azcount -split “: “

Now you can compare this number with the number of files that should be uploaded,

if numbers are equal than all files have been uploaded successfully else you need to check the log files what has failed.

Here is what is logged inside the log:

[2017/03/17 06:18:34.316-04:00] >>>>>>>>>>>>>>>>
[2017/03/17 06:18:34.316-04:00][VERBOSE] 5.0.0 : AzCopy /Source:\\Server01\e$\Archivemigration\CloudMigration /Dest:f80a787887ybjnb hjjbhjBCCBH84c.blob.core.windows.net/ingestiondata?****** /V:E:\CloudMigration\ExchangeOnlineMigration\PSTlogs\AZcopy_Upload_3-17-2017_6-18AM_.log
[2017/03/17 06:18:35.938-04:00][VERBOSE] Start transfer: VikasS_Export_0001.pst => VikasS_Export_0001.pst
[2017/03/17 06:18:46.687-04:00][VERBOSE] Finished transfer: VikasS_Export_0001.pst => VikasS_Export_0001.pst
[2017/03/17 06:18:46.687-04:00] Transfer summary:
—————–
Total files transferred: 1
Transfer successfully: 1
Transfer skipped: 0
Transfer failed: 0
Elapsed time: 00.00:00:12

——————————————————————————————-

Enjoy scripting the cloud 🙂

Thanks for reading

Sukhija Vikas

http://SysCloudPro.com

 

2 thoughts on “Scripting AzCopy for automated upload

  1. Pingback: Scripting AzCopy for automated upload - 365 Community

Leave a comment