'Need help spiting Azure snapshots into another tenant/subscription

I have a script which snapshots all my disks in a certain RG.

However when I do the snapshotting, I need them to be spat out into another tenant/subscription for a migration project!

I've got as far as snapshotting everything and spitting them into a different RG but I need to take it a step further and spit them into the same named RG but in a different tenant/sub.

My script is below:

Login-AzureRmAccount -Credential $psCred –SubscriptionId $SubscriptionId -ErrorAction Stop | out-null
Connect-AzureRmAccount
Get-AzureRmSubscription -SubscriptionId $SubscriptionId | Select-AzureRmSubscription

$tagResList = Get-AzureRmResource -TagName Environment -TagValue Staging
#$tagResList = Find-AzureRmResource -ResourceGroupNameEquals testrs

#$tagRsList[0].ResourceId.Split("//")
#subscriptions
#<SubscriptionId>
#resourceGroups
#<ResourceGroupName>
#providers
#Microsoft.Compute
#virtualMachines
#<vmName>

foreach($tagRes in $tagResList) { 
        if($tagRes.ResourceId -match "Microsoft.Compute")
        {
            $vmInfo = Get-AzureRmVM sandbox207478603000 #$tagRes.ResourceId.Split("//")[4] -Name $tagRes.ResourceId.Split("//")[8]

                #Set local variables
                $location = $vmInfo.Location
                $resourceGroupName = $vmInfo.ResourceGroupName
                $timestamp = Get-Date -f MM-dd-yyyy_HH_mm_ss

                #Snapshot name of OS data disk
                $snapshotName = $vmInfo.Name + $timestamp 

                #Create snapshot configuration
                $snapshot =  New-AzureRmSnapshotConfig -SourceUri $vmInfo.StorageProfile.OsDisk.ManagedDisk.Id -Location $location  -CreateOption copy
                
                #Take snapshot
                New-AzureRmSnapshot -Snapshot $snapshot -SnapshotName $snapshotName snapshots $resourceGroupName 
                
                
                if($vmInfo.StorageProfile.DataDisks.Count -ge 1){
                        #Condition with more than one data disks
                        for($i=0; $i -le $vmInfo.StorageProfile.DataDisks.Count - 1; $i++){
                                
                            #Snapshot name of OS data disk
                            $snapshotName = $vmInfo.StorageProfile.DataDisks[$i].Name + $timestamp 
                            
                            #Create snapshot configuration
                            $snapshot =  New-AzureRmSnapshotConfig -SourceUri $vmInfo.StorageProfile.DataDisks[$i].ManagedDisk.Id -Location $location  -CreateOption copy
                            
                            #Take snapshot
                            New-AzureRmSnapshot -Snapshot $snapshot -SnapshotName $snapshotName snapshots $ResourceGroupName
                        }
                    }
                else{
                        Write-Host $vmInfo.Name + " doesn't have any additional data disk."
                }
        }
        else{
            $tagRes.ResourceId + " is not a compute instance"
        }
}

$tagRgList = Get-AzureRmResourceGroup -Tag @{ Environment = "Staging" }


Solution 1:[1]

I am not sure if you can save snapshot in another tenant in one command as you'd need to be authenticated there. I would suggest using azcopy tool to move snapshot files between storage accounts

#######################################

Reviewed your comment and found that indeed you can't use azcopy on vm images.

But you may create access to the snapshot

#Generate the SAS for the snapshot 
$sas = Grant-AzSnapshotAccess -ResourceGroupName $ResourceGroupName -SnapshotName $SnapshotName  -DurationInSecond $sasExpiryDuration -Access Read

and save it to the destination storage account:

#Copy the snapshot to the storage account 
Start-AzStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName

More details canbe found here

Solution 2:[2]

You may use subprocess module. I found a similar question to your and I do encourage reading all answers and specially @jfs answer

https://stackoverflow.com/a/15055133/5203248

I will quote the important part from his answer to how to do it with subprocess

If you want to run a specific application then you could use subprocess module e.g., Popen() allows to start a program without waiting for it to complete:

import subprocess

p = subprocess.Popen(["notepad.exe", fileName])
# ... do other things while notepad is running
returncode = p.wait() # wait for notepad to exit

There are many ways to use the subprocess module to run programs e.g., subprocess.check_call(command) blocks until the command finishes and raises an exception if the command finishes with a nonzero exit code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Moataz Farid