Powershell Script to Upload Files in Loop
Sep 28, 2016
More and more people are starting to use Azure as a platform for their applications, these applications might want to get information from files. If this is a requirement nosotros will have to set upwardly storage in Azure then upload the needed documents into our Azure environment, luckily this tin be done easily past creating a PowerShell script. When you are creating a PowerShell script you have the capabilities of saving the script and rerunning it in the future, this makes the process of uploading the files easier equally you could only schedule the script to run.
Within this weblog I will create a script that will handle uploading files to an already existing storage container. When you need to upload files into Azure you will first take to make sure the Storage container is in existence, otherwise you volition accept to create it offset.
In my scenario I have already created a storage account and a container to exist used, please annotation that I have used the Resource managing director deployment model as this should exist used for all new applications on Azure.
Hither you can encounter I accept a storage account called "asblogstorage" and I take a container called "asdemocontainer", these are important to remember as we will demand to refer to them in script.
In one case yous accept the storage of interest we can go ahead and get-go with the script. Within the script I will start by specifying what login I want to utilise and I will reach this goal by using the "Add-AzureAccount" this volition give me a prompt to specify what credentials I desire to use.
If you haven't already y'all should also install the "AzureRM" module and then that you can fire of command against your Resource Manager model that nosotros specified when creating the storage account.
Additionally you might desire to import the "AzureRM.profile" also to make certain you can run the login command. (Not shown here) Once you lot have the modules in identify yous have to run the "Login-AzureRmAccount" control.
One time y'all are logged in y'all take to get a hold of the storage account key that you want to use, this exist the business relationship that houses the container where you want to upload the files.
We will need the following values to successfully retrieve the correct storage business relationship cardinal:
- The storage account name (Mine is called "asblogstorage")
- The resources group under which it was created (Mine is chosen "AusSouteast")
The command to get this fundamental value is the "Get-AzureRmStorageAccountKey", and it volition take the previously mentioned values to select the proper account access key. Key notation to call up hither is that whatsoever storage account in Azure will have two Access keys generated and for us to upload to the container of our liking nosotros volition simply need one of them (Note the ".Key1" at the stop).
We volition also populate a variable with the value of the central every bit we volition be needing information technology afterward in the script. I called my variable ($storageAccountkey)
Once we accept the cardinal stored in a variable nosotros need to create a storagecontext, this will be used so we can get the list of storage containers within the previously retrieved account. This context will provide usa with the proper noun and the access key to the storage account in which the container is found.
Afterward we take the context we tin can then loop through all the files of interest in our local folder and upload it into the container. To do this we will create an array containing all the files in a specified binder, and then we will loop through this array and upload each detail individually into the container. To upload a file you can utilize the "Fix-AzureStorageBlobContent" command, this control will expect the following input arguments:
- File, this will be the local full path location of the file
- Container, this will be the container name of where to upload the files to
- Blob, this volition be the relative path of where it will be uploaded to
- Context, this volition be the Storage business relationship name and key to be used in the uploading process
- (Optional) Force, indicates whether to overwrite existing blobs without confirmation
Now all you have to practise in the future is add more files to this location and then run the script to upload information technology to the same container. Your last result will wait similar this:
Source: https://nhaustralia.com.au/using-powershell-to-upload-files-to-your-azure-storage-container/