New-AzureStorageAccount PowerShell command never returns - powershell

I am trying to create a new Azure storage account using the following command:
PS> New-AzureStorageAccount -StorageAccountName 'newstoragerobower' -Location 'West US' -Description 'Test account for automation VMs' -Label 'AutomationStorageAccount'
However, it never returns. It successfully creates the storage account (verified through Azure Management Console). I can get it to return using a workaround where I start it as a job, then call Wait-Job with a timeout, thusly:
PS> $job = Start-Job -Name myName -Command { $storageAcct = New-AzureStorageAccount -StorageAccountName 'newstoragerobower' -Location 'West US' -Description 'Test account for automation VMs' -Label 'AutomationStorageAccount' }
PS> Wait-Job $job -Timeout 20
At best, this will add significant unnecessary delay to my scripts, and at worst it could cause the job to abort mid-execution (sometimes Azure jobs take longer then usual). Does anyone know why the command is not returning normally?

Can you try running with the -Debug option, or setting $DebugPreference to Continue? This isn't generally the case for creating storage accounts, would like to understand what's different about yur subscription that is producing this issue.

Related

az cli login session scope to process

In PowerShell you can do the following:
$result = Connect-AzAccount `
-SubscriptionId $SubscriptionId `
-TenantId $TenantId `
-Credential $credential `
-ContextName $contextName `
-Scope Process `
-ServicePrincipal
As per the doc, if you specify the -Scope Process the Az Context will be bound to that specific PS Process.
Determines the scope of context changes, for example, whether changes apply only to the current process, or to all sessions started by this user.
Is there any way of replicating this behaviour with az cli?
My use case
I will connect to Azure from a Jenkins job. If I start two jobs, maybe one of them will disconnect via az logout -u <user> and affect the other job.
I would like to isolate the az cli session.
I haven't personally used it yet, but according to this GitHub issue which links to this documentation, it should be possible using a process scoped environment variable.
Unfortunately, there is not such an equivalent feature as -Scope Process in Azure CLI, all the available parameters here.
https://learn.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest#az_login

Switch-AzureRmWebAppSlot does not swap the physical site only the application settings

I have been running the following command via powershell for AZURE but all that gets swapped are the application settings:
Switch-AzureRmWebAppSlot -ResourceGroupName 'myresourcegroup' -Name 'mywebsitename' -SourceSlotName "staging" -DestinationSlotName "production" -confirm -verbose
The same thing happens when I run this command:
Switch-AzureRmWebAppSlot -ResourceGroupName 'myresourcegroup' -Name 'mywebsitename' -SourceSlotName "staging" -DestinationSlotName "production" -SwapWithPreviewAction CompleteSlotSwap -confirm -verbose
I cannot use Switch-AzureWebsite as I cannot set a default subscription with my permissions.
Using the Login-AzureRMAccount the only way I found to switch slots is as follows:
$ParametersObject = #{targetSlot = "production"}
$RGN = 'resource-group-name-'
Invoke-AzureRmResourceAction -ResourceGroupName $RGN -ResourceType Microsoft.Web/sites/slots -ResourceName website-name/staging -Action slotsswap -Parameters $ParametersObject -Verbose -force
This was hard to find, in part because many examples required you to set your Azure default subscription prior to executing Switch-AzureWebsite and the other switch only moved the configuration elements over. I am curious if anyone has a clever way to discover power shell commands or ARM template commands aside from the ones generated for deployments on Azure. Ideally, I could perform an action on Azure an then see same thing scripted as PowerShell.
REF: Microsoft Docs Here

Set-AzureRmVMCustomScriptExtension SecureExecution flag not working

We are running a custom powershell script against an Azure VM using the Set-AzureRMVMCustomScriptExtension command.
The ps script creates local user accounts provided with "username" and "password" parameters.
My understanding is that Set-AzureRMVMCustomScriptExtension supports -SecureExecution switch, which according to the documentation (https://learn.microsoft.com/en-us/powershell/module/azurerm.compute/set-azurermvmcustomscriptextension?view=azurermps-4.4.1&viewFallbackFrom=azurermps-4.4.0)
"Indicates that this cmdlet makes sure that the value of the Run parameter is not logged on the server or returned to the user by using the GET extension API. The value of Run might contain secrets or passwords to be passed to the script file securely."
However, providing the switch and executing the ps script results in the following being logged to the "Windows Powershell" event log on the target VM:
Engine state is changed from None to Available.
Details:
NewEngineState=Available
PreviousEngineState=None
SequenceNumber=13
HostName=ConsoleHost
HostVersion=5.1.14393.1770
HostId=
HostApplication=powershell -ExecutionPolicy Unrestricted -file createuser.ps1 -username myNewUser -password somePassword
EngineVersion=5.1.14393.1770
RunspaceId=
PipelineId=
CommandName=
CommandType=
ScriptName=
CommandPath=
CommandLine=
Which is not what I am expecting to see. Is this a bug? Or is the documentation incorrect?
This is the command I'm executing:
Set-AzureRmVMCustomScriptExtension -ResourceGroupName $vmResourceGroupName
-VMName $vmName
-StorageAccountName $scriptStorageAccountName
-ContainerName $scriptStorageAccountContainerName
-FileName $scriptName
-Run $scriptName
-Argument $scriptArguments
-Name $extensionAlias
-Location $location
-ForceRerun $(New-Guid).Guid
-SecureExecution
Well, its not like Azure can magically change how windows server operates. this is windows server logging, not extension logging. it won't log this to the extension log and will hide sensitive information from the api, but, obviously, powershell engine can log this input like any other input

Powershell Workflow not able to use -parameters to create a VM

I am having a problem with my Powershell Workflow that I am authoring in Windows Azure Pack Admin Site. In Powershell ISE, the following works but in Service Management Portal Admin Site Runbook Authoring, it does not work. Where it is getting stuck is that it is saying that it cannot validate the arguments passed -Name. I remove -Name out and now -FullName doesnt work. It seems like all the switch parameters for the command is not working. Can anyone help me out?
param (
[string]$DomainAdminAcct,
[string]$DomainAdminPass,
[string]$ServerName
)
InlineScript {
Add-PSSnapin VMWare.VimAutomation.Core
$vCenter = "test300"
Connect-ViServer -server $vCenter -ErrorAction Stop
$myCluster = Get-Cluster -Name "DC Test"
$myTemplate = Get-Template -Name "2012dc" -Location "our company"
$OSCustomizationSpec = New-OSCustomizationSpec –Name “$ServerName” –FullName “$ServerName” –OrgName “our company” –Domain “our.domain.com” –DomainUsername “$DomainAdminAcct” –DomainPassword "$DomainAdminPass" -AdminPassword "changeme" -ChangeSid
New-VM -Name $ServerName -ResourcePool $myCluster -Template $myTemplate -OSCustomizationSpec $OSCustomizationSpec
}
}
Sounds like you have Hyper-V and PowerCLI modules loaded and the commands are conflicting. It is trying to run New-VM from the Hyper-V module. You can confirm this by running:
get-command New-VM -all
You should see two commands one from the Hyper-V module and one from the Vmware module.
To get past the problem you can add the module name to the command name:
VMware.VimAutomation.Core\New-VM
for using parameters inside INLINESTRING structure, you must use $using:<>

Azure deployment script keeps complaining "CurrentStorageAccount is not set"

I want to do automated deployment with Bamboo (a Continuous Integration solution). I found in here(http://www.windowsazure.com/en-us/develop/net/common-tasks/continuous-delivery/#script) a piece of script to help. The main code snippet:
#configure powershell with Azure 1.7 modules
Import-Module Azure
#configure powershell with publishsettings for your subscription
$pubsettings = $subscriptionDataFile
Import-AzurePublishSettingsFile $pubsettings
Set-AzureSubscription -CurrentStorageAccount $storageAccountName -SubscriptionName $selectedsubscription
write-host $storageAccountName
write-host "$selectedsubscription"
#set remaining environment variables for Azure cmdlets
$subscription = Get-AzureSubscription $selectedsubscription
$subscriptionname = $subscription.subscriptionname
$subscriptionid = $subscription.subscriptionid
$slot = $environment
#main driver - publish & write progress to activity log
Write-Output "$(Get-Date –f $timeStampFormat) - Azure Cloud Service deploy script started."
Write-Output "$(Get-Date –f $timeStampFormat) - Preparing deployment of $deploymentLabel for $subscriptionname with Subscription ID $subscriptionid."
Publish
$deployment = Get-AzureDeployment -slot $slot -serviceName $servicename
$deploymentUrl = $deployment.Url
Write-Output "$(Get-Date –f $timeStampFormat) - Created Cloud Service with URL $deploymentUrl."
Write-Output "$(Get-Date –f $timeStampFormat) - Azure Cloud Service deploy script finished."
I just first tried running this script from cmd by executing powershell E:\CI\OCZDeployment\publish_cloud_service.ps1 -environment Staging -serviceName "ocz" -storageAccountName "t2vsoft" -packageLocation OCZ.cspkg -cloudConfigLocation ServiceConfiguration.Cloud.cscfg -subscriptionDataFile E:\CI\OCZDeployment\SubscriptionDataFile.publishsettings -selectedsubscription "Development Subscription"
, where the publishsettings file is downloaded from Azure.
But I kept getting the error message:
New-AzureDeployment : CurrentStorageAccount is not set. Use Set-AzureSubscription subname -CurrentStorageAccount storageaccount to set it.
It has definitely been set in the snippet. I even tried copy & paste the Set-AzureSubscription line to inside New-AzureDeployment function. No luck.
Help me! Thank you!
Fixed. Don't know what was exactly the cause but i removed all registered azure subscriptions by executing:
PS> Get-AzureSubscription | ForEach-Object { Remove-AzureSubscription $_.SubscriptionName }
and re-imported the subscription by executing:
PS> Import-AzurePublishSettingsFile xxx.publishsettings
and it all worked! :)
There really were some similarly-named subscriptions in the subscription list. There might have been conflicts as such the azure scripts didn't know which one they really wanted.
If you have multiple subscriptions then you need to make sure the subscription that you want to use is set to the default. This can be done with the following:
Select-AzureSubscription -SubscriptionName "{subscriptionName}" -Default
if you aren't sure what your subscription name is you can use the following to see all of your subscriptions:
Get-AzureSubscription