Azure PowerShell - How to Create Queue If Not Exists - powershell

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
}

Related

Azure PowerShell - Publish-AzureVMDscConfiguration : throws object not set error

I am writing an automation script for Infrastructure creation in Azure which contains the following code:
Azure-LoginAndPickSubscription -azureSubscriptionId $CONFIG_AZURE["SUBSCRIPTIONID"] `
-azureUsername $CONFIG_AZURE["USERNAME"] `
-azureEncryptedPassword $CONFIG_AZURE["PASSWORD"] `
-passwordKeyFilePath "$SCRIPT_DIRECTORY\private.key"
$solutionRoot = Split-Path -Path $SCRIPT_DIRECTORY -Parent
$storContext = (New-AzureStorageContext -StorageAccountName mystorename -StorageAccountKey "mystoragekey")
Publish-AzureVMDscConfiguration "$SCRIPT_DIRECTORY\NewDSC\InitialConfig.ps1" -ContainerName "windows-powershell-dsc" -Force -StorageContext $storContext
The last line (Publish-AzureVMDscConfiguration) throws an error:
Publish-AzureVMDscConfiguration : Object reference not set to an
instance of an object. At
C:\temp\Base.Deploy\ARM.Create.ps1:38 char:5
+ Publish-AzureVMDscConfiguration "$SCRIPT_DIRECTORY\NewDSC\Initia ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Publish-AzureVMDscConfiguration], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions.DSC.PublishAzureVMDscConfigurationCommand
I have checked the script (InitialConfig.ps1) exists, and the $storContext object is populated. Any ideas what may be causing this?
#Carl,
From your PowerShell code and error message, it seems that you used the ARM mode in your PowerShell. However, your code should use the ASM mode. I recommedn you can use Switch-AzureMode to switch your powershell mode and then run your code again. Please refer to this blog to check their difference (https://blogs.msdn.microsoft.com/powershell/2015/07/20/introducing-azure-resource-manager-cmdlets-for-azure-powershell-dsc-extension/ ). Like this
PS C:\> Switch-AzureMode -Name AzureResourceManager
PS C:\>Switch-AzureMode -Name AzureServiceManagement
Any results, please let me know.

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.

Create azure storage container through powershell in new portal

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

how to add data from a file to an open variable in powershell

I am trying to add an open varible to this command so when the data pulls from line 24 it adds it to the variable and makes it executable.
$data = Get-Content "C:\Users\bgriffiths\Documents\test.dat"
$data[24]
I have tried adding different formats to do this and nothing seems to work.
one command i tried was
invoke-command sql -query = $data
I get an error telling me
Invoke-Command : A parameter cannot be found that matches parameter name 'query'.
At line:4 char:26
+ invoke-command sql -query <<<< = $data
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
another command I have been trying to run is
$Command.CommandType.text = $data
the only error I get from this is
Property 'text' cannot be found on this object; make sure it exists and is settable.
At line:10 char:30
+ $Command.CommandType. <<<< text = $data
+ CategoryInfo : InvalidOperation: (text:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
I am at a lost on how to import file data into the script and have it run it.
I figured out how to run commands for sql through my powershell I had to actaully import the assembly list for the sql ps and then I was able to run the Invoke-SqlCmd
the script to add the information for sqlps to your windows ps is -
$ErrorActionPreference = "Stop"
$sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps"
if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue")
{
throw "SQL Server Powershell is not installed."
}
else
{
$item = Get-ItemProperty $sqlpsreg
$sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path)
}
#
# Preload the assemblies. Note that most assemblies will be loaded when the provider
# is used. if you work only within the provider this may not be needed. It will reduce
# the shell's footprint if you leave these out.
#
$assemblylist =
"Microsoft.SqlServer.Smo",
"Microsoft.SqlServer.Dmf ",
"Microsoft.SqlServer.SqlWmiManagement ",
"Microsoft.SqlServer.ConnectionInfo ",
"Microsoft.SqlServer.SmoExtended ",
"Microsoft.SqlServer.Management.RegisteredServers ",
"Microsoft.SqlServer.Management.Sdk.Sfc ",
"Microsoft.SqlServer.SqlEnum ",
"Microsoft.SqlServer.RegSvrEnum ",
"Microsoft.SqlServer.WmiEnum ",
"Microsoft.SqlServer.ServiceBrokerEnum ",
"Microsoft.SqlServer.ConnectionInfoExtended ",
"Microsoft.SqlServer.Management.Collector ",
"Microsoft.SqlServer.Management.CollectorEnum"
foreach ($asm in $assemblylist)
{
$asm = [Reflection.Assembly]::LoadWithPartialName($asm)
}
#
# Set variables that the provider expects (mandatory for the SQL provider)
#
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30
Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false
Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000
#
# Load the snapins, type data, format data
#
Push-Location
cd $sqlpsPath
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Update-TypeData -PrependPath SQLProvider.Types.ps1xml
update-FormatData -prependpath SQLProvider.Format.ps1xml
Pop-Location
Write-Host -ForegroundColor Yellow 'SQL Server Powershell extensions are loaded.'
Write-Host
Write-Host -ForegroundColor Yellow 'Type "cd SQLSERVER:\" to step into the provider.'
Write-Host
Write-Host -ForegroundColor Yellow 'For more information, type "help SQLServer".'
the link where I found this is http://blogs.msdn.com/b/mwories/archive/2008/06/14/sql2008_5f00_powershell.aspx