VMware Cloud Community
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

Change network portgroup name for multiple VM

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?

pastedImage_1.png

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

finally i corrected my self thanks Lucd

View solution in original post

17 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

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 ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

it returns to me , sorry for typo

"Parent","Name","NetworkName"

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

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

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

}

}

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

}


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

dhanu2k7
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

this one

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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!


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

dhanu2k7
Enthusiast
Enthusiast
Jump to solution

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

pastedImage_0.png

dhanu2k7
Enthusiast
Enthusiast
Jump to solution

I have created script now but i get only one error for alternative guest name  can you help now

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

foreach ($entry in $srv)

{

$Server = $entry.VMname

$Cluster = $entry.Clustername

$DSname = "SDN_INTRA_VOL6"

$NetworkAdapter = $entry.NIC

#$Network = $entry.portgroup

$ESXhost = $entry.ESX

# Collect .vmx paths of registered VMs on the datastore

$registered = @{}

$Datastore = Get-Datastore -Name $DSname

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 $_.Name -like "XYZ.vmx" -and !$registered.ContainsKey($_.Name)})

Remove-PSDrive -Name TgtDS

#Register all .vmx Files as VMs on the datastore

foreach($VMXFile in $unregistered) {

$vm = New-VM -AlternateGuestName "$ServerDR" -VMFilePath $VMXFIle.DatastoreFullPath -VMHost $ESXHost

#$NIC = Get-NetworkAdapter -VM $vm

cmd.exe /c  findstr /i $vm network.csv  >new.csv

$nw = import-csv -path new.csv -Header VMname,Name,portgroup -UseCulture

foreach($network in $nw)

{

Get-VM -VM $network.VMname |Get-NetworkAdapter |Where {$_.Name -eq $network.Name } | Set-NetworkAdapter -NetworkName $network.portgroup

}

}

}

errorr is

New-VM : Parameter set cannot be resolved using the specified named parameters.

DR-Register.ps1:21 char:7

+ $vm = New-VM -AlternateGuestName "$Server" -VMFilePath $VMXFIle.Datas ...

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

    + CategoryInfo          : InvalidArgument: (:) [New-VM], ParameterBindingException

    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

0 Kudos
dhanu2k7
Enthusiast
Enthusiast
Jump to solution

finally i corrected my self thanks Lucd

Loagu
Contributor
Contributor
Jump to solution

HI 

Can you please share me the script and template for that input file as well as i need in same scenario where i need to do this kind of work in DR environment

0 Kudos