Quantcast
Channel: VMware Communities : Blog List - All Communities
Viewing all articles
Browse latest Browse all 3135

Re-register VMs from vCenter using PowerCLI

$
0
0

Sometimes protection of VMs in Site Recovery Manager (SRM) might fail with error 'VmAlreadyProtected'. A quick workaround would be to remove the vm from vCenter's inventory and add it back.

Below is a PowerCLI sample to remove all VMs and add them back.

 

# connect to VIServer

$vi = Connect-VIServer <VIServer_IP> -user <Username> -password <password>

 

# get vm list

$vm_list = Get-VM

 

# save working dir

$old_pwd = Get-Location

 

# loop over all vms

$skipped = 0;

$processed = 0;

$errorText = ''

 

$ErrorActionPreference = "Stop"; # Make all errors terminating

 

foreach ($vm in $vm_list) {

 

    # Shutdown VM

    try {

        if( $vm.powerstate -ne 'PoweredOff') {

            Write-Output "Shutting down vm '$vm'"

            Shutdown-VMGuest -VM $vm -Confirm:$false

         

            # Wait for Shutdown to complete

            # max wait time-out

            $timeout = 120  # unit: seconds

            do {

                #Wait 5 seconds

                Write-Output 'wait 5 seconds to check the power status...'

                Start-Sleep -s 5

                $timeout -= 5

                #Check the power status

                $status = (Get-VM -id $vm.id).PowerState

                $status

            }until(($status -eq "PoweredOff") -Or ($timeout -le 0))

         

            if( $timeout -le 0) {

                $t = "Error: Timed-out while shuting down vm '$vm', will ignore this one and continue."

                Write-Output $t

                $errorText += $t + "`n"

                $skipped++;

                continue

            }

        }

    }

    catch {

        $t = "Error: Caught exception while shuting down vm '$vm', will ignore this one and continue."

        $skipped++;

        Write-Output $t

        $errorText += $t + "`n"

        continue

    }

 

    $ds_path = ($vm | Get-Datastore).DatastoreBrowserPath        # sample: vmstores:\10.161.19.51@443\dc_pri\datastore1

    $vmHost  = $vm | get-vmhost

 

    # get vm folder name on datastore

    $vm.extensionData.config.files.VmPathName -match "\[.+\] (\S+)/"    # sample: [datastore1] test1_1/test1.vmx

    $folder = $matches[1]

    $vm_folderpath = $ds_path + "\" + $folder

 

    cd $vm_folderpath

 

    $vmxFile = Get-Item ($vm.name + ".vmx")

 

    try {

        Write-Output "Removing vm '$vm' with path '$vm_folderpath'"

        Remove-VM -VM $vm -Confirm:$false -DeletePermanently:$false

    }

    catch {

        $t = "Error: Caught exception while removing vm '$vm', will ignore this one and continue."

        Write-Output $t

        $errorText += $t + "`n"

        $skipped++;

        continue

    }

 

    try {

        Write-Output "Registering vm $vm..."

        New-VM -VMHost $vmhost -VMFilePath $vmxFile.DatastoreFullPath

    }

    catch {

        $t = "Caught exception while adding vm '$vm', will ignore this one and continue."

        $skipped++;

        Write-Output $t

        $errorText += $t + "`n"

        continue

    }

    $processed++

}

 

cd $old_pwd

 

Write-Output "Job finished. Processed: $processed, skipped: $skipped"

if( $errorText -ne '') {

    Write-Output "Errors: `n$errorText"

}


Viewing all articles
Browse latest Browse all 3135

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>