question

BartHofstede-5480 avatar image
0 Votes"
BartHofstede-5480 asked ElevenYu-MSFT answered

HyperV VM creation assign virtual switch with powershell

I'm testing with creating vm's and VM settings with powershell to do for some automation.

But i'm not able to acomplish something as simple as setting the correct virtual-switch for the VM :(

new-vm -Name "rr-rds2" -MemoryStartupBytes 45056MB -BootDevice NetworkAdapter -NewVHDPath "D:\hyperv\rr-rds2\Virtual Machines\rr-rds2.vhdx" -Path "D:\hyperv\rr-rds2\Virtual Machines\" -Generation 2 -NewVHDSizeBytes 250GB -SwitchName "VM's - RR"

When i'm removing this switch -SwitchName "VM's - RR" the new virtual machine is created with the network adaptper virtual switch status not connected.

What is the correct way to achieve this when creating the vm after the VM is created?

windows-server-powershellwindows-server-hyper-v
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ElevenYu-MSFT avatar image
0 Votes"
ElevenYu-MSFT answered

Hi,

Please try below command:

New-VM -Name rr-rds2 -MemoryStartupBytes 45056MB -BootDevice NetworkAdapter -NewVHDPath D:\hyperv\rr-rds2\Virtual Machines\rr-rds2.vhdx -Path D:\hyperv\rr-rds2\Virtual Machines -NewVHDSizeBytes 250GB -Generation 2 -Switch VM's-RR

I think you need to replace "SwitchName" parameter with "Switch" parameter. Also, please ensure your switch name is correct. There should not be spacing character in the name.

If the problem persists, please take a screenshot of the command running result. Thanks.

Thanks,


If the Answer is helpful, please click "Accept Answer" and upvote it. Thanks.





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ElevenYu-MSFT avatar image
0 Votes"
ElevenYu-MSFT answered

Hi,

Could you clarify more about your requirement? What would you like to achieve when creating VM by PowerShell?

If your PowerShell command does not include switch part "- switch <SwitchName>", the VM's networking configuration will be "not connected" by default.

Please kindly check below article for reference.

Create a virtual machine by using Windows PowerShell
https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/create-a-virtual-machine-in-hyper-v#create-a-virtual-machine-by-using-windows-powershell

Thanks,


If the Answer is helpful, please click "Accept Answer" and upvote it. Thanks.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BartHofstede-5480 avatar image
0 Votes"
BartHofstede-5480 answered

When i create the machine with the -SwitchName "VM's - RR"

new-vm -Name "rr-rds2" -MemoryStartupBytes 45056MB -BootDevice NetworkAdapter -NewVHDPath "D:\hyperv\rr-rds2\Virtual Machines\rr-rds2.vhdx" -Path "D:\hyperv\rr-rds2\Virtual Machines\" -Generation 2 -NewVHDSizeBytes 250GB -SwitchName "VM's - RR"

new-vm : Invalid query
At line:1 char:1
+ new-vm -Name "VM's - RR" -MemoryStartupBytes 45056MB -BootDevice Networ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], VirtualizationException
+ FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.NewVM

When i remove the -SwitchName "VM's - RR" the VM gets created with the network adaptper virtual switch status not connected. So this parameter is not correct. But i dont know why and how to correct this.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BartHofstede-5480 avatar image
0 Votes"
BartHofstede-5480 answered

That's it. The parameter and the spaces aren't working. What is right command if i want to change it after the VM is created?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ElevenYu-MSFT avatar image
0 Votes"
ElevenYu-MSFT answered

Hi,

Do you mean you would like to connect the created VM to a switch by PowerShell Command?

If so, the command can be as below:

Get-VMNetworkAdapter -VMName rr-rds | Connect-VMNetworkAdapter -SwitchName VM's-RR

For your reference:
https://docs.microsoft.com/en-us/powershell/module/hyper-v/connect-vmnetworkadapter?view=windowsserver2019-ps

Thanks,


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.