#定义连接到Vcenter的信息
$vc = "vc.dk"
$user = "Administrator@v.com"
$password = "Yi"
#连接到Vcenter
Connect-VIServer -Server $vc -username $user -Password $password
#定义需要迁移的虚机IP
[array]$ips = gc D:\Operations\Scripts\vmmove.txt.txt
#分别定义存储目标虚机的宿主、目标存储
$vmhost = "10.10.20.103"
#迁移目标宿主IP
$vmserver_dst = "10.10.20.114"
$datastore_dst = "data" + ($vmserver_dst.Split('.')[2..3] -join '.')
#先提前通过IP获取虚机名称,将其放到一个新文件。虚机在关机状态下无法获取IP
$host103 = Get-VMHost -Name $vmhost
$vms = Get-VM | ? {$_.VMHost -eq $host103}
foreach ($vm in $vms)
{
$vmip = $vm.guest.IPAddress[0]
if ($vmip -in $ips)
{
$vm.name |out-file D:\Operations\Scripts\vmmove_name.txt -Append
}
}
#Move VM
$vs= gc D:\Operations\Scripts\vmmove_name.txt
foreach ($v in $vs)
{
$v
# get-vm $v |Move-VM -Destination $vmserver_dst -Datastore $datastore_dst
}
#Start VM
start-sleep -s 10
foreach ($v in $vs)
{
$v
Get-VM -Name $v |Start-VM
sleep -s 10
}
#断开连接
Disconnect-VIServer -Server $vc -Confirm:$false