VMware Cloud Community
arvindathere
Enthusiast
Enthusiast
Jump to solution

Network Adapter information using Invoke-VmScript

Hi LucD​ ,

I was using the Get-vmguestnetworkinterface API to fetch the network interface information. like below

$interfaces = Get-VMGuestNetworkInterface -VM "VMName" -GuestUser "Administrator" -GuestPassword "XXXXX" -ErrorAction Stop

PowerCLI C:\>

PowerCLI C:\> $interfaces[0].NetworkAdapter.Name

Network Adapter 2

Now using the Invoke-VM Script am fetching the same n.w interface information through ipconfig /all command.

However the output of this ipconfig doesnt list the network adapter details.

This n.w adapter information is needed to set the network interface for me. Basically i need to know the mapping of interface to n.w adapter.

Any methods to do this ?

Regards,

Arvind S.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think I got what you are looking for.

The following will allow you to configure the network per adapter inside the guest OS, based on the vNIC adapter name.

Just define the settings in the hash table at the beginning, and you can use as many network adapters as you want.

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$ipConfig = @(

    @{

        Interface = 'Network adapter 1'

        MAC = '00:0c:29:fa:f6:f0'

        IP = '192.168.1.81'

        Mask = '255.255.255.0'

        Gateway = '192.168.1.254'

    },

    @{

        Interface = 'Network adapter 2'

        IP = '192.168.1.181'

        Mask = '255.255.255.0'

        Gateway = '192.168.1.254'

    }

)

$code = @"

`$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "MACAddress = '$($vnic.MacAddress)'"

`$wmi.EnableStatic("$($nic.IP)", "$($nic.Mask)")

`$wmi.SetGateways("$($nic.Gateway)", 1)

"@

foreach($nic in $ipConfig){

    $vnic = Get-NetworkAdapter -VM $vm -Name $nic.Interface 

    if($vnic){

        Invoke-VMScript -VM $vm -ScriptText $code -ScriptType Powershell

    }

    else{

        Write-Error "Network adapter $($nic.Interface) not found inside guest OS"

    }

}


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

View solution in original post

10 Replies
LucD
Leadership
Leadership
Jump to solution

You should be able to acquire that info with the help of Invoke-VMScript and the netsh command.

Do these return the info you are looking for?

netsh interface ipv4 show interfaces

netsh interface ipv4 show config


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

Reply
0 Kudos
PabloJAguilar20
Enthusiast
Enthusiast
Jump to solution

May be this scripts help you

$propc= @()

$Resultsstore = @()

$vmx=read-host "INPUT VM NAME"

$vm= get-vm $vmx

$vmnic= $vm | get-networkadapter

$ip=$VM.GUEST.IPADDRESS | select-object -index 0

$mac=$vmnic.MACADDRESS

$vmnicname=$VMNIC.NAME

$type= $VMNIC.TYPE

$networkadapter=$VMNIC.NETWORKNAME

$conect=$vmnic.ExtensionData.Connectable.Connected

$propc=@{

        NAME=$VM.NAME

        IP= $ip

        MACADDRESS= $mac

        VMNIC= $vmnicname

        TYPE= $type

        NETWORKNAME=$networkadapter

        CONNECTABLE= $conect

        }

$Resultsstore += New-Object PSObject -Property $propc

$Resultsstore | Export-Csv nics.csv -NoTypeInformation

write-host $resultsstore

regards

Pablo J Aguilar Consultor de Infraestructuras Virtuales. Buenos Aires - Argentina
Reply
0 Kudos
IT_pilot
Expert
Expert
Jump to solution

$GuestCredential = Get-Credential

$ScriptText = 'Get-NetIPConfiguration -InterfaceAlias "Ethernet0"'

     Ethernet0 - Required interface number

Invoke-VMScript -ScriptText $ScriptText -VM TestVM -GuestCredential $GuestCredential 

     TestVM - Name VM.

http://it-pilot.ru
Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

PilotXP_11

When I run this I get "Not recognized as internal or external command"

Get-NetIPConfiguration -InterfaceAlias "Ethernet0"

Regards,

Arvind S

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

No LucD​ . The commands you have given returns only the interface name ..

Whereas I need the  corresponding network adaptor name.

Regards,

Arvind S.

Reply
0 Kudos
IT_pilot
Expert
Expert
Jump to solution

This cmdlet does not run on the command line, but is passed as a script element.

Look at the screenshots of the execution in my Russian blog

PowerCLI. Invoke-VMScript. Установка IP-адреса гостя (static, DHCP).

http://it-pilot.ru
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What do you mean with 'network adapter name'?

The name of the vNIC, like 'Network adapter 1'.


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think I got what you are looking for.

The following will allow you to configure the network per adapter inside the guest OS, based on the vNIC adapter name.

Just define the settings in the hash table at the beginning, and you can use as many network adapters as you want.

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$ipConfig = @(

    @{

        Interface = 'Network adapter 1'

        MAC = '00:0c:29:fa:f6:f0'

        IP = '192.168.1.81'

        Mask = '255.255.255.0'

        Gateway = '192.168.1.254'

    },

    @{

        Interface = 'Network adapter 2'

        IP = '192.168.1.181'

        Mask = '255.255.255.0'

        Gateway = '192.168.1.254'

    }

)

$code = @"

`$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "MACAddress = '$($vnic.MacAddress)'"

`$wmi.EnableStatic("$($nic.IP)", "$($nic.Mask)")

`$wmi.SetGateways("$($nic.Gateway)", 1)

"@

foreach($nic in $ipConfig){

    $vnic = Get-NetworkAdapter -VM $vm -Name $nic.Interface 

    if($vnic){

        Invoke-VMScript -VM $vm -ScriptText $code -ScriptType Powershell

    }

    else{

        Write-Error "Network adapter $($nic.Interface) not found inside guest OS"

    }

}


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

IT_pilot
Expert
Expert
Jump to solution

LucD I probably did not say specifically.

Network adapter 1 - this is the interface name when running the ipconfig command.

The script uses an alias, i.e. interface name in Control Panel > Network and Internet > Network Connections.

I use the alias for setting the network settings

IP address:

$ScriptText = 'New-NetIPAddress -InterfaceAlias "Ethernet0" -AddressFamily Ipv4 -IPAddress 10.10.10.210 -PrefixLength 24 -DefaultGateway 10.10.10.1'

DNS:

$ScriptText = 'Set-DnsClientServerAddress -InterfaceAlias "Ethernet0" -ServerAddresses 10.10.10.20'

Script for IP address, DNS, verification of the installed configuration:

$ScriptText = 'New-NetIPAddress -InterfaceAlias "Ethernet0" -AddressFamily Ipv4 -IPAddress 10.10.10.210 -PrefixLength 24 -DefaultGateway 10.10.10.1 | Out-Null;Set-DnsClientServerAddress -InterfaceAlias "Ethernet0" -ServerAddresses 10.10.10.20 | Out-Null;Start-Sleep -Seconds 5;Get-NetIPConfiguration -InterfaceAlias "Ethernet0"'

It is run by the cmdlet

Invoke-VMScript -ScriptText $ScriptText -VM TestVM -GuestCredential $GuestCredential 

About the preliminary input of credentials for logging into the OS, I already wrote above ($GuestCredential = Get-Credentia

http://it-pilot.ru
Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast
Jump to solution

Thanks a lot LucD​ . This approach helped.

Regards,

Arvind S

Reply
0 Kudos