Automated vSphere to HyperV

VM image converter (VMDK, VHD, VHDX, IMG, RAW, QCOW and QCOW2), P2V migrator

Moderators: anton (staff), art (staff), Max (staff), Anatoly (staff)

Post Reply
pacificeve
Posts: 1
Joined: Tue May 14, 2024 10:56 am

Tue May 14, 2024 11:02 am

I wrote this script to automated pulling down vsphere vm's and converting to hyper-v. While the script does work (this config is for remote, have to change to local if running on hyperv server itself.)

However this only converts the virtual disk, I'm looking for something that does exactly what the gui does where you select esxi to hyper-v and it exports the full VM with all its settings and puts in Hyper-V for you. Not sure if this is an option in either the default exe or the console exe. Any assistance would be appriciated.
# Load VMware PowerCLI
# Import-Module VMware.PowerCLI

# Step 1: Prompt for vCenter connection details
$serverIP = Read-Host "Enter vCenter Server IP"
$cred = Get-Credential -Message "Enter your vCenter credentials"

# Connect to the vCenter Server
Connect-VIServer -Server $serverIP -Credential $cred

# Step 2: Get VM and VMHost information
$vmHosts = Get-VMHost
$allVMs = Get-VM

# Display the VMHost and VM data
$vmHosts | Format-Table Name, ConnectionState, PowerState, NumCpu, CpuUsageMhz, CpuTotalMhz, MemoryUsageGB, MemoryTotalGB, Version
$allVMs | Format-Table Name, PowerState, NumCPUs, MemoryGB

# Fetch and display all datastores
$datastores = Get-Datastore | Sort-Object Name
$datastores | Format-Table Name, FreeSpaceGB, CapacityGB

# Ask the user to specify the datastore
$datastoreName = Read-Host "Enter the name of the datastore where the VMs are stored"
$selectedDatastore = ($datastores | Where-Object { $_.Name -eq $datastoreName })[0] # Select the first datastore with the specified name

if (-not $selectedDatastore) {
Write-Host "Datastore not found. Please verify the name and try again."
exit
}


# Filter VMs by name and exclude specific names
$filteredVMs = $allVMs | Where-Object {
$_.Name -notmatch "vCLS|vc|NTP|vSphere_Rep|Automation"
}


# Step 4: Prompt for Hyper-V host details
$hyperVHost = Read-Host "Enter the IP address of the Hyper-V host"
$hyperVCred = Get-Credential -Message "Enter your Hyper-V host credentials"
$driveLetter = Read-Host "Enter the drive letter on Hyper-V host for storing VMs (e.g., E)"

# Step 5: Convert VMs using StarWind V2V
# Define the full path to the V2V Converter
$V2VPath = "C:\Program Files\StarWind Software\StarWind V2V Converter\V2V_ConverterConsole.exe"
$totalVMs = $filteredVMs.Count
$convertedCount = 0
$totalTime = New-TimeSpan

foreach ($vm in $filteredVMs) {
$vmDir = "$driveLetter`:\$($vm.Name)"
New-Item -Path $vmDir -ItemType Directory -Force

$startTime = Get-Date
$inFileName = "[$($selectedDatastore.Name)] $($vm.Name)/$($vm.Name).vmdk"
$outFileName = "$vmDir\$($vm.Name).vhdx"

$arguments = @(
"convert",
"in_host_type=esx",
"in_host_address=$serverIP",
"in_host_username=`"$($cred.UserName)`"",
"in_host_password=`"$($cred.GetNetworkCredential().Password)`"",
"in_file_name=`"$inFileName`"",
"out_host_type=win",
"out_host_address=$hyperVHost",
"out_host_username=$($hyperVCred.UserName)",
"out_host_password=$($hyperVCred.GetNetworkCredential().Password)",
"out_file_name=`"$outFileName`"",
"out_file_type=ft_vhdx_growable"
)

Write-Host "Starting conversion for $($vm.Name)..."
Start-Process -FilePath $V2VPath -ArgumentList $arguments -NoNewWindow -Wait

$endTime = Get-Date
$timeTaken = $endTime - $startTime
$totalTime += $timeTaken
$convertedCount++

if ($LASTEXITCODE -ne 0) {
Write-Host "Error converting $($vm.Name)."
} else {
Write-Host "Successfully converted $($vm.Name) to $outFileName."
}

Write-Host "$convertedCount out of $totalVMs VMs converted. Time taken for this VM: $($timeTaken.ToString('hh\:mm\:ss')). Total time elapsed: $($totalTime.ToString('hh\:mm\:ss'))"
}

# if running local to hyper-v I believe comment out
# "out_host_address=$hyperVHost",
# "out_host_username=$($hyperVCred.UserName)",
# "out_host_password=$($hyperVCred.GetNetworkCredential().Password)",
yaroslav (staff)
Staff
Posts: 2438
Joined: Mon Nov 18, 2019 11:11 am

Tue May 14, 2024 2:15 pm

Welcome to StarWind Forum and thanks for sharing the script. Sadly, VM conversion is not available in CLI.
We are planning to introduce bulk conversions via GUI in future builds. Stay tuned!
Post Reply