Automation

 View Only
Expand all | Collapse all

Change network portgroup name for multiple VM

  • 1.  Change network portgroup name for multiple VM

    Posted Jun 11, 2019 11:06 AM

    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?



  • 2.  RE: Change network portgroup name for multiple VM

    Posted Jun 11, 2019 11:36 AM

    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



  • 3.  RE: Change network portgroup name for multiple VM

    Posted Jun 11, 2019 01:58 PM

    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 ?



  • 4.  RE: Change network portgroup name for multiple VM

    Posted Jun 11, 2019 02:28 PM

    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



  • 5.  RE: Change network portgroup name for multiple VM

    Posted Jun 11, 2019 02:59 PM

    it returns to me , sorry for typo

    "Parent","Name","NetworkName"

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

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



  • 6.  RE: Change network portgroup name for multiple VM

    Posted Jun 11, 2019 03:07 PM

    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



  • 7.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 06:06 AM

    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

    }

    }

    }



  • 8.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 06:40 AM

    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.



  • 9.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 10:15 AM

    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



  • 10.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 10:23 AM

    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

    }



  • 11.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 10:55 AM

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



  • 12.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 11:20 AM

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



  • 13.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 11:28 AM

    this one

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



  • 14.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 11:31 AM

    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!



  • 15.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 11:37 AM

    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



  • 16.  RE: Change network portgroup name for multiple VM

    Posted Jun 12, 2019 12:37 PM

    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



  • 17.  RE: Change network portgroup name for multiple VM
    Best Answer

    Posted Jun 12, 2019 02:17 PM

    finally i corrected my self thanks Lucd



  • 18.  RE: Change network portgroup name for multiple VM

    Posted Jun 03, 2021 05:09 PM

    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