none
Powershell script to De-protect / Re-protect VM

    Question

  • Hello 

    The Company that i work for use ASR to replication between 2 Data centers. There are many issues around the configuration and our project team is working with MS to resolve this.

    MS has stated that when changing the memory/CPU or adding a disk on a VM protected by ASR you need to do this following in SCVMM

    • Select the VM
    • Right click and select "Migrate Virtual Machine"
    • Untick “Protect this VM”
    • Select the host the VM is currently running on
    • Click Next
    • Specify the unprotected CSV Volume number to Storage Migrate the VM to
    • Click Move

    Now this is simple via the GUI but this is time consuming, so i tasked my self to write this process in PowerShell to automate it. So far i have the below which is not working as intended

    Import-Module virtualmachinemanager
    
    $Server = "<VM Name>"
    $Primary = "<Primary cluster>"
    
    # Gets the VM details
    $Vm = Get-SCVirtualMachine $Server -VMMServer $Primary
    
    # gets the current vol path
    $path = (([String]$vm.Location).ToLower()).Replace("\$(($vm.name).ToLower())","")
    
    # Which i can query to find current vol
    $originalVol = Get-SCStorageVolume -VMHost $vm.vmhost | ?{$_.name -eq $path }
    
    # Selects the Vol to be migrated to
    $TempVolume = Get-SCStorageVolume -VMHost $vm.vmhost | ?{$_.VolumeLabel -eq "<Unprotected CSV name>"}
    
    # Records the currect replication group
    $ReplicationGroup = $vm.ReplicationGroup
    
    # Here is the command to do the migration
    $Vm | Move-SCVirtualMachine -VMHost $Vm.VMHost -Path $TempVolume.ObjectID -RunAsynchronously -UseDiffDiskOptimization -ReplicationGroup $null
    
    # At this stage I have been watching to make sure the VM completes successfully
    
    # Gets the VM details after the migration completes
    $Vm = Get-SCVirtualMachine $Server -VMMServer $Primary
    
    # This command in threoy should reprotect the VM (Untested yet)
    $Vm | Move-SCVirtualMachine -VMHost $Vm.VMHost -Path $originalVol.ObjectID -RunAsynchronously -UseDiffDiskOptimization -ReplicationGroup $ReplicationGroup
    

    When i run the first part to de-protect the VM it works, the VM is removed from ASR. The volume looks odd with a -1 at the end

    • \\?\volume{137b0cf5-2e56-41d9-86d8-ccdd6879a96d}-1

    also when I use the GUI to re-protect the VM, it is no longer HA so i select HA and Protect and this works. 

    I think the issue is that the command Move-SCVirtualMachine is not the right cmdlet to use, there is not switch to simulate "Untick 'Protect this VM'". I'm thinking that a Set-scvirtualmachine –ClearDRProtection | Move-SCStorageVolume is required?

    If anyone has a script or a link to one that has already been tested, or can help me solve my issue that would be great.

     
    Monday, February 6, 2017 9:30 PM

Answers

  • Hi Sir,

    >>Now this is simple via the GUI but this is time consuming

     

    Have you checked the scripts displayed in "View Script" at the end of GUI wizard :

    Best Regards,

    Elton


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Marked as answer by Grant-Nz Sunday, February 12, 2017 7:07 PM
    Friday, February 10, 2017 5:40 AM
    Moderator

All replies

  • Hi Sir,

    >>Now this is simple via the GUI but this is time consuming

     

    Have you checked the scripts displayed in "View Script" at the end of GUI wizard :

    Best Regards,

    Elton


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Marked as answer by Grant-Nz Sunday, February 12, 2017 7:07 PM
    Friday, February 10, 2017 5:40 AM
    Moderator
  • WOW Elton thank you I missed that's completely.

    Cheers

    Grant

    Sunday, February 12, 2017 7:24 PM