VMware Cloud Community
JordonC
Enthusiast
Enthusiast
Jump to solution

Need help modifying a powercli script to use vss

Hello.  I'm in need of someone with a little more powercli knowledge than me.  I have found a great resource online and a shout out to Rafael Moura for the great script.  The script utilizes a csv file to input information such as vm name, IP, port group name, cpu, memory, disk, etc, etc.  I am in need of modifying this script for 2 things.  

1.  To utilize vss (virtual switch) instead of vds (virtual distributed switch)  We have some sites that do not utilize vDs

2.  Utilize RDS Cluster names instead of individual host names for placement of the virtual machines if possible.  

I have attached examples of the csv file and script.    

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You specify the VMhost with the Get-VirtualPortgroup cmdlet

$VMPg = Get-VirtualPortGroup -Name $vm.vNetwork -VMHost $vm.ESXi -Standard

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

1. Assuming the column vNetwork contains the name of the VSS portgroup, you could do

    $VMPg = Get-VirtualPortGroup -Name $vm.vNetwork -Standard

 

2. Use the ResourcePool parameter on the New-VM cmdlet
Assuming there is a column named Cluster, which would replace the ESXi column

    $cluster = Get-Cluster -Name $vm.Cluster
    New-VM -Name $vm.Name -ResourcePool $cluster -Template $vm.Template -OSCustomizationSpec $vm.Specs -confirm:$false -Datastore $vm.Datastore -Location $vm.VMFolder -Notes $vm.Notes | Out-Null

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JordonC
Enthusiast
Enthusiast
Jump to solution

Thanks for the assistance.  Everything almost works.  I get the following error for setting the portgroup for the vm at the end of the script.  

 

Set-NetworkAdapter : Cannot convert 'System.Object[]' to the type 'VMware.VimAutomation.ViCore.Types.V1.Host.Networking.VirtualPortGroupBase' required by parameter 'Portgroup'. Specified method is not supported.
At I:\vmware\VMsBuild1.ps1:87 char:74
+ ... me | Get-NetworkAdapter | Set-NetworkAdapter -Portgroup $VMPg -Confir ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-NetworkAdapter], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter

 

I believe this is because all of our virtual switches have a simple numerical value name of just the VLAN number.  So a virtual switch that is connected to VLAN 30 is named 30.  When I changed the virtual switches network label to VLAN-30 as a test your code works 100%.  Is there any way to change it to accept when only a numerical value is given.  Its looks like its expecting alpha numeric.  

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The error message seems to say that you have portgroups with the same name on different VSS.
To avoid that you would have to include the name of the VSS in your input CSV.
And use the VirtualSwitch parameter with that name on the Get-VirtualPortgroup cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JordonC
Enthusiast
Enthusiast
Jump to solution

Thanks @LucD I think I see the issue but not sure how to solve for.  In the VCenter Data Center there are multiple hosts with a vswitch named vswitch0 and Portgroup named the same.  Simple example would be VMhost01 has vswitch0 PortGroup called 30.  VMhost02 also has a vswitch0 Portgroup called 30.  I think this is where the script is getting hung up.  When I created a unique PortGroup that no other host had the script works.  Any way to add logic to the script to fix?  

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can add the VMHost parameter on the Get-VirtualGroup cmdlet.
I assume you don't have duplicate portgroup names on different VSS on a single ESXi node.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JordonC
Enthusiast
Enthusiast
Jump to solution

Thanks @LucD 

Appreciate the assistance.  You assume correct no duplicate portgroup names on a single host. 

Where would I place your suggestion in the CLI setting the network adapter below?  Am I inserting a Get-Vmhost $vm.ESXi ?

Get-VM $vm.Name | Get-NetworkAdapter | Set-NetworkAdapter -Portgroup $VMPg -Confirm:$false | Out-Null
Get-VM $vm.Name | Get-NetworkAdapter -Name “Network adapter 1” | Set-NetworkAdapter -StartConnected:$true -Confirm:$false | Out-Null
Get-VM $vm.Name | Start-VM | Out-Null

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You specify the VMhost with the Get-VirtualPortgroup cmdlet

$VMPg = Get-VirtualPortGroup -Name $vm.vNetwork -VMHost $vm.ESXi -Standard

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JordonC
Enthusiast
Enthusiast
Jump to solution

Thanks @LucD 

Was just responding to let you know I found where to place it and your suggestion of adding -VMhost worked perfectly.  Thanks for all the help. 

0 Kudos