VMware Cloud Community
ngillettspio
Contributor
Contributor
Jump to solution

Assigning Multiple vlans when deploying Powercli Script

I have been assigning vLANs (the same network adapter) when I deploy multiple vms and it has worked sucessfully.. My issue now is I have been given vms to deploy with the same specs but different vlans. Is there a way via powercli or csv to have these 10+ vlans assigned via script when deploying my vms? Below is what i've currently been using.. Any help would be greatly appreciated.

       

Get-VM $VM_Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName Test_0207 -Confirm:$false

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.

This uses a variable to get each row in the CSV, that way we avoid using the $_ pipeline object in the Set-NetworkAdapter as well.

foreach($row in (Import-Csv "C:\Users\otcgill\Desktop\Book3.csv" -UseCulture)){

  Get-NetworkAdapter -VMName $row.Vmname | Set-NetworkAdapter -NetworkName $row.NetworkName -Confirm:$false

}


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

View solution in original post

Reply
0 Kudos
18 Replies
LucD
Leadership
Leadership
Jump to solution

You assign a Portgroup through the NetworkName parameter, and a Portgroup on a VSS can have only 1 VLAN assigned.

Unless you are using distributed portgroups with Trunking enabled.

Or did I not get the question correctly?


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

# Create all the VMs specified in $newVmList

$newVmList = "testvm1","testvm2","testvm3"

#

# Specify the VM you want to clone

$mySourceTemplate = Get-Template -Name "win2k12R2stdScanned_enhanced_template"

#

# Specify the Customization Specification to use

$customspecification= "Windows_2012_std_R2_template"

#

# Specify the datastore or datastore cluster placement

$ds = "PROD_1"

#

$vmhost = Get-VMHost -Name testvhost1234

#

# Specify vCenter Server Virtual Machine & Templates folder

$Folder = "Templates"

#

# Specify the vSphere Cluster

$Cluster = "Mycluster"

#

# End of user input parameters

#_______________________________________________________

#

$newVmList | ForEach-Object {

      $VM_name= $_

      $ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random

      write-host "Creation of VM $VM_name initiated” -foreground green

      New-VM -Name $VM_Name -Template $mySourceTemplate -VMHost $ESXi -Datastore $ds -Location $Folder -OSCustomizationSpec $customspecification

      Start-VM $VM_Name

      Start-Sleep 600   # seconds

        Get-VM $VM_Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName testvlan1 -Confirm:$false

How would I assign testvlan2, testvlans3 etc when running this script?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just to make sure, you want to attach testvlan1 to VM testvm1. testvlan2 to VM testvm2 and testvlan3 to Vm testvm3?


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

correct!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

One way of doing this could be

# Create all the VMs specified in $newVmList

$newVmList = "testvm1","testvm2","testvm3"

#

# Specify the VM you want to clone

$mySourceTemplate = Get-Template -Name "win2k12R2stdScanned_enhanced_template"

#

# Specify the Customization Specification to use

$customspecification= "Windows_2012_std_R2_template"

#

# Specify the datastore or datastore cluster placement

$ds = "PROD_1"

#

$vmhost = Get-VMHost -Name hubdellvhost-01.njes.state.nj.us

#

# Specify vCenter Server Virtual Machine & Templates folder

$Folder = "Templates"

#

# Specify the vSphere Cluster

$Cluster = "Mycluster"

#

# End of user input parameters

#_______________________________________________________

#

for($i=1; $i -le 3; $i++){

      $VM_name= $newVmList[$i - 1]

      $ESXi=Get-Cluster $Cluster | Get-VMHost -state connected | Get-Random

      write-host "Creation of VM $VM_name initiated” -foreground green

      New-VM -Name $VM_Name -Template $mySourceTemplate -VMHost $ESXi -Datastore $ds -Location $Folder -OSCustomizationSpec $customspecification

      Start-VM $VM_Name

      Start-Sleep 600   # seconds

      Get-VM $VM_Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName "testvlan$($i)" -Confirm:$false

}


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

LucD,

I'm a bit confused and don't understand how

for($i=1; $i -le 3; $i++){

      $VM_name= $newVmList[$i - 1]  calls the for vms. I'm not a scripting pro but was hoping there was another way to do this?  What if i had names VMs and numbered vlans that varied 3643, 5623 etc. Appreciate you for helping me figure this out in advance :-). I'm okay with doing this after a deployment if this would make things easier as far as the script is concerned.

Set-NetworkAdapter : 11/28/2017 1:19:50 PM    Set-NetworkAdapter        Object reference not set to an instance of an object.

At line:8 char:46

+       Get-VM $VM_Name | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName "te ...

+                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Set-NetworkAdapter], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Like I said, that is only one way of doing this.

In case the Vlans do not have 1,2,3..., you could store the Vlans also in an array, and then you could use the same array index $i.

Another way is to store all that info in a CSV file.

Something with this layout for example:

VMName,NetworkName

testvm1,vlan123

testvm2,vlan456

It all depends on what data you have available.

On the network adapter error, could it be that the VM has no network adapter yet?


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

Lucd, Thanks for your fast response. I like the idea of using the csv file. Here is what I have but I'm getting an error message. could I include network name and VMName on the third line? The first 3 lines have worked with no issues. I guess I just need to figure out how to add NetworkName VMName correctly.

fields  Server Name ESX HOST Datastore Template NumCPU MemoryMB NetworkName VMName

Import-Csv "C:\Users\otcgill\Desktop\book2.csv" -UseCulture | %{

  New-vm -Name $_."Server Name" -VMhost $_."ESX Host" -Template $_.Template -Datastore $_.Datastore  -OSCustomizationspec $_.Customization

  set-vm -vm $_."Server Name" -NumCpu $_.NumCpu -MemoryMB $_.MemoryMB -Confirm:$false

  set-vm -vm -NetworkName -Confirm:$false

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to provide the object returned by Get-NetworkAdapter as input to the Set-NetworkAdapter cmdlet.

Something like this (but you have to make sure that Get-NetworkAdapater returns an object, in other words that there is a vNIC on the VM)

Import-Csv "C:\Users\otcgill\Desktop\book2.csv" -UseCulture | %{

  $vm = New-vm -Name $_."Server Name" -VMhost $_."ESX Host" -Template $_.Template -Datastore $_.Datastore  -OSCustomizationspec $_.Customization

  Set-VM -VM $vm -NumCpu $_.NumCpu -MemoryMB $_.MemoryMB -Confirm:$false

 

  Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $_.NetworkName -Confirm:$false

}


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

Hi LucD,

I have been testing this for several days and still haven't had a luck and keep getting error messages. I tried to keep it simple and add just 2 columns to my csv file VMName  NetworkName and it returns

Import-Csv "C:\Users\otcgill\Desktop\Book3.csv" -UseCulture | %{

  Get-NetworkAdapter -VMName $vm | Set-NetworkAdapter -NetworkName $_.NetworkName -Confirm:$false

}

Get-NetworkAdapter : A parameter cannot be found that matches parameter name 'VMName'.

At line:2 char:22

+   Get-NetworkAdapter -VMName $vm | Set-NetworkAdapter -NetworkName $_.NetworkNam ...

+                      ~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-NetworkAdapter], ParameterBindingException

    + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetNetworkAdapter

get-vm "testvm1" | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName 526_VLAN Confirm:$false works for me but I figured I should be able to use this from csv to do this in bulk as you mentioned.  Sorry for the last response on this thread.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using?

Can you do a Get-PowerCLIVersion?


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

6.5 Release 1 build 4624819

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try changing the parameter from VMName to VM?


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

Do you think it may be the version?

Get-NetworkAdapter : Cannot bind parameter 'VM'. Cannot convert the "VMware.Vim.VirtualMachine" value of type "VMware.Vim.VirtualMachine" to type

"VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine".

At line:2 char:26

+   Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $_.NetworkName -C ...

+                          ~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-NetworkAdapter], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.GetNetworkAdapter

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, the version is ok.

Can you attach the script again, as you are currently running it?


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

Import-Csv "C:\Users\otcgill\Desktop\Book3.csv" -UseCulture | %{

  Get-NetworkAdapter -VMName $vm | Set-NetworkAdapter -NetworkName $_.NetworkName -Confirm:$false

}

I also tried changing the parameter from VMName to VM as you mentioned..

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

This uses a variable to get each row in the CSV, that way we avoid using the $_ pipeline object in the Set-NetworkAdapter as well.

foreach($row in (Import-Csv "C:\Users\otcgill\Desktop\Book3.csv" -UseCulture)){

  Get-NetworkAdapter -VMName $row.Vmname | Set-NetworkAdapter -NetworkName $row.NetworkName -Confirm:$false

}


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

Reply
0 Kudos
ngillettspio
Contributor
Contributor
Jump to solution

LucD,

Thanks so much!!! You saved me so much time 🙂

Reply
0 Kudos