VMware Cloud Community
arbelac
Contributor
Contributor

Rename Adapter Name & IP Assign for multiple vm adapter via Invoke-VMScript

Hi,

I have been deploying VM from Template. After created VM, I am adding two network adapter which is relationship with different port group via PowerCLI.

Now , I want to rename these interfaces as LAN&BACKUP via Invoke-VMScript. Now , I've been searching all over for a way I could match network adapter inside windows to vmware's network adapter.

After  , I will assign ip address to both these interface  via   Invoke-VMScript. How can I do that ?

LAN : ipaddress : 10.10.10.15  subnet : 255.255.255.0  gateway :10.10.10.1   DNS1 : 172.12.12.10  DNS2 : 172.12.12.11  METRIC :5

BACKUP  :  ipaddress : 192.168.120.15  subnet : 255.255.255.0   UNTICK Register this connection's address in DNS   ,METRIC : 10

And  route add xx.xx.xx.xx mask 255.255.255.0 xx.xx.xx.xx -p

script part :

LAN  ->> New-NetworkAdapter -VM $vm -Type Vmxnet3 -NetworkName $_.NetworkName -WakeOnLan:$true -StartConnected:$true -Confirm:$false

  if($_."Second Network Adapter" -eq "Y"){

   $BackupNetworkName = "VLAN1000"

   New-NetworkAdapter -VM $vm -Type Vmxnet3 -NetworkName $BackupNetworkName -WakeOnLan:$true -StartConnected:$true -Confirm:$false

   }

   else{

    #do nothing

   }

0 Kudos
10 Replies
LucD
Leadership
Leadership

Can't you use the MAC address to match the vNIC to the NIC inside the guest OS?


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

0 Kudos
arbelac
Contributor
Contributor

Hi Lucd,

I tried a lot of stuff. but no luck. I am very confused. could you please help me ?

So , I want to do something like below.  if CSV Second Network Adapter field equal "Y" , firstly , I will add network adapters then to match the vNIC to the NIC inside the guest OS. And

I will assign ip address related to the adapters.

New-NetworkAdapter -VM $vm -Type Vmxnet3 -NetworkName $_.NetworkName -StartConnected:$true -Confirm:$false

  if($_."Second Network Adapter" -eq "Y"){

   $BackupNetworkName = "VLAN1000"

   New-NetworkAdapter -VM $vm -Type Vmxnet3 -NetworkName $BackupNetworkName  -StartConnected:$true -Confirm:$false

   }

   else{

    #do nothing

   }

Then ,

$nwset= @"

Rename-NetAdapter -Name "<Old NIC Adapter Name>" -NewName "LAN"

## Configure the IP address and gateway ##

    New-NetIpAddress -InterfaceAlias LAN -IPAddress $_.LANIP -PrefixLength 24 -DefaultGateway $_.LANGW

## Configure the DNS Server IP address ##

Set-DnsClientServerAddress -InterfaceAlias LAN -ServerAddresses $dns1,$dns2

Set-NetIPInterface -InterfaceAlias LAN -automaticmetric disabled -InterfaceMetric 5

#BACKUP Interface

if ($_."Second Network Adapter" -eq "Y"){

Rename-NetAdapter -Name "<Old NIC Adapter Name>" -NewName "BACKUP"

New-NetIpAddress -InterfaceAlias BACKUP -IPAddress $_.BACKUPIP -PrefixLength 22

Set-NetIPInterface -InterfaceAlias BACKUP -automaticmetric disabled -InterfaceMetric 10

Get-NetAdapter BACKUP | Set-DNSClient –RegisterThisConnectionsAddress $False

}

"@

Invoke-VMScript -VM $vm-ScriptText $nwset  -Verbose -GuestCredential $VMLocalCredential

thanks,

0 Kudos
LucD
Leadership
Leadership

Can you try this snippet?
It will, if it doesn't exist already, add a 2nd vNIC to the VM.

Then it will use Invoke-VMScript to rename the network adapters inside the Guest OS.

There might be an issue when the new vNIC is not immediately picked up by the Guest OS.

There is, afaik, no working solution to force a hard rescan inside a Windows OS, except for installing and running the devcon tool.

When the code below works as desired, you can add the IP and other network settings for the 2nd NIC in the same script in the correct switch case.

$vmName = 'MyVM'

$newPG = 'PGName'


$nic1Name = 'LAN'

$nic2Name = 'Backup'


$user = 'domain/user'

$pswd = 'VMware1!'

$cred =  New-Object -TypeName PSCredential -ArgumentList $user, (ConvertTo-SecureString -String $pswd -AsPlainText -Force)


$code = @'

foreach(`$nic in Get-NetAdapter){

    switch(`$nic.MacAddress){

        '$($nic1.MacAddress.Replace(':','-'))' {

            Rename-NetAdapter -InputObject `$nic -NewName '$nic1Name'

        }

        '$($nic2.MacAddress.Replace(':','-'))' {

            Rename-NetAdapter -InputObject `$nic -NewName '$nic2Name'

        }

        Default {

            Write-Error 'Unknown MAC address found'

        }

    }

}


Get-NetAdapter | Select Name,MacAddress

'@


$vm = Get-VM -Name $vmName

$nic1 = Get-NetworkAdapter -VM $vm -Name 'Network adapter 1'

try{

    $nic2 = Get-NetworkAdapter -VM $vm -Name 'Network adapter 2' -ErrorAction Stop

}

catch{

    $nic2 = New-NetworkAdapter -VM $vm -NetworkName $newPg -StartConnected:$true -Confirm:$false

}


$sInvoke = @{

    VM = $vm

    ScriptType = 'PowerShell'

    ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)

    GuestCredential = $cred

}

Invoke-VMScript @sInvoke


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

0 Kudos
arbelac
Contributor
Contributor

Hi Lucd,

I assuming , after Vm poweron, you are adding a 2nd vNIC to the VM. Also , I have a csv file which is BackupINT field. Now , if I set as "yes" this field , it will add 2nd vNIC to the VM. My question is :  how can I integrate if/else statement ? you are saying that " no working solution to force a hard rescan inside a Windows OS".  if I add  "start-sleep"  after adding vNIC ? will it work ?

thanks,

0 Kudos
LucD
Leadership
Leadership

Yes, the 2nd vNIC is added to the VM in the try-catch construct.

And yes, the VM needs to be powered on, at least at the moment the Invoke-VMScript is called.


The snippet of code is to test the validity for renaming the NIC inside the Guest OS.

It has no integration whatsoever with your CSV.

Correct, afaik, besides using devcon, there is no method that works, except for restarting the Guest OS.

Not sure if the HW scan is done on a timed interval, and if in that case a sleep would be of any use.


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

0 Kudos
arbelac
Contributor
Contributor

Now , it works your script. Now , how can I add if statement within script ?

my script :

Rename-NetAdapter -Name "<Old NIC Adapter Name>" -NewName "LAN"

## Configure the IP address and gateway ##

    New-NetIpAddress -InterfaceAlias LAN -IPAddress $_.LANIP -PrefixLength 24 -DefaultGateway $_.LANGW

## Configure the DNS Server IP address ##

Set-DnsClientServerAddress -InterfaceAlias LAN -ServerAddresses $dns1,$dns2

Set-NetIPInterface -InterfaceAlias LAN -automaticmetric disabled -InterfaceMetric 5

#BACKUP Interface

if ($_."Second Network Adapter" -eq "Y"){

Rename-NetAdapter -Name "<Old NIC Adapter Name>" -NewName "BACKUP"

New-NetIpAddress -InterfaceAlias BACKUP -IPAddress $_.BACKUPIP -PrefixLength 22

Set-NetIPInterface -InterfaceAlias BACKUP -automaticmetric disabled -InterfaceMetric 10

Get-NetAdapter BACKUP | Set-DNSClient –RegisterThisConnectionsAddress $False

0 Kudos
LucD
Leadership
Leadership

You could incorporate your test and code something like the below.

Note that I use the PipelineVariable on the Import-Csv cmdlet, this allows me to $row to address the line in the CSV file.

Also note that since I don't know your complete New-VM cmdlet, you will have to add whatever parameters you are using on there.
Further I don't know how you populate some of the variables like $dns1 and $dns2, you might have to add/adapt that somewhere in the code.

Same goes for the Guest OS user and password.

$nic1Name = 'LAN'

$nic2Name = 'Backup'


$user = 'domain/user'

$pswd = 'VMware1!'

$cred =  New-Object -TypeName PSCredential -ArgumentList $user, (ConvertTo-SecureString -String $pswd -AsPlainText -Force)


$code1 = @'

foreach(`$nic in Get-NetAdapter){

    switch(`$nic.MacAddress){

        '$($nic1.MacAddress.Replace(':','-'))' {

            Rename-NetAdapter -InputObject `$nic -NewName '$nic1Name'

            New-NetIpAddress -InterfaceAlias '$nic1Name' -IPAddress '$($row.LANIP)' -PrefixLength 24 -DefaultGateway '$($row.LANGW)'

            Set-DnsClientServerAddress -InterfaceAlias 'LAN' -ServerAddresses $dns1,$dns2

        }

        '$($nic2.MacAddress.Replace(':','-'))' {

            Rename-NetAdapter -InputObject `$nic -NewName '$nic2Name'

            New-NetIpAddress -InterfaceAlias '$nic2Name' -IPAddress '$($row.BACKUPIP)' -PrefixLength 22

            Set-NetIPInterface -InterfaceAlias '$nic2Name' -AutomaticMetric disabled -InterfaceMetric 10

            Get-NetAdapter -Name '$nic2Name' | Set-DNSClient –RegisterThisConnectionsAddress `$False

       }

        Default {

            Write-Error 'Unknown MAC address found'

        }

    }

}


Get-NetAdapter | Select Name,MacAddress

'@


Import-Csv -Path .\vm.csv -UseCulture -PipelineVariable row |

ForEach-Object -Process {


    # Create the VM, store result in $vm

    $vm = New-VM -Name $row.Name .... # Add all other parameters for the New-VM


    $nic1 = Get-NetworkAdapter -VM $vm -Name 'Network adapter 1'


    if($row.'Second Network Adapter' -eq 'Y'){

        try{

            $nic2 = Get-NetworkAdapter -VM $vm -Name 'Network adapter 2' -ErrorAction Stop

        }

        catch{

            $nic2 = New-NetworkAdapter -VM $vm -NetworkName $newPg -StartConnected:$true -Confirm:$false

        }

    }

    else{

        $nic2 = @{

            MacAddress = '12:34:56:78:90:12'

        }

    }

    $sInvoke = @{

        VM = $vm

        ScriptType = 'PowerShell'

        ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)

        GuestCredential = $cred

    }

    Invoke-VMScript @sInvoke

}


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

0 Kudos
arbelac
Contributor
Contributor

Hi Lucd,

thanks for your valuable help. BTW , I have tried already your script on poweron VM. Now , I have been developing VM deploy script from template from stratch. I will open new thread for this.  I need your advice.

0 Kudos
LucD
Leadership
Leadership

Did it work?


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

0 Kudos
arbelac
Contributor
Contributor

yes it works.  I have done partial test. no issue.

0 Kudos