1 2 Previous Next 18 Replies Latest reply on Dec 5, 2017 10:53 AM by ngillettspio

    Assigning Multiple vlans when deploying Powercli Script

    ngillettspio Novice

      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

      }

        • 1. Re: Assigning Multiple vlans when deploying Powercli Script
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          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?

          • 2. Re: Assigning Multiple vlans when deploying Powercli Script
            ngillettspio Novice

            # 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?

            • 3. Re: Assigning Multiple vlans when deploying Powercli Script
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

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

              • 5. Re: Assigning Multiple vlans when deploying Powercli Script
                LucD Guru
                vExpertUser ModeratorsCommunity Warriors

                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

                }

                 

                • 6. Re: Assigning Multiple vlans when deploying Powercli Script
                  ngillettspio Novice

                  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

                  • 7. Re: Assigning Multiple vlans when deploying Powercli Script
                    LucD Guru
                    Community WarriorsUser ModeratorsvExpert

                    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?

                    • 8. Re: Assigning Multiple vlans when deploying Powercli Script
                      ngillettspio Novice

                      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

                       

                       

                      }

                      • 9. Re: Assigning Multiple vlans when deploying Powercli Script
                        LucD Guru
                        Community WarriorsUser ModeratorsvExpert

                        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

                        }

                        • 10. Re: Assigning Multiple vlans when deploying Powercli Script
                          ngillettspio Novice

                          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.

                          • 11. Re: Assigning Multiple vlans when deploying Powercli Script
                            LucD Guru
                            vExpertUser ModeratorsCommunity Warriors

                            Which PowerCLI version are you using?

                            Can you do a Get-PowerCLIVersion?

                            • 12. Re: Assigning Multiple vlans when deploying Powercli Script
                              ngillettspio Novice

                              6.5 Release 1 build 4624819

                              • 13. Re: Assigning Multiple vlans when deploying Powercli Script
                                LucD Guru
                                Community WarriorsUser ModeratorsvExpert

                                Can you try changing the parameter from VMName to VM?

                                • 14. Re: Assigning Multiple vlans when deploying Powercli Script
                                  ngillettspio Novice

                                  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

                                  1 2 Previous Next