VMware Cloud Community
blamman1
Contributor
Contributor

Import Vm Network adapter port groups from csv.

I am taking my esxi host and disconnecting them from a vcenter 5.5 and importing them to a vcenter 6.5. I am using distributed switch for these vm's. After I add the host into vcenter 6.5 I add it to my imported VDS. The host and vms connect with no issue and I do have the option of migrating the port groups but I have have thousands of vms to migrate. I am not losing network connectivity but I notice the network adapter as blank and does not have the proper network ( port group).

I have the script below that exports all the vm port groups to a csv but I need help with script that will import the csv and change all the vms to their proper portgroups. Some of my vms have multiple nics also.

Pull script

Connect-VIServer -Server "vcenter" -User admin -Password admin

Get-DataCenter BNA | Get-VM | Get-NetworkAdapter | Select-Object @{N="VM";E={$_.Parent.Name}},@{N="NIC";E={$_.Name}},@{N="Network";E={$_.NetworkName}} | Export-csv C:\temp\VMPortGroups.csv

Disconnect-VIServer -Server * -Force

The csv export looks like this

   

VMNICNetwork
BNALAPTL303Network adapter 1

VLAN612_Development

Thanks for any help.

0 Kudos
7 Replies
LucD
Leadership
Leadership

Try like this

foreach($pg in Import-Csv -Path C:\temp\VMPortGroups.csv -UseCulture){

    Get-VM -Name $pg.VM | Get-NetworkAdapter -Name $pg.NIC |

    Set-NetworkAdapter -Portgroup $pg.Network -Confirm:$false

}


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

0 Kudos
blamman1
Contributor
Contributor

Works like a champ. Thankyou for the help

0 Kudos
Savelich1
Contributor
Contributor

Hi, LucD

I have problem with this script, pleas help me

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command

gain.

At E:\Scripts\Import-vmnetwork.ps1:3 char:18

+     Get-VM -Name $pg.VM | Get-NetworkAdapter -Name $pg.NIC |

+                  ~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

0 Kudos
LucD
Leadership
Leadership

Looks like you might have an issue in the CSV file.

Any rows where the VM column is empty, or perhaps an empty line in the CSV?


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

0 Kudos
Savelich1
Contributor
Contributor

There are no empty column or line

It looks like this

 

#TYPE Selected.VMware.VimAutomation.ViCore.Impl.V1.VirtualDevice.NetworkAdapterImpl
VM,"NIC","Network"
mgts-count01-t1,"Network adapter 1","2408_10.226.210.0/24"
Mgts-swnw01,"Network adapter 1","2408_10.226.210.0/24"
MGTS-DWH01T,"Network adapter 1","2700_10.226.150.0/25"

etc

For all of this lines get the error

0 Kudos
LucD
Leadership
Leadership

That looks ok, and works for me.

What does this say?

Import-Csv -Path C:\temp\VMPortGroups.csv -UseCulture


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

0 Kudos
Savelich1
Contributor
Contributor

Finaly, it's working lik this

We were have cisco 1000v and "\" coming with export config from there

foreach($pg in Import-Csv -Path E:\Scripts\VC03\VMPortGroups.csv){

    if($pg.Network -match '/'){

$pgtemp = $pg.Network -replace '/','%2F'

      } else {

$pgtemp = $pg.Network

      }

      Get-VM -Name $pg.VM | Get-NetworkAdapter -Name $pg.NIC | Set-NetworkAdapter -Portgroup $pgtemp -Confirm:$false

}

0 Kudos