Create azure storage container through powershell in new portal - powershell

I want to create an azure storage container in an existing storage account, through powershell.I have tried the following commands:
Set-AzureSubscription -CurrentStorageAccountName "storageaccountv1" -SubscriptionId $SubscriptionId
New-AzureStorageContainer -Name $ContainerName -Permission Off
For those who know, Azure has two types of storage accounts: v1, v2. v1 accounts are the ones that are available through the http://manage.windowsazure.com/ and v2 that can be created in http://portal.azure.com/. While the command New-AzureStorageContainer is working with a storage account in v1, the command is not working for a storage account in v2. It is giving the following error:
New-AzureStorageContainer : ResourceNotFound: The storage account 'storageaccountv2' was not found.
At CreateStorageContainer.ps1:31 char:1
+ New-AzureStorageContainer -Name $ContainerName -Permission Off
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureStorageContainer], CloudException
+ FullyQualifiedErrorId : CloudException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.NewAzureStorageContainerCommand
Can anyone tell how to create an azure storage container in v2 through powershell?

If using StorageAccountContext is an option, you can try the following:
$ctx = New-AzureStorageContext -StorageAccountName "account-name" -StorageAccountKey "account-key"
New-AzureStorageContainer -Name "container-name" -Context $ctx

Azure PowerShell 1.0 Preview (https://azure.microsoft.com/en-us/blog/azps-1-0-pre/) is release.
With Azure PowerShell 1.0 Preview, you can run following cmdlets:
Login-AzureRMAccount -SubscriptionId [SubscriptionID]
Set-AzureRmCurrentStorageAccount -StorageAccountName [accountName] -ResourceGroupName [ResourceGroupName]
Then you can run "New-AzureStorageContainer" with resource mode account (create in new portal) without context.
BTW, "Set-AzureSubscription" is only for Service mode account, not applicable to resource mode account.

Here is the idempotent code
[System.String]$script:ResourceGroupNameVariable = 'resourcegroupnameone'
[System.String]$script:StorageAccountNameVariable = 'storacctnameone'
[System.String]$script:ContainerNameVariable = 'containernameone'
Write-Host "Start Container Create"
#Get/Set the AzureRmStorageAccountKey
# the below -Name parameter may be -AccountName according to documentation
[System.Object[]]$currentAzureRmStorageAccountKeys = Get-AzureRmStorageAccountKey -ResourceGroupName $script:ResourceGroupNameVariable -Name $script:StorageAccountNameVariable;
#Write-Host "about to currentAzureRmStorageAccountKeys.GetType"
#Write-Output $currentAzureRmStorageAccountKeys.GetType().FullName
### Create the AzureStorageContext (you always do this, regardless if the container itself exists or not)
[Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext]$currentAzureStorageContext = New-AzureStorageContext -StorageAccountName $script:StorageAccountNameVariable -StorageAccountKey $currentAzureRmStorageAccountKeys[0].Value;
#Write-Host "about to currentAzureStorageContext.GetType"
#Write-Output $currentAzureStorageContext.GetType().FullName
# Get/Set the AzureStorageContainer
[Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageContainer]$currentAzureStorageContainerCheck=Get-AzureStorageContainer -Context $currentAzureStorageContext -Name $script:ContainerNameVariable;
if(!$currentAzureStorageContainerCheck)
{
### The container does not already exist. Create a Blob Container in the Storage Account
New-AzureStorageContainer -Context $currentAzureStorageContext -Name $script:ContainerNameVariable;
}
else{
#Write-Host "about to currentAzureStorageContainerCheck.GetType"
#Write-Output $currentAzureStorageContainerCheck.GetType().FullName
#Write-Host "about to currentAzureStorageContainerCheck"
#$currentAzureStorageContainerCheck
}
Write-Host "End Container Create"
This runs against : Version 4.3.1 (of AzureRM )
get-module –listavailable -Name "AzureRM"

this helped but I needed the ARM command and not the classic method.
Hope it helps
$NewRGName = 'Lab'
$NewRGLocation = "West US"
$NewStrAccName ="Labstorage2"
$SkuName = 'Standard_LRS'
# Create new storage account
New-AzureRmStorageAccount -Location $NewRGLocation -Name $NewStrAccName -ResourceGroupName $NewRGName -SkuName $SkuName

Related

Cannot start Azure VM from PowerShell

I'm trying to start an Azure VM using the following PowerShell script:
$subscriptionId = "00000000-0000-0000-0000-000000000000"
$azureVM = "VS2017"
$resourceGroup = "VS2017ResourceGroup"
Add-AzureRmAccount # here start interactive login
Select-AzureSubscription -SubscriptionId $subscriptionId
Start-AzureRmVM - -ResourceGroupName $resourceGroup -Name $azureVM
When executing the script, I receive the following response:
Start-AzureRmVM : A positional parameter cannot be found that accepts argument '-'.
At C:\Users\ralbano\Desktop\Senza nome7.ps1:11 char:1
+ Start-AzureRmVM - -ResourceGroupName "VS2017ResourceGroup" -Name $azu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-AzureRmVM], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Azure.Commands.Compute.StartAzureVMCommand
I am using Windows PowerShell ISE to run the script.
Am I doing something wrong?
I checked the subscription id and the resource group name and are correct (of course in the sample above the subscription id is a fake). The VM is in "Stopped (deallocated)" state on Azure.
Start-AzureRmVM - -ResourceGroupName $resourceGroup -Name $azureVM
According to your error message, we should use this command like this:
Start-AzureRmVM -ResourceGroupName $resourceGroup -Name $azureVM

Retrieve Azure Service Bus Shared Access Connection String via Powershell

It's possible to retrieve the access key to an Azure storage account using Get-AzureRmStorageAccountKey from Powershell. How would I get hold of the access key to a shared access policy of an Azure Service Bus?
More clarification
This is what I get when I use the Get-AzureRmServiceBusNamespaceKey cmdlet:
PS C:\Windows\system32> Login-AzureRmAccount -Credential $cred
Environment : AzureCloud
Account : ***redacted***
TenantId : ***redacted***
SubscriptionId : ***redacted***
CurrentStorageAccount :
PS C:\Windows\system32> Set-AzureRmContext -SubscriptionId ***redacted***
Environment : AzureCloud
Account : ***redacted***
TenantId : ***redacted***
SubscriptionId : ***redacted***
CurrentStorageAccount :
PS C:\Windows\system32> Get-AzureRmServiceBusNamespaceKey -ResourceGroup testresourcegroup -Name test-bus -AuthorizationRuleName SendPolicy
Get-AzureRmServiceBusNamespaceKey : Run Login-AzureRmAccount to login.
At line:1 char:1
+ Get-AzureRmServiceBusNamespaceKey -ResourceGroup testresourcegroup -Name test-bus ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-AzureRmServiceBusNamespaceKey], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.ServiceBus.Commands.Namespace.GetAzure RmServiceBusNamespaceKey
PS C:\Windows\system32> Get-AzureRmStorageAccountKey -ResourceGroupName testresourcegroup -Name teststoragexxx
Key1 Key2
---- ----
***redacted*** ***redacted***
Not with Get-AzureRmStorageAccountKey, but you can use Get-AzureRmServiceBusNamespaceKey
$resourceGroup = "myResourceGroup"
$serviceBusName ="myservicebusname"
$policyName = "policyname"
Get-AzureRmServiceBusNamespaceKey -ResourceGroup $resourceGroup -Name
$serviceBusName -AuthorizationRuleName $policyName
This will return the whole object, so you can pass it into a variable and get the keys or connection strings from that.
Please have a try to login with tenantId and ServicePrincipal. I do a demo test about that, it works correctly for me.
Login-AzureRmAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal -SubscriptionId $subscriptionId
The following is may detail steps.
1 . We need to install service Bus module if it is not install. More detail info about AzureRM.ServiceBus please refer to document.
Install-Module -Name AzureRM.ServiceBus
2.More detail info about Automatically login script please refer to another SO thread.
3.Run the test script and check the result.
$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "strong password" -AsPlainText -Force
$subscriptionId="Your subcription"
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Login-AzureRmAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal -SubscriptionId $subscriptionId
$resourceGroup = "Resource Group name"
$serviceBusName ="Service Bus Name"
$policyName = "Policy Name"
Get-AzureRmServiceBusNamespaceKey -ResourceGroup $resourceGroup -Name $serviceBusName -AuthorizationRuleName $policyName

Azure Powershell to copy azure storage account to another azure storage account

I'm trying to copy all of the containers and files from one account to another using an automation powershell script. The script runs fine but I see no output and no files at the destination.
How can I correct this script so it will copy all of the containers and files on one azure storage account onto another storage account, creating the containers as needed?
#Define the source storage account and context.
$SourceStorageAccountName = "importantthings"
$SourceStorageAccountKey = "[mysourcekeyhere]"
$SourceContext = New-AzureStorageContext -StorageAccountName
$SourceStorageAccountName -StorageAccountKey $SourceStorageAccountKey
#Define the destination storage account and context.
$DestStorageAccountName = "dailybackup"
$DestStorageAccountKey = "[mydestkeyhere]"
$DestContext = New-AzureStorageContext -StorageAccountName $DestStorageAccountName -StorageAccountKey $DestStorageAccountKey
$Containers = Get-AzureStorageContainer -Prefix group -Context $SourceContext
foreach ($Container in $Containers) {
$DestContainer = New-AzureStorageContainer -Name $Container.Name -Context $DestContext -Force #-Permission Off
#Get a reference to blobs in the source container.
$blob = Get-AzureStorageBlob -Container $Container.Name -Context $SourceContext | Start-CopyAzureStorageBlob -destcontainer $DestContainer.Name #–destblob $blob.name #-srccontainer $Container.Name
$blob | Get-AzureStorageBlobCopyState –WaitForComplete
}
The first error is this:
New-AzureStorageContainer : The remote server returned an error: (403) Forbidden. HTTP Status Code: 403 - HTTP Error
Message: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly
including the signature.
At line:19 char:21
+ ... Container = New-AzureStorageContainer -Name $Container.Name -Context ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureStorageContainer], StorageException
+ FullyQualifiedErrorId :
StorageException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.NewAzureStorageContainerCommand
Solution is to ensure you have the right storage account key with the storage account.
Silly mistake.

Start-AzureSqlDatabaseExport: Object reference not set to an instance of an object

I am trying to start an export of a SQL Azure database to a blob. However, after trying different approaches and searching the web I can't find a way to make it work.
$exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $SqlContext -StorageContainer $Container -DatabaseName 'Users' -BlobName 'autobackupotest.bacpac' -Verbose -Debug
The line above results in:
DEBUG: 2:05:14 PM - StartAzureSqlDatabaseExport begin processing with ParameterSet 'ByContainerObject'.
WARNING: Client Session Id: '111746f6-65c2-4ba1-b7c6-52a9171ee6-2016-03-28 08:15:58Z'
WARNING: Client Request Id: 'f20b3326-a6c4-48d7-beb0-6ce7b17585-2016-03-28 11:05:14Z'
Start-AzureSqlDatabaseExport : Object reference not set to an instance of an object.
At C:\tests\thirdversion.ps1:29 char:22
+ $exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $SqlCont ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Start-AzureSqlDatabaseExport], NullReferenceException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.SqlDatabase.Database.Cmdlet.StartAzureSqlDatabaseExport
DEBUG: 2:05:19 PM - StartAzureSqlDatabaseExport end processing.
I verified the variables I use for this cmdlet and they are not null. Prior to that cmdlet I use the following code:
Import-Module Azure
Import-Module Azure.Storage
Get-AzureRmSubscription –SubscriptionName “Production” | Select-AzureRmSubscription
# Username for Azure SQL Database server
$ServerLogin = "username"
# Password for Azure SQL Database server
$serverPassword = ConvertTo-SecureString "abcd" -AsPlainText -Force
# Establish credentials for Azure SQL Database Server
$ServerCredential = new-object System.Management.Automation.PSCredential($ServerLogin, $serverPassword)
# Create connection context for Azure SQL Database server
$SqlContext = New-AzureSqlDatabaseServerContext -FullyQualifiedServerName “myspecialsqlserver.database.windows.net” -Credential $ServerCredential
$StorageContext = New-AzureStorageContext -StorageAccountName 'prodwad' -StorageAccountKey 'xxxxx'
$Container = Get-AzureStorageContainer -Name 'automateddbbackups' -Context $StorageContext
$exportRequest = Start-AzureSqlDatabaseExport -SqlConnectionContext $SqlContext -StorageContainer $Container -DatabaseName 'Users' -BlobName 'autobackupotest.bacpac' -Verbose -Debug
What could be wrong here? That exception message does not provide any detail.
I tested your code and it works fine on my side.
Run Login-AzureRmAccount to login before Get-AzureRmSubscription –SubscriptionName “Production” | Select-AzureRmSubscription, verify that the subscription Production exists in current tenant. And double check the username,password for SQL server, StorageAccount,StorageAccountKey,ContainerName are all correct.

Azure PowerShell - How to Create Queue If Not Exists

I am using Azure PowerShell to create and add entries to an Azure queue, following example here: MSDN: Using Azure PowerShell with Azure Storage - How to manage Azure queues .
Here is my PowerShell script:
$storeAuthContext = New-AzureStorageContext -StorageAccountName '[my storage account name]' -StorageAccountKey '[my storage account key'
$myQueue = New-AzureStorageQueue –Name 'myqueue' -Context $storeAuthContext
$queueMessage = New-Object -TypeName Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage -ArgumentList 'Hello'
$myQueue.CloudQueue.AddMessage($queueMessage)
This works fine the first time I run it.
Second time, I get this:
New-AzureStorageQueue : Queue 'myqueue' already exists. At line:1
char:12
+ $myQueue = New-AzureStorageQueue –Name 'myqueue' -Context $storeAuthContext
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (:) [New-AzureStorageQueue], ResourceAlreadyExistException
+ FullyQualifiedErrorId : ResourceAlreadyExistException,Microsoft.WindowsAzure.Commands.Storage.Queue.NewAzureStorageQueueCommand
In .NET Azure Storage API there is cloudqueue.createifnotexists (MSDN), but I cannot find the equivalent in Azure PowerShell.
What is the best way in PowerShell to create Azure storage queue if it does not already exist, otherwise get reference to the existing queue?
Afaik there is no CreateIfNotExist flag through the PowerShell module.
You can easily do the following to achieve the same:
$queue = Get-AzureStorageQueue -name 'myName' -Context $storeAuthContext
if(-not $queue){
# your code to create the queue
}
If you want to surpress errors and always try to create it (regardless if it exists or not); you should be able to use the -ErrorAction SilentlyContinue when creating the queue.
I would recommend the first approach though as it's a better practice.
As of 2017.03.14, the accepted answer did not work in a Powershell Azure Function, the Get-AzureStorageQueue throws an exception if the specified queue does not exist.
Example:
Here's the code
$storageAccountName = $env:AzureStorageAccountName
Write-Output ("storageAccountName: {0}" -f $storageAccountName)
$storageAccountKey = $env:AzureStorageAccountKey
Write-Output ("storageAccountKey: {0}" -f $storageAccountKey)
$storageQueueName = $env:AzureStorageQueueName
Write-Output ("storageAccountKey: {0}" -f $storageAccountKey)
Write-Output "Creating storage context"
$azureStorageContext = New-AzureStorageContext $storageAccountName - StorageAccountKey $storageAccountKey
Write-Output "Retrieving queue"
$azureStorageQueue = Get-AzureStorageQueue -Name $storageQueueName –Context $azureStorageContext
Here's the log
2017-03-15T04:16:57.021 Get-AzureStorageQueue : Can not find queue 'my-queue-name'.
at run.ps1: line 21
+ Get-AzureStorageQueue
+ _____________________
+ CategoryInfo : ObjectNotFound: (:) [Get-AzureStorageQueue], ResourceNotFoundException
+ FullyQualifiedErrorId : ResourceNotFoundException,Microsoft.WindowsAzure.Commands.Storage.Queue.GetAzureStorageQueueCommand
2017-03-15T04:16:57.021 Function completed (Failure, Id=58f35998-ebe0-4820-ac88-7d6ca42833df)
Resolution
I had to filter the results and only create the queue if it didn't exist. here's how to solve it:
Write-Output "Retrieving queue"
# Get-AzureStorageQueue returns an exception if the queue does not exists when passing -Name, so instead
# we need to get them all, filter by Name, and if null, create it
$azureStorageQueue = Get-AzureStorageQueue –Context $azureStorageContext | Where-Object {$_.Name -eq $storageQueueName}
if ($azureStorageQueue -eq $null)
{
Write-Output "Queue does not exist, creating it"
$azureStorageQueue = New-AzureStorageQueue -Name $storageQueueName -Context $azureStorageContext
}