Skip to content

Commit c78d24c

Browse files
committed
adding PS example
1 parent 5eedc7d commit c78d24c

File tree

1 file changed

+51
-43
lines changed

1 file changed

+51
-43
lines changed

articles/storage/common/storage-auth-aad-script.md

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: tamram
66

77
ms.service: storage
88
ms.topic: article
9-
ms.date: 03/12/2019
9+
ms.date: 03/06/2019
1010
ms.author: tamram
1111
ms.subservice: common
1212
---
@@ -17,52 +17,53 @@ Azure Storage provides extensions for Azure CLI and PowerShell that enable you t
1717

1818
When you log in to Azure CLI or PowerShell with an Azure AD identity, an access token is returned for accessing Azure Storage under that identity. That token is then automatically used by CLI or PowerShell to authorize operations against Azure Storage. For supported operations, you no longer need to pass an account key or SAS token with the command.
1919

20-
[!INCLUDE [storage-auth-aad-note-include](../../../includes/storage-auth-aad-note-include.md)]
21-
2220
## Supported operations
2321

2422
The extensions are supported for operations on containers and queues. Which operations you may call depends on the permissions granted to the Azure AD identity with which you log in to Azure CLI or PowerShell. Permissions to Azure Storage containers or queues are assigned via role-based access control (RBAC). For example, if a Data Reader role is assigned to the identity, then you can run scripting commands that read data from a container or queue. If a Data Contributor role is assigned to the identity, then you can run scripting commands that read, write, or delete a container or queue or the data they contain.
2523

2624
For details about the permissions required for each Azure Storage operation on a container or queue, see [Permissions for calling REST operations](https://docs.microsoft.com/rest/api/storageservices/authenticate-with-azure-active-directory#permissions-for-calling-rest-operations).
2725

28-
## Call CLI commands with an Azure AD identity
26+
## Environment variable
27+
28+
The environment variable associated with the `--auth-mode` parameter is `AZURE_STORAGE_AUTH_MODE`. You can specify the appropriate value in the environment variable to avoid including it on every call to an Azure Storage data operation.
2929

30-
To install the extension for Azure CLI, make sure that you have installed Azure CLI version 2.0.46 or later. Run `az --version` to check your installed version.
30+
## Call CLI commands using Azure AD credentials
3131

3232
Azure CLI supports the `--auth-mode` parameter for data operations against Azure Storage:
3333

3434
- Set the `--auth-mode` parameter to `login` to sign in using an Azure AD security principal.
3535
- Set the `--auth-mode` parameter to the legacy `key` value to attempt to query for an account key if no authentication parameters for the account are provided.
3636

37-
The following example shows how to create a container in a new storage account from Azure CLI using your Azure AD credentials.
37+
The following example shows how to create a container in a new storage account from Azure CLI using your Azure AD credentials. Remember to replace placeholder values in angle brackets with your own values:
38+
39+
1. Make sure that you have installed Azure CLI version 2.0.46 or later. Run `az --version` to check your installed version.
3840

39-
1. First, run `az login` and authenticate in the browser window:
41+
1. Run `az login` and authenticate in the browser window:
4042

4143
```azurecli
4244
az login
4345
```
4446
45-
1. Next, set your subscription, then create a resource group and a storage account within that resource group. Make sure to replace placeholder values in angle brackets with your own values:
47+
1. Next, set your subscription, then create a resource group and a storage account within that resource group:
4648
4749
```azurecli
4850
az account set --subscription <subscription-id>
4951
az group create \
50-
--name sample-resource-group \
52+
--name sample-resource-group-cli \
5153
--location eastus
54+
5255
az storage account create \
5356
--name <storage-account> \
54-
--resource-group sample-resource-group \
57+
--resource-group sample-resource-group-cli \
5558
--location eastus \
5659
--sku Standard_LRS \
5760
--encryption-services blob
5861
```
5962
60-
1. Before you create the container, assign RBAC permissions to the new storage account for yourself. Assign these two roles:
61-
62-
- Owner
63-
- Storage Blob Data Contributor (preview)
63+
1. Before you create the container, assign the [Storage Blob Data Contributor (preview)](../../role-based-access-control/built-in-roles.md#storage-blob-data-contributor-preview) role to yourself. Even though you are the account owner, you need explicit permissions to perform data operations against the storage account. For more information about assigning RBAC roles, see [Grant access to Azure containers and queues with RBAC in the Azure portal (preview)](storage-auth-aad-rbac.md).
6464
65-
For more information about assigning RBAC roles, see [Grant access to Azure containers and queues with RBAC in the Azure portal (preview)](storage-auth-aad-rbac.md).
65+
> [!IMPORTANT]
66+
> During the preview of Azure AD support for blobs and queues, RBAC role assignments may take up to 5 minutes to propagate.
6667
6768
1. Call the [az storage container create](https://docs.microsoft.com/cli/azure/storage/container?view=azure-cli-latest#az-storage-container-create) command with the `--auth-mode` parameter set to `login` to create the container using your Azure AD credentials:
6869
@@ -73,47 +74,54 @@ The following example shows how to create a container in a new storage account f
7374
--auth-mode login
7475
```
7576
76-
The environment variable associated with the `--auth-mode` parameter is `AZURE_STORAGE_AUTH_MODE`. You can specify the appropriate value in the environment variable to avoid including it on every call to an Azure Storage operation.
77-
78-
## Call PowerShell commands with an Azure AD identity
77+
## Call PowerShell commands using Azure AD credentials
7978
8079
[!INCLUDE [updated-for-az](../../../includes/updated-for-az.md)]
8180
82-
To use Azure PowerShell to sign in with an Azure AD identity:
81+
To use Azure PowerShell to sign in and run subsequent operations against Azure Storage using Azure AD credentials, create a storage context to reference the storage account, and including the `-UseConnectedAccount` parameter.
82+
83+
The following example shows how to create a container in a new storage account from Azure PowerShell using your Azure AD credentials. Remember to replace placeholder values in angle brackets with your own values:
8384
84-
1. Uninstall any previous installations of Azure PowerShell:
85+
1. Sign in to your Azure subscription with the `Connect-AzAccount` command and follow the on-screen directions to enter your Azure AD credentials:
8586
86-
- Remove any previous installations of Azure PowerShell from Windows using the **Apps & features** setting under **Settings**.
87-
- Remove all **Azure*** modules from `%Program Files%\WindowsPowerShell\Modules`.
87+
```powershell
88+
Connect-AzAccount
89+
```
90+
91+
1. Create an Azure resource group by calling [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup).
8892
89-
1. Make sure that you have the latest version of PowerShellGet installed. Open a Windows PowerShell window, and run the following command to install the latest version:
90-
9193
```powershell
92-
Install-Module PowerShellGet –Repository PSGallery –Force
94+
$resourceGroup = "sample-resource-group-ps"
95+
$location = "eastus"
96+
New-AzResourceGroup -Name $resourceGroup -Location $location
9397
```
94-
1. Close and reopen the PowerShell window after installing PowerShellGet.
9598
96-
1. Install the latest version of Azure PowerShell:
99+
1. Create a storage account by calling [New-AzStorageAccount](/powershell/module/az.storage/new-azstorageaccount).
97100
98101
```powershell
99-
Install-Module Az –Repository PSGallery –AllowClobber
102+
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
103+
-Name "<storage-account>" `
104+
-SkuName Standard_LRS `
105+
-Location $location `
100106
```
101107
102-
1. Install the latest Azure Storage module:
103-
104-
```powershell
105-
Install-Module Az.Storage -Repository PSGallery -AllowClobber -Force
106-
```
107-
1. Close and reopen the PowerShell window.
108-
1. Call the [New-AzStorageContext](https://docs.microsoft.com/powershell/module/az.storage/new-azstoragecontext) cmdlet to create a context, and include the `-UseConnectedAccount` parameter.
109-
1. To call a cmdlet with an Azure AD identity, pass the newly created context to the cmdlet.
110-
111-
The following example shows how to list the blobs in a container from Azure PowerShell using an Azure AD identity. Be sure to replace the placeholder account and container names with your own values:
112-
113-
```powershell
114-
$ctx = New-AzStorageContext -StorageAccountName storagesamples -UseConnectedAccount
115-
Get-AzStorageBlob -Container sample-container -Context $ctx
116-
```
108+
1. Get the storage account context that specifies the new storage account. When acting on a storage account, you can reference the context instead of repeatedly passing in the credentials. Include the `-UseConnectedAccount` parameter to call subsequent data operations using your Azure AD credentials.
109+
110+
```powershell
111+
$ctx = New-AzStorageContext -StorageAccountName "<storage-account>" -UseConnectedAccount
112+
```
113+
114+
1. Before you create the container, assign the [Storage Blob Data Contributor (preview)](../../role-based-access-control/built-in-roles.md#storage-blob-data-contributor-preview) role to yourself. Even though you are the account owner, you need explicit permissions to perform data operations against the storage account. For more information about assigning RBAC roles, see [Grant access to Azure containers and queues with RBAC in the Azure portal (preview)](storage-auth-aad-rbac.md).
115+
116+
> [!IMPORTANT]
117+
> During the preview of Azure AD support for blobs and queues, RBAC role assignments may take up to 5 minutes to propagate.
118+
119+
1. Create a container. Because this call uses the context created in the previous steps, the container is created using your Azure AD credentials.
120+
121+
```powershell
122+
$containerName = "sample-container"
123+
new-AzStorageContainer -Name $containerName -Context $ctx
124+
```
117125
118126
## Next steps
119127

0 commit comments

Comments
 (0)