Score:1

Adding multiple virtual switches to New-VM?

in flag

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
    }
    
}
djdomi avatar
za flag
IMHO stackoverflow.com would be a better fit for you
joeqwerty avatar
cv flag
Are you saying that a VM exported from Hyper-V running on Windows Server 2012 R2 can't be imported to Hyper-V running on Windows Server 2019? If so, where did you read that?
Davidw avatar
in flag
It appears I may have misread that information.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.