adbertram / AzureVmSnapshots

A small PowerShell module to automate creating and restoring Azure VM snapshots
12 stars 6 forks source link

Restore-AzVmSnapshot -VmName not found #3

Open NachoHawk opened 5 years ago

NachoHawk commented 5 years ago

When trying to restore snapshot getting error "The Resource 'Microsoft.Compute/virtualMachines/Win' under resource group 'xxxxxx' was not found." Get-AzVM shows that the resource is there

CloseError: (:) [Get-AzVM], ### ### ComputeCloudException

2019-11-04 17_59_22-pwsh (Admin)

2019-11-04 17_57_33-pwsh (Admin)

2019-11-04 18_01_45-Window

adbertram commented 5 years ago

I just pushed a fix that should resolve this. Let me know how you fare now.

ajunlee commented 4 years ago

https://github.com/adbertram/AzureVmSnapshots/blob/e78b38f0a9945d07d50b5fcb65dea119b944368e/Public/Restore-AzVmSnapshot.ps1#L35

It didn't fix this issue and the restore function is broken by it. The root cause of this issue is the "-" is contained in the vm name, so, split the snapshot name by "-", will split the vm name too. This fix is trying to split by "AzVmSnapshot-", and it will get the vmname with timestamp. so the vm not found issue is still existing.

According to the naming rule of snapshot, suggest to change following line $vmName = ($Snapshot.Name -split 'AzVmSnapshot-')[1] to $vmName = $Snapshot.Name.substring("AzVmSnapshot-".length, $Snapshot.Name.LastIndexOf('-') - "AzVmSnapshot-".length)

adbertram commented 4 years ago

@ajunlee Would you mind issuing a PR?