how to copy capture images in azure from one subscription to another subscription - powershell

i am trying to copy azure captured image from one subscription-another subscription currently in portal move is coming soon so i am trying to copy it and searching for some power shell script i don't want to create a vm from that with out creating vm i am trying to copy
it is managed disk through power shell i can copy managed disk from one subscription-another by creating vm from it but i am trying without creating vm i am trying copy or move capture image is this possible with power shell can any have idea about this.?

i am trying copy or move capture image is this possible with power
shell can any have idea about this.?
No, it is not possible. Image does not copy from one subscription to another subscription. You need copy image's managed disk to other subscription.
You have two option.
1.Using image's managed to create a snapshot and copy this snapshot to other subscription, then using this snapshot to create a managed disk, then create a image.
#Create a snapshot from managed disk
$disk = "/subscriptions/************/resourceGroups/SHUICLI/providers/Microsoft.Compute/disks/shui_OsDisk_1_21af43450987448184b5e9793da08e54"
$snapshot = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $region
$snapshotName = $imageName + "-" + $region + "-snap"
New-AzureRmSnapshot -ResourceGroupName $resourceGroupName -Snapshot $snapshot -SnapshotName $snapshotName
#copy the snapshot to another subscription, same region
$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
#change to the target subscription
Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId
$snapshotConfig = New-AzureRmSnapshotConfig -OsType Windows `
-Location $region `
-CreateOption Copy `
-SourceResourceId $snap.Id
$snap = New-AzureRmSnapshot -ResourceGroupName $resourceGroupName `
-SnapshotName $snapshotName `
-Snapshot $snapshotConfig
More information about this please refer to this blog.
2.Copy image's managed disk to a storage account, then using this VHD to create a new image.
##create $SAS
$sas = Grant-AzureRmDiskAccess -ResourceGroupName shui -DiskName shuitest -DurationInSecond 3600 -Access Read
$destContext = New-AzureStorageContext –StorageAccountName contosostorageav1 -StorageAccountKey 'YourStorageAccountKey'
Start-AzureStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer 'vhds' -DestContext $destContext -DestBlob 'MyDestinationBlobName.vhd'
See this answer.

Related

Create/Update PATH MAPPINGS of Azure WebApp using Azure Devops task or PowerShell

I want to update configure an Azure storage mount with Azure File Share(PATH MAPPINGS) for Azure WebApp on Containers via Azure DevOps pipeline. To achieve this, I am currently using an 'Azure PowerShell task' (code below). However, the PowerShell script is configuring the 'Advance' option, and exposes the Storage Account Access Key. Is there a way to configure the 'Basic' one.
PowerShell 'New-AzWebAppAzureStoragePath' creates an 'advance' configuration.
There is also a Devops task 'Azure webapp for containers' which allows me to set 'app settings' and 'configuration settings', but no option for 'path mappings'.
Is there any alternative in PowerShell or Azure devops task, to create a 'path mapping' to Azure file share with 'BASIC' configuration.
Script:
try{
# Check if AzureStoragePath (PATH MAPPINGS) already exists for the web app
$webapp = get-AzWebApp -ResourceGroupName $webAppResourceGroupName -Name $webAppName
if($webapp.AzureStoragePath -ne $null){
# 'Path Mapping' to Azure storage File does not exist. Proceed with creating one.
# Get Storage Account Primary Key
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $storageResourceGroupName -AccountName $storageAccountName).value[0]
$storageAccountContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Returns 'True' or 'False' depending whether File share exists or not
$storageShareObject = get-azstorageshare -Name $storageFileShareName -context $storageAccountContext -erroraction silentlycontinue
$storageShareExists = (($storageShareObject) -ne $null)
if($storageShareExists -ne 'True'){
$storageShareObjectFQDN = $storageShareObject.name + ".files.core.windows.net"
# Create a WebApp Storage Path object
$webAppStoragePathObject = New-AzWebAppAzureStoragePath -Name 'someConfig' -AccountName $storageShareObjectFQDN -Type AzureFiles -ShareName $storageFileShareName -AccessKey $storageAccountKey -MountPath "/edgemicro/config"
# Configure the 'Path mappings' for the web app
Set-AzWebApp -ResourceGroupName $webAppResourceGroupName -Name $webAppName -AzureStoragePath $webAppStoragePathObject
}
}
}catch{
# '$_' contains all the details about exception
$_
}
I can reproduce the same issue with Azure powershell.
However, You can use azure cli instead of azure powershell as workaround. See below command. Check here for more information.
az webapp config storage-account add --resource-group $storageResourceGroupName --name $webAppName --custom-id "someConfig" --storage-type "AzureFiles" --share-name $storageFileShareName --account-name $storageAccountName --access-key $storageAccountKey --mount-path "/parent"
With above azure cli command, you can create a basic configuration. See below screenshot:

Creating OS disk from VHD stored in another storage account

Have an existing PowerShell script that is creating VM's using pre-created VHD's stored in a storage account (copied across regional storage accounts for speed).
In PS we can use the following:
New-AzureRmDisk -DiskName $osDiskName -Disk `
(New-AzureRmDiskConfig -AccountType Premium_LRS `
-Location $location -CreateOption Import `
-StorageAccountId $storageAccountId `
-SourceUri $osVHDUri) `
-ResourceGroupName $resourceGroupName
$osDisk = Get-AzureRMDisk -DiskName $osDiskName -ResourceGroupName $resourceGroupName
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $osDisk.Id -CreateOption Attach -Windows -StorageAccountType Premium_LRS
Where $storageAccountId is similar to:
/subscriptions/{0}/resourceGroups/my-snapshot/providers/Microsoft.Storage/storageAccounts/mysnapshots -f $sourceSnapshotSubscriptionId
In the .net Azure SDK, I don't see a way to replicate this? When I try to create, it's saying unable to find, yet my PS works ok.
If you want to use an existing disk instead of a marketplace image, use this code:
var managedDisk = azure.Disks.Define("myosdisk")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithWindowsFromVhd("https://mystorage.blob.core.windows.net/vhds/myosdisk.vhd")
.WithSizeInGB(128)
.WithSku(DiskSkuTypes.PremiumLRS)
.Create();
azure.VirtualMachines.Define("myVM")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetworkInterface(networkInterface)
.WithSpecializedOSDisk(managedDisk, OperatingSystemTypes.Windows)
.WithExistingAvailabilitySet(availabilitySet)
.WithSize(VirtualMachineSizeTypes.StandardDS1)
.Create();
Check this link for further reference. Hope it helps.

Azure Runbook - Get a file from Azure File System Storage

I am creating a Azure workflow runbook wherein I have to get a file from Azure File System Storage and publish that to a azure web app.
I tried with New-PSDrive but that command is not supported in runbook (even InlineScript doesn't work). Could anyone help me with the script. In the below code I need to populate file path from azure file system.
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationId $Conn.ApplicationID `
-CertificateThumbprint $Conn.CertificateThumbprint
$zipFilePath = ???
Publish-AzureWebsiteProject -Name $siteName -Package $zipFilePath
I searched a lot but couldn't find much information on this.
Are you referring to a file in a Azure Storage account? If so, that is pretty easy to accomplish. Add the following to your Runbook, filling in the required information:
$StorageAccountKey = Get-AutomationVariable -Name 'storageKey'
$Context = New-AzureStorageContext -StorageAccountName 'your-storage' `
-StorageAccountKey $StorageAccountKey
Get-AzureStorageFileContent -ShareName 'your-share' -Context $Context `
-path 'your-file' -Destination 'C:\Temp'
$filePath = Join-Path -Path 'C:\Temp' -ChildPath 'your-file'
You also need to create an variable in your Automation Account, called "storageKey" containing your Storage Accounts key.
Mounting Azure File share as a drive is not currently supported in Automation cloud jobs, though it will probably be supported in a few months. In the meantime, use the Get-AzureStorageFile command from the Azure.Storage module to retrieve the file to a temp folder.
Alternatively, run this job on a Hybrid worker. In this case, make sure all the prerequisites are met in order to mount the share as a network drive.

Create a Disk in Azure New Portal is not working , old portal I could do it easily

I'm not even sure why azure even has a GUI Website. It is starting to feel a bit ridiculous when the old manage.windowsazure.com I could powershell up a VHD, and then very easily use a Storage and container and Add the image and then choose from gallery of my own images.
NOW I read that in May 2017 a lot of things with the old portal are going away. I created a Storage Account myvmblobs and then a container mywincontainer and then I uploaded a VHD , tmppro2.vhd is sitting there as a VHD Blob
URL https://myvmblobs.blob.core.windows.net/mywincontainer/TMPPRO2.VHD
So I read that I could create a Disk image from powershell ( I have to no way to do it with website portal.azure.com )
Add-AzureDisk 'tmppro2' -MediaLocation https://myvmblobs.blob.core.windows.net/mywincontainer/TMPPRO2.VHD -Label 'OS' -OS "Windows"
However, I don't know if the Label or OS is important...
Add-AzureDisk : BadRequest: The storage account with the name myvmblobs as specified in the VHD URI https://myvmblobs.blob.core.windows.net/mywincontainer/TMPPRO2.VHD does not exists in the current subscription
According to your description, we can use PowerShell or template to create new VM in Azure ARM module.
About PowerShell, here is a example script(use existing VHD):
$rgname = "jason-newgroup"
$loc = "japaneast"
$vmsize = "Standard_DS1_v2"
$vmname = "jason-newtest2"
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize
$nic = Get-AzureRmNetworkInterface -Name ("jason-newtest45") -ResourceGroupName $rgname
$nicId = $nic.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nicId
$osDiskName = "jason-newtest"
$osDiskVhdUri = "https://jasonnewgroupdisks912.blob.core.windows.net/vhds/jason-newtest201681285042.vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption attach -Linux
New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm
Use template to create Azure VM:
Here is the template.
Update:
We can use azure storage explorer to upload VHD to Azure:

Creating Linux Virtual Machine in Azure from Image in Powershell

I want to be able to create a new Azure Virtual Machine using Powershell with a custom image. This custom image is in Azure Storage Account X and the new Virtual Machine will be created using Azure Storage Account Y. When I run these commands to create the new Virtual Machine:
Set-AzureSubscription -CurrentStorageAccount $StorageAccount -SubscriptionName $SubscriptionName
New-AzureVMConfig -Name $MachineName -InstanceSize $InstanceSize -ImageName $Image |
Add-AzureProvisioningConfig -Linux -LinuxUser $LinuxUser -Password $AdminPassword |
Set-AzureSubnet $subNet |
New-AzureVM -ServiceName $MachineName -VNetName $VNet -ErrorVariable errs
where $Image is the custom image in Storage Account X and CurrentStorageAccount set to Y for the Subscription, I get the following error:
The disk's VHD must be in the same account as the VHD of the source image
If the custom image is in Storage Account Y the commands to create the new Virtual Machine work correctly.
Can an Azure Virtual Machine be created when the image is in a different Storage Account?
Apparently not. However, you could copy the image from storage account X to storage account Y before you create the new VM. It would only add a few more lines to your script.
Here is an example:
http://michaelwasham.com/windows-azure-powershell-reference-guide/copying-vhds-blobs-between-storage-accounts/