Move VM To New Subscription - azure

I have a new subscription to Azure but have existing VM's in a prior subscription. What is the easiest/best way to move my VM's to the new Subscription?
Thanks for reading.

I don't know if there's another option, but you can do this with powershell:
# Copy a virtual machine to a different subscription (no VNET)
.\vmcopy.ps1 -SourceSubscription "source subscription" `
-DestinationSubscription "destination subscription" `
-VirtualMachineName "existingvmname" `
-SourceServiceName "sourcecloudservice" `
-DestinationServiceName "destinationcloudservice" `
-DestinationStorageAccount "destinationstorageaccount" `
-Location "West US"
# Copy a virtual machine to a different subscription and specify an existing virtual network and subnet.
.\vmcopy.ps1 -SourceSubscription "source subscription" `
-DestinationSubscription "destination subscription" `
-VirtualMachineName "existingvmname" `
-SourceServiceName "sourcecloudservice" `
-DestinationServiceName "destinationcloudservice" `
-DestinationStorageAccount "destinationstorageaccount" `
-VNETName "DestinationVNET" `
-SubnetName "DestinationSubnet"
Source / more info
http://michaelwasham.com/2014/01/21/copy-a-windows-azure-virtual-machine-between-subscriptions/

For those who wonder if there is a better option: Yes, there is.
When you want to move instead of copy (as Thiago suggested) you should do the following:
Create a JSON file with this content:
{
"targetResourceGroup": "/subscriptions/<TARGET-SUBSCRIPTION-ID>/resourceGroups/<TARGET-RESOURCEGROUP-NAME>",
"resources": [
"<SOURCE-RESOURCE-ID>"
]
}
Note that you may have multiple resources which belongs to a VM: VM, cloudservice, storage account, VNet, reserved IP, ...
You need to add the resource IDs of all these resources into the JSON above.
Then you can run:
armclient post https://management.azure.com/subscriptions/<SOURCE-SUBSCRIPTION-ID>/resourceGroups/<SOURCE-RESOURCEGROUP>/moveResources?api-version=2015-01-01 #<PAHT_TO_JSON> -verbose
on your command line. You will ned armclient, which you can install by using choco:
choco install armclient

Related

PowerShell Task not running the script in Azure Pipelines?

I have a powershell task that is used to run a script which involves creating azure resources (Example: Resource group, Azure Key Vault, Function App...). When the pipeline is being run and it arrives to the powershell task in the deploy stage, it shows the following message:
The problem here, it says Finishing:Powershell but it didn't execute the script and did not create any azure resource.
Here is a sample of the powershell script:
$vaultName = "key vault name"
$blobstorageName = "blob storage name"
$Location = "Location Name"
$resourceGroupName = "Resource Group Name"
try {
#Creation of Resource Group
$resourceGroup = Get-AzResourceGroup -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
if($null -eq $resourceGroup)
{
New-AzResourceGroup -Name $resourceGroupName -Location $Location
}
else
{
Write-Host "The ResourceGroup with the name: $resourceGroupName already exists."
}
# Creation of Storage Account
$checkBlobStorage = (Get-AzStorageAccountNameAvailability -Name $blobstorageName) | Select-Object NameAvailable
if ($checkBlobStorage.NameAvailable)
{
New-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $blobstorageName -Location $Location -SkuName Standard_LRS -Kind StorageV2 -AccessTier Hot
}
else
{
Write-Host "The name $blobStorageName is not available. Suggest a new globally unique name!"
}
catch
{
}
Does anyone have a clue what is wrong ? Am I missing something in the powershell script (Maybe I don't have direct access to the azure portal from azure devops) or maybe something is missing in
the Yaml file ?
Two major issues:
you seem to be using the Powershell Task, which is not designed for communication with Azure. You should use the Azure Powershell task for this kind of script, because it already has the right modules loaded and the authentication prepared.
your script is swallowing the error so it is hiding what went wrong. It's usually more useful not to catch exceptions; if your script is erroring then let it error, and let the pipeline show you in its log what has happened.

How to add custom extension to existing vm using arm template

How to add the following custom extension to run the powershell script in already existing VM ? How to refer to existing VM ?
{
"condition":"[empty(parameters ('DR User Secret'))]",
"type":"Microsoft.Compute/virtualMachines/extensions",
"name":"[concat(parameters('vmName'),'/', 'customscript')]",
"apiVersion":"2015-06-15",
"location":"[resourceGroup().location]",
"properties":{
"publisher":"Microsoft.Compute",
"type":"CustomScriptExtension",
"typeHandlerVersion":"1.9",
"autoUpgradeMinorVersion":true,
"settings":{
"fileUris":[
]
},
"protectedSettings":{
"commandToExecute":"[concat('powershell -ExecutionPolicy Unrestricted -file ', 'C:\\test.ps1', ' -AdminPass ', parameters('Password'))]"
}
}
}
Here is an alternate way you may also try.
I created a VM using the ARM template.
Reference: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/ps-template
And I use the below following command that uses the Custom Script extension to download a script from a GitHub repository onto the target virtual machine and then run the script.
fileUris: The locations where the script files are stored.
Set-AzVMCustomScriptExtension -ResourceGroupName "v-rash18" -VMName "SampleVM" -Name "myCustomScript"
-FileUri "https://raw.githubusercontent.com/neilpeterson/nepeters-azure-templates/master/windows-custom-script-simple/support-scripts/Create-File.ps1"
`
-Run "Create-File.ps1" -Location "Central US".
Reference: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/features-windows#run-vm-extensions
To check the extension is set on the existing VM is Get-AzVMExtension

How to create a Linux AppService Plan with New-AzAppServicePlan?

What is the equivalient of this code using New-AzAppServicePlan?
az appservice plan create --resource-group $ServerFarmResourceGroupName `
--name $AppServicePlanName `
--is-linux `
--location $ResourceGroupLocation `
--sku $AppServicePlanTier `
--number-of-workers $NumberOfWorkers
Is there really no way to create an App Service Plan using Az Powershell? Why can it only be done via Azure CLI or ARM?
I only found this answer, which basically uses ARM directly: How do I use Powershell to create an Azure Web App that runs on Linux?
There are some issues about this, suppose for now this is not supported for New-AzureRmAppServicePlan, however you could use New-AzureRmResource to create a linux plan. You could try the below command.
New-AzureRmResource -ResourceGroupName <>group name -Location "Central US" -ResourceType microsoft.web/serverfarms -ResourceName <plan name> -kind linux -Properties #{reserved="true"} -Sku #{name="S1";tier="Standard"; size="S1"; family="S"; capacity="1"} -Force
I originally used my script to create a ConsumptionPlan (Y1) through PowerShell and AzureCLI because I don't like when Azure put a generated name when creating a ConsumptionPlan.
Please find my solution to create a Linux App Service Plan (B1) using New-AzResource:
$fullObject = #{
location = "West Europe"
sku = #{
name = "B1"
tier = "Basic"
}
kind = "linux"
properties = #{
reserved = $true
}
}
$resourceGroupName = "rg-AppServicePlanLinux"
$serverFarmName = "aspl-test"
Write-Host "Step 1: CREATING APP SERVICE PLAN B1:Basic named [$serverFarmName]"
# Create a server farm which will host the function app in the resource group specified
New-AzResource -ResourceGroupName $resourceGroupName -ResourceType "Microsoft.Web/serverfarms" -Name $serverFarmName -IsFullObject -PropertyObject $fullObject -Force
So I used the ARM template to understand which information you need to provide on the -PropertyObject parameter
It also now seems possible to do an App Service Plan Linux with New-AzAppServicePlan command since Az PowerShell 4.3.0 (June 2020) with the parameter -Linux
Az.Websites
Added safeguard to delete created webapp if restore failed in 'Restore-AzDeletedWebApp'
Added 'SourceWebApp.Location' for 'New-AzWebApp' and 'New-AzWebAppSlot'
Fixed bug that prevented changing Container settings in 'Set-AzWebApp' and 'Set-AzWebAppSlot'
Fixed bug to get SiteConfig when -Name is not given for Get-AzWebApp
Added a support to create ASP for Linux Apps
Added exceptions for clone across resource groups
Release Note: https://learn.microsoft.com/en-us/powershell/azure/release-notes-azureps?view=azps-5.6.0&viewFallbackFrom=azps-4.3.0#azwebsites-7
New-AzAppServicePlan: https://learn.microsoft.com/en-us/powershell/module/az.websites/new-azappserviceplan?view=azps-5.6.0
If you get "The Service is unavailable" after deploying your new Function app (Consumption Plan) with Azure CLI, please make sure the following statement from Microsoft:
https://github.com/Azure/Azure-Functions/wiki/Creating-Function-Apps-in-an-existing-Resource-Group
I waste the whole day because I got another Function App (Premium Plan) in the same resource group I used to deploy the Consumption one.
This worked for me:
Adding -Linux as a parameter to my command
New-AzAppServicePlan -ResourceGroupName $RESOURCE_GROUP_NAME -Name $APP_SERVICE_PLAN_NAME -Location $RESOURCE_LOCATION -Linux -Tier $APP_SERVICE_PLAN_TIER -NumberofWorkers $APP_SERVICE_PLAN_WORKERS -WorkerSize $APP_SERVICE_PLAN_WORKER_SIZE
Example:
New-AzAppServicePlan -ResourceGroupName 'MyResourceGroup' -Name 'MyServicePlan' -Location 'northeurope' -Linux -Tier 'PremiumV2' -NumberofWorkers 2 -WorkerSize Medium
That's all.
I hope this helps

Add Azure Custom Script Extension from Blob to Virtual Machine Scale Set by Powershell

To do this with a script that is publicly available this is no Problem using:
$publicSettings = #{
"fileUris" = (,"$uri");
"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File azure_cse_vm_initial_script.ps1 $argument"
}
Write-Host " ==> Add-AzureRmVmssExtension"
Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss `
-Name "customScript" `
-Publisher "Microsoft.Compute" `
-Type "CustomScriptExtension" `
-TypeHandlerVersion 1.8 `
-Setting $publicSettings
But how to do in case I use a storage account with a blob container? Can the access key be added to the Settings object? But how? And what to use for the URL.
The script I want to run should not be public accessible because it is the Installation script of my application.
Thanks,
Daniel
I would create a shared access signatur for that script (see Using shared access signatures). Then you can simple add the SAS token to the URI. E. g:
https://myaccount.blob.core.windows.net/sascontainer/sasblob.txt?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D
You could also use storage account name and storage account key to download the script: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#extension-schema

azure cannot create vm from vhd image

I'm currently running into difficulty in creating an Azure VM from a custom VM image. I am following the guide from Azure from here: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-capture-image/
I've used Waagent and deprovisioned the machine as instructed, and deallocated, generalized, and captured my machine image (I have made some modifications to the core Ubuntu 16.04LTS image available from Azure software wise). I have successfully created the template.json file (Can provide it if needed). I then completed all the tasks below in the powershell script as outlined in the article, just extracting the parameters to variables to make things a bit easier.
## Global
$rgName = "testrg"
$location = "eastus"
## Storage
$storageName = "teststore"
$storageType = "Standard_GRS"
## Network
$nicname = "testnic"
$subnetName = "subnet1"
$vnetName = "testnet"
$vnetAddressPrefix = "10.0.0.0/16"
$vnetSubnetAddressPrefix = "10.0.0.0/24"
$ipName = "TestIP"
## Compute
$vmName = "testvm"
$computerName = "testcomputer"
$vmSize = "Standard_D1_v2"
$osDiskName = $vmName + "osDisk"
#template
$fileTemplate = "C:\AzureTemplate\template.json"
azure group create $rgName -l $location
azure network vnet create $rgName $vnetName -l $location
azure network vnet subnet create --resource-group $rgName --vnet-name $vnetName --name $subnetName --address-prefix $vnetSubnetAddressPrefix
azure network public-ip create $rgName $ipName -l $location
azure network nic create $rgName $nicName -k $subnetName -m $vnetName -p $ipName -l $location
azure network nic show $rgName $nicname
azure group deployment create $rgName $computerName -f $fileTemplate
I am able to successfully run all the commands to create the resource group and the network components, however, when I try to run the deployment command at the bottom of the powershell script, I get the following and it just hangs here indefinitely. Am I using the right approach to create a VM from a custom image? Or is that Azure guide outdated?
azure group deployment create $rgName $computerName -f $fileTemplate
[32minfo[39m: Executing command [1mgroup deployment create[22m
[32minfo[39m: Supply values for the following parameters
EDIT: Link to image showing the issue: http://imgur.com/a/Fgh8K
I believe your understanding is not complete. If you see at the last line it says Supply values for the following parameters
You need to pass the values for VM name, the admin user name and password, and the Id of the NIC you created previously. My be you should re-read the documentation. Here is the screenshot for your reference from https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-capture-image/#deploy-a-new-vm-from-the-captured-image -

Resources