Azure Storage and PowerShell 101

I’ve been working for a while on a deep-dive course about Azure Storage for Opsgility. This will include video training, and Opsgility will offer live training classes using the material I’ve written. One of the things that I’ve found really difficult is doing anything remotely complicated with blob storage and PowerShell. For example, how do you take snapshots of a blob in PowerShell? You go ahead and bing that and let me know what you find. (whistling) Yeah, I didn’t find anything either. So I did a bit of trial and error, and thought I would blog some of the results in a series of posts, of which this is the first.

In this article, we’ll see how to install the PowerShell cmdlets and set up a default Azure subscription and Azure storage account. You must already have an Azure account. If you don’t have one, you can sign up for a free trial at http://azure.microsoft.com.

Let’s begin by installing the PowerShell cmdlets.

  1. Open your browser and navigate to http://azure.microsoft.com.
  2. Click Downloads and then click the Install link under Windows PowerShell.
  3. Click Run when prompted and follow the instructions to install the cmdlets.

You can also install the cmdlets using the Web Platform Installer, or by downloading the msi from GitHub.

There are two ways to run PowerShell. There’s the command-line, and then there’s something called the ISE which is basically a window that allows you to write script and execute it one line at a time. You can also select a range of lines to run, or even run the entire script. I use this a lot to test my scripts.

I like to run PowerShell ISE from my task bar. If you want to try that, here’s how to set that up in Windows 8.1 or Windows 10.

  1. Run Microsoft Azure PowerShell. To do this in Windows 8, you can go to the Start screen and type “powershell” and select it. In Windows 10, you can go to the Start menu and look for it. This runs the command-line version.
  2. After it’s running, right-click on the icon in the task bar and select the option to pin this program to the taskbar.
  3. Close the PowerShell command line window.
  4. Right-click on the icon in the task bar, and you’ll see a bunch of options. Choose “Run ISE as Administrator”.

This should look like the following – you should have a white window on the top for the script and a blue window at the bottom. The window at the bottom will show the output from any parts of the script that you run. You can also type commands into the bottom window directly if you want to.

image

Before you can run anything, you need to set up the Azure account you’re going to use. To do this, you’re going to use the command “Add-AzureAccount”. You can type it into the script window and highlight it, then click icon for “Run Selection” (F8 also works). This is the icon with the red square around it I this image:

image

If you click the green play button, it runs the whole script.  We’re going to use this a lot in future articles.

For now, I’m going to put my command in the command window and run it – just type it in and press Enter. You will be prompted to enter your Azure subscription account information. After authentication, it will show the properties of the Azure account in the output window. My subscription name is “Azure Pass”. Write down your subscription name and set it as the current subscription and the default subscription by executing this command:

> Select-AzureSubscription –SubscriptionName “Azure Pass”

If you want to see a list of the subscriptions you have set up, you can ask to see the list and just show just the names of each subscription like this:

> Get-AzureSubscription | Select SubscriptionName

You can just see the subscription set as the current by using this command:

> Get-AzureSubscription –Current

If you don’t have a storage account, you’ll need to add one. You can go to the portal to do this, or you can do it right here in PowerShell. Here’s the command:

> New-AzureStorageAccount –StorageAccountName “nameofnewaccount” –Location “West US”

If the storage account already exists, you will get an error message. If the account was added successfully, the new storage account will be displayed in the output window.

Now set that storage account as the default account for this subscription to use. These defaults will be used the next time you run PowerShell. My new storage account is called “robinsnewstorage”.

> Set-AzureSubscription –SubscriptionName “Azure Pass”  -CurrentStorageAccountName “robinsnewstorage”

Now view the current Azure Subscription again and it will show the current storage account name.

image

In the next few articles, I’ll show you how to access the storage account and perform some actions against blob storage such as uploading and downloading files, deleting blobs, and copying blobs (including async copy). I’ll also show you how to access the properties of a blob. Then we’ll move onto the hard topics of snapshots, leases, and shared access signatures.

Tags: , ,

One Response to “Azure Storage and PowerShell 101”

  1. Uploading and downloading files to Azure Blob Storage with PowerShell | RobinDotNet's Blog Says:

    […] Azure Like It / ClickOnce « Azure Storage and PowerShell 101 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: