Automate File Transfers Like an Expert with PowerShell & Google Cloud

Today, if you are not embracing the cloud then it would seem you are in a steadily declining minority. Cloud is the buzz term that refuses to die; and so instead we find ourselves in an age of migration toward the cloud or in a state of hybrid network, where the boundaries between cloud and hosted services are blurred.

But I know what you are thinking.

Cloud migration can be a tough world, full of APIs, resource IDs and tenants, where even the simplest of tasks, such as moving a number of files to a cloud storage location is made complicated.

Nobody wants to spend their day dragging and dropping content from one location to another...

...and you don't have to either!

With a little PowerShell scripting, you can be moving files from network to cloud without any hands, that's an automation promise.

 

PowerShell and Google Drive

 

Take a look at this script we have put together which takes a file from MOVEit Automation, an automated file transfer which we highly recommend, and moves it to a designated Google Cloud Drive.

# Change this to the file you want to upload
$sourceFile = $miaclient.MICacheFilename()

# Get the source file contents and details, encode in base64
$sourceItem = Get-Item $sourceFile
$sourceBase64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes($miaclient.MIGetOriginalFilename()))
$sourceMime = [System.Web.MimeMapping]::GetMimeMapping($miaclient.MIGetOriginalFilename())

# If uploading to a Team Drive, set this to 'true'
$supportsTeamDrives = 'false'

# Set the file metadata
$uploadMetadata = @{
originalFilename = $miaclient.MIGetOriginalFilename()
name = $miaclient.MIGetOriginalFilename()
description = $sourceItem.VersionInfo.FileDescription
#parents = @('teamDriveid or folderId') # Include to upload to a specific folder
#teamDriveId = ‘teamDriveId’ # Include to upload to a specific teamdrive
}

# Set the upload body
$uploadBody = @"
--boundary
Content-Type: application/json; charset=UTF-8
$($uploadMetadata | ConvertTo-Json)
--boundary
Content-Transfer-Encoding: base64
Content-Type: $sourceMime
$sourceBase64
--boundary--
"@

# Set the upload headers
$uploadHeaders = @{
"Authorization" = "Bearer $accessToken"
"Content-Type" = 'multipart/related; boundary=boundary'
"Content-Length" = $uploadBody.Length
}

# Perform the upload
$response = Invoke-RestMethod -Uri "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsTeamDrives=$supportsTeamDrives" -Method Post -Headers $uploadHeaders -Body $uploadBody

Not so difficult right?

 

Auditing and Error Control for Automated File Transfers

 

With file transfers to Google Cloud being automated, you might want to look at including some reporting and auditing capabilities to keep a track of your masterpiece.

MOVEit Automation is a great tool which allows you to automate the movement of files and script execution, such as the one above.

It contains capabilities to be able to audit the movement of every file, the time consumed moving it and the final outcome. In addition it can automate error handling, such as what to do when Google Cloud becomes unavailable or if the file is locked and cannot be moved.

Ipswitch MOVEit Automation Automated File Transfers

Pro2col are specialists in managed file transfer, automated transfers and MFT solutions like MOVEit, GoAnywhere, Globalscape and Axway solutions. Servicing the Northern European market with nearly a decade of product experience and awards to match.

If you would like to speak to one of our solution specialists about automated file movements and Managed File Transfer you can book a short call by clicking here.

 

About the Author

 
sam-23-headshot

 

Sam Fry is the Senior Technical Consultant at Pro2col. As a data and systems architect, Sam is always looking for ways to increase the accuracy and automation of data for efficiencies. Naturally, there’ll be both successes and failures when developing new data processes but Sam believes that each experience is valuable in facilitating continued technical and personal improvement. Sam is a big advocate for shared learning and makes herself available to the wider team to both share her skills and also learn from them. Find out more about Sam here.