VMware Cloud Community
emcclure
Enthusiast
Enthusiast
Jump to solution

Trying to create a script to move VM's serially from multiple datastores to one datastore

So I've been tasked with moving VM's from multiple old datastores to one new datastore.  Unfortunately the way things were setup (not my doing) was a convoluted mess.  I basically want to connect to a host, get the list of VM's on it and migrate them to the new datastore that will also be mounted on that host.  So far I've come up with something like this:

Get-Datacenter | select Name | Format-Table #Lists available datacenters
$mydatacenter = Read-Host "Choose a datacenter"
Get-Datacenter $mydatacenter | Get-VMHost | where {$_.ConnectionState -eq "Connected"} | sort Name | Select Name,ConnectionState,PowerState | Format-Table  #Lists available hosts in the chosen datacenter
$esxhost = Read-Host "Select the host to migrate VM's in"
Get-Datastore -VMHost $esxhost | sort Name | Format-Table
$destinationdatastore = Read-Host "Select the datastore to migrate VM's to"
$selectedvms = Get-VMHost $esxhost | Get-VM | sort Name | Out-GridView -OutputMode Multiple #Opens a new window allowing you to select multiple VM's

if($selectedvms){
foreach ($vm in $selectedvms)
{
Move-VM -VM $vm.Name -Destination $destinationdatastore #Moves to the new datastore
}
}

But when I try to run it, it winds up failing on the migration.  I'm not sure what I'm missing on this.  I do plan on making a change to look for the NFS datastore to migrate to, but for now I'm doing a quick test from a NFS storage to a local storage.  The error I get is below:

Move-VM : 10/15/2018 12:12:38 PM        Move-VM         Could not find VIContainer with name 'datastore_localhost'.     

At C:\Users\emcclure\Desktop\GenScripts\GeneCMMigrateVMs.ps1:34 char:2

+     Move-VM -VM $vm.Name -Destination $destinationdatastore #Moves to ...

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (datastore_localhost:String) [Move-VM], VimException

    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle

   ts.Commands.MoveVM

0 Kudos
1 Solution

Accepted Solutions
emcclure
Enthusiast
Enthusiast
Jump to solution

Ok I got this to work.  Here's the code:

Get-Datacenter | select Name | Format-Table #Lists available datacenters
$mydatacenter = Read-Host "Choose a datacenter"
Get-Datacenter $mydatacenter | Get-VMHost | where {$_.ConnectionState -eq "Connected"} | sort Name | Select Name,ConnectionState,PowerState | Format-Table  #Lists available hosts in the chosen datacenter
$esxhost = Read-Host "Select the host to migrate VM's in"
Get-Datastore -VMHost $esxhost | sort Name | Format-Table #Lists available datastores
$destinationdatastore = Read-Host "Select the datastore to migrate VM's to"
$selectedvms = Get-VMHost $esxhost | Get-VM | sort Name | Out-GridView -OutputMode Multiple #Opens a new window allowing you to select multiple VM's

if($selectedvms){
foreach ($vm in $selectedvms)
{
Write-Host "Relocating VM:" $vm.Name "to" $destinationdatastore
Get-VM -Name $vm.Name | Move-VM -datastore $destinationdatastore -diskstorageformat thin #Moves to the new datastore
}
}

Write-Host "1.21 gigawatts?  1.21 gigawatts?  Great scott."

View solution in original post

0 Kudos
1 Reply
emcclure
Enthusiast
Enthusiast
Jump to solution

Ok I got this to work.  Here's the code:

Get-Datacenter | select Name | Format-Table #Lists available datacenters
$mydatacenter = Read-Host "Choose a datacenter"
Get-Datacenter $mydatacenter | Get-VMHost | where {$_.ConnectionState -eq "Connected"} | sort Name | Select Name,ConnectionState,PowerState | Format-Table  #Lists available hosts in the chosen datacenter
$esxhost = Read-Host "Select the host to migrate VM's in"
Get-Datastore -VMHost $esxhost | sort Name | Format-Table #Lists available datastores
$destinationdatastore = Read-Host "Select the datastore to migrate VM's to"
$selectedvms = Get-VMHost $esxhost | Get-VM | sort Name | Out-GridView -OutputMode Multiple #Opens a new window allowing you to select multiple VM's

if($selectedvms){
foreach ($vm in $selectedvms)
{
Write-Host "Relocating VM:" $vm.Name "to" $destinationdatastore
Get-VM -Name $vm.Name | Move-VM -datastore $destinationdatastore -diskstorageformat thin #Moves to the new datastore
}
}

Write-Host "1.21 gigawatts?  1.21 gigawatts?  Great scott."

0 Kudos