I'm working on a Powershell script to create new virtual machines as part of a migration between a Hyper-V server running Hyper-V Server 2012R2 and a new Hyper-V server running Hyper-V Server 2019.
For the virtual machines that have one single virtual switch assigned to them, this script works fine, however, I have some virtual machines that have two switches. For those VM's, I get this message: Cannot convert 'System.Object[]' to the type 'System.String' required by parameter
.
How would I go about adding the second switch?
Import-Module -Name Hyper-V -RequiredVersion 1.1
$currentvms = Get-VM -ComputerName Hypervserver
foreach ($currentvm in $currentvms)
{
$name = ($currentvm).Name
$generation = ($currentvm).Generation
$startupmemory = ($currentvm).MemoryStartup
$virtualswitch = (Get-VMNetworkAdapter -VMName $name -ComputerName Hypervserver).SwitchName
$virtualswitchcount = (Measure-Object -InputObject $virtualswitch).Count
if ($virtualswitchcount -ne 1)
{
New-VM -Name $name -Generation $generation -MemoryStartupBytes $startupmemory -SwitchName $virtualswitch[0],$virtualswitch[1] -ComputerName Hypervserver2 -WhatIf
}
else
{
New-VM -Name $name -Generation $generation -MemoryStartupBytes $startupmemory -SwitchName $virtualswitch -ComputerName Hypervserver2 -WhatIf
}
}