Hello,
I'm trying to write a script to migrate multiple hosts from one cluster to another. I'm not finding much luck on the google, so I'm hoping I can get help here. Here's the basic script (sorry I don't know how to get the code to look all pretty):
#Datacenter
Get-Datacenter | select Name | Format-Table #Lists available datacenters
$mydatacenter = Read-Host "Choose a datacenter"
Get-Datacenter $mydatacenter | Get-Cluster | select Name | Format-Table
$fromcluster = Read-Host "Select a cluster to migrate hosts from"
$tocluster = Read-Host "Select a cluster to migrate the hosts to"
#ESX Hosts to migrate
Write-Host "Select the hosts that you want to migrate"
Get-Cluster $fromcluster | Get-VMHost | where {$_.ConnectionState -eq "Maintenance"} | sort Name | Select Name,ConnectionState,PowerState | Out-GridView -OutputMode Multiple #Lists hosts that are powered on and in "Maintenance" state
#Cluster to migrate to
foreach ($vmhost in $fromcluster)
{
Move-VMHost -VMHost $vmhost -Destination $tocluster -WhatIf
}
I basically get an error stating it couldn not find a VMhost with name <cluster name>. What am I missing? I feel like it's something so simple, but I'm still new enough to this that I can't quite figure it out just yet.
Try like this.
You have to capture the entries you selected in the GridView ($selectedHosts).
The objects returned for waht you selected in Out-GridView, are not PowerCLI objects,
so use the Name property on the VMHost parameter on Move-VM
#Datacenter
Get-Datacenter | select Name | Format-Table #Lists available datacenters
$mydatacenter = Read-Host "Choose a datacenter"
Get-Datacenter $mydatacenter | Get-Cluster | select Name | Format-Table
$fromcluster = Read-Host "Select a cluster to migrate hosts from"
$tocluster = Read-Host "Select a cluster to migrate the hosts to"
#ESX Hosts to migrate
Write-Host "Select the hosts that you want to migrate"
$selectedHosts = Get-Cluster $fromcluster | Get-VMHost | where {$_.ConnectionState -eq "Maintenance"} | sort Name | Select Name,ConnectionState,PowerState |
Out-GridView -OutputMode Multiple #Lists hosts that are powered on and in "Maintenance" state
#Cluster to migrate to
if($selectedHosts){
foreach ($vmhost in $selectedHosts)
{
Move-VMHost -VMHost $vmhost.Name -Destination $tocluster -WhatIf
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this.
You have to capture the entries you selected in the GridView ($selectedHosts).
The objects returned for waht you selected in Out-GridView, are not PowerCLI objects,
so use the Name property on the VMHost parameter on Move-VM
#Datacenter
Get-Datacenter | select Name | Format-Table #Lists available datacenters
$mydatacenter = Read-Host "Choose a datacenter"
Get-Datacenter $mydatacenter | Get-Cluster | select Name | Format-Table
$fromcluster = Read-Host "Select a cluster to migrate hosts from"
$tocluster = Read-Host "Select a cluster to migrate the hosts to"
#ESX Hosts to migrate
Write-Host "Select the hosts that you want to migrate"
$selectedHosts = Get-Cluster $fromcluster | Get-VMHost | where {$_.ConnectionState -eq "Maintenance"} | sort Name | Select Name,ConnectionState,PowerState |
Out-GridView -OutputMode Multiple #Lists hosts that are powered on and in "Maintenance" state
#Cluster to migrate to
if($selectedHosts){
foreach ($vmhost in $selectedHosts)
{
Move-VMHost -VMHost $vmhost.Name -Destination $tocluster -WhatIf
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
That was it. Works like a charm. Thanks so much.