1 2 Previous Next 16 Replies Latest reply on Jun 12, 2019 7:16 AM by dhanu2k7

    Change network portgroup name for multiple VM

    dhanu2k7 Enthusiast

      Hello

       

      I have something like this , i want to change portgroup for each VM, note that in screenshot every single row has multiple network adapter and portgroups as well, can i have script?

       

        • 1. Re: Change network portgroup name for multiple VM
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          With what is 'network1' corresponding?

          The name of the vMIC?

          That is normally something like 'Network adapter 1'.

           

          Can't you have this as this?

          VM,HostName,NetworkAdapter,Portgroup

          ABC,ABCHOST,network1,i-VLAN1

          ABC,ABCHOST,network2,i-VLAN2

          XYZ,XYZHost,network1,i-VLAN2

          XYZ,XYZHost,network2,i-VLAN3

           

          • 2. Re: Change network portgroup name for multiple VM
            dhanu2k7 Enthusiast

            hi Lucd

             

            Thanks, Yes that name of VMNIC,

             

            regarding the input table, i cannot have like that reason I am creating script to search datastore, register VM with different name and in each VM change network adapter, so this is my requirement, I have import-csv activity in one line so for each server i am doing task, but when VM have multple adapter with multiple portgroup i am stuck. so either I need to proceed above or do you have script to fulfill  my requirement ?

            • 3. Re: Change network portgroup name for multiple VM
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              Not sure how that can be the name of the vNIC.
              Is that the name that is returned when you do

              Get-VM -Name XYZ | Get-NetworkAdapter | Select Name

              • 4. Re: Change network portgroup name for multiple VM
                dhanu2k7 Enthusiast

                it returns to me , sorry for typo

                 

                "Parent","Name","NetworkName"

                "xyz","Network adapter 1","i-VLAN65"

                "xyz","Network adapter 2","i-VLAN65"

                • 5. Re: Change network portgroup name for multiple VM
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  Ok, that seems more realistic.

                   

                  Now for your file, is that a CSV file? Or a TXT file?

                  If it is a CSV, what is returned when you do

                   

                  Import-Csv -Path .\yourfile.csv -UseCulture

                  • 6. Re: Change network portgroup name for multiple VM
                    dhanu2k7 Enthusiast

                    Hi Lucd

                     

                    Sorry for delay response, Here is my script, i left gap for for loop for each NIC, I am blank in changing portgroup, can you suggest ?

                     

                    for below  changing network portgroup, if I have servers.csv then i have to include multiple adapter in one row, or do i need create seperate csv for changing network portgroup

                     

                    $srv = Import-csv -Path servers.csv -Header VMname,Clustername,Datastorename,ESX -UseCulture

                    foreach ($entry in $srv)

                    {

                    $Server = $entry.VMname

                    $Cluster = $entry.Clustername

                    $Datastore = $entry.Datstorename

                    $NetworkAdapter = $entry.NIC

                    $Network = $entry.portgroup

                    $ESXhost = $entry.ESX

                    # Collect .vmx paths of registered VMs on the datastore

                    $registered = @{}

                    Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File  | where {$_.Name -like "*.vmx"} | %{$registered.Add($_.Name,$true)}}

                    # Set up Search for .VMX Files in Datastore

                    New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

                    $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `

                    where {$_.FolderPath -notmatch ".snapshot" -and !$registered.ContainsKey($_.Name)})

                    Remove-PSDrive -Name TgtDS

                    #Register all .vmx Files as VMs on the datastore

                    foreach($VMXFile in $unregistered) {

                    New-VM  -Name $Server_dr -VMFilePath $VMXFIle.DatastoreFullPath -VMHost $ESXHost

                    $NIC = Get-NetworkAdapter -VM $Server_dr

                    foreach($network in $NIC)

                    {

                    # can you correct what I need to put here, if I have same csv servers.csv then i have to include multiple adapter in one row, or do i need create seperate csv for changing network portgroup

                    }

                    }

                    }

                    • 7. Re: Change network portgroup name for multiple VM
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      Sorry, but you keep utterly confusing me.
                      That Import-Csv with the Header parameter, doesn't correspond at all with the sample CSV content you gave earlier.

                      • 8. Re: Change network portgroup name for multiple VM
                        dhanu2k7 Enthusiast

                        Ok sorry....please ignore all.....i have one requirment ...can u provide me script

                         

                        My requirement is search given datastore...

                        Register vm as vm_dr

                        Change the portgroup for the registered vm

                         

                        Can u suggest a good script

                         

                        Regards

                        Dhananjaya

                        • 9. Re: Change network portgroup name for multiple VM
                          LucD Guru
                          vExpertUser ModeratorsCommunity Warriors

                          Try like this

                           

                          $dsName = "MyDS"

                          $pgName = 'MyPG'

                          $registered = @{ }


                          $ds = Get-Datastore -Name $dsName

                          Get-VM -Datastore $ds |

                          ForEach-Object -Process {

                             $_.Extensiondata.LayoutEx.File | where { $_.Name -like "*.vmx" } |

                             ForEach-Object -Process {

                             $registered.Add($_.Name, $true)

                             }

                          }


                          New-PSDrive -Name TgtDS -Location $ds -PSProvider VimDatastore -Root '\' | Out-Null

                          $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `

                             where { $_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.Name) })

                          Remove-PSDrive -Name TgtDS


                          foreach ($VMXFile in $unregistered)

                          {

                             $vm = New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync

                             Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $pgName

                          }

                          • 10. Re: Change network portgroup name for multiple VM
                            dhanu2k7 Enthusiast

                            but my problem is, port group is not same for all 400 servers.

                            • 11. Re: Change network portgroup name for multiple VM
                              LucD Guru
                              vExpertUser ModeratorsCommunity Warriors

                              And where would the script find out which portgroup to use for which VM?

                              • 12. Re: Change network portgroup name for multiple VM
                                dhanu2k7 Enthusiast

                                this one

                                 

                                Get-VM | Get-NetworkAdapter | select Parent,Name,Networkname | export-csv network.csv -NoTypeInformation

                                • 13. Re: Change network portgroup name for multiple VM
                                  LucD Guru
                                  User ModeratorsCommunity WarriorsvExpert

                                  Sorry, but that makes no sense at all.
                                  You are registering VMs, you want to change the portgroup they are connected to, and that info comes from querying the network cards and the portgroup they are currently connected to???

                                   

                                  I give up!

                                  • 14. Re: Change network portgroup name for multiple VM
                                    dhanu2k7 Enthusiast

                                    sorry thats not the command , i forget to tell you that we are changing port group at DR environment, that means if production port group is PG-ABC then DR port group is PG-ABC_DR  so we will create csv file in such way that it should look like below

                                     

                                    1 2 Previous Next