PowerCLI

 View Only
Expand all | Collapse all

Network Adapter information using Invoke-VmScript

  • 1.  Network Adapter information using Invoke-VmScript

    Posted Feb 16, 2018 02:21 PM

    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.



  • 2.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 16, 2018 02:29 PM

    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



  • 3.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 18, 2018 06:15 PM

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

    Whereas I need the  corresponding network adaptor name.

    Regards,

    Arvind S.



  • 4.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 18, 2018 07:00 PM

    What do you mean with 'network adapter name'?

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



  • 5.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 19, 2018 08:11 AM

    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



  • 6.  RE: Network Adapter information using Invoke-VmScript
    Best Answer

    Posted Feb 18, 2018 07:47 PM

    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"

        }

    }



  • 7.  RE: Network Adapter information using Invoke-VmScript

    Posted Mar 01, 2018 11:47 AM

    Thanks a lot LucD​ . This approach helped.

    Regards,

    Arvind S



  • 8.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 16, 2018 04:29 PM

    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



  • 9.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 16, 2018 07:42 PM

    $GuestCredential = Get-Credential

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

         Ethernet0 - Required interface number

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

         TestVM - Name VM.



  • 10.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 18, 2018 06:14 PM

    PilotXP_11

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

    Get-NetIPConfiguration -InterfaceAlias "Ethernet0"

    Regards,

    Arvind S



  • 11.  RE: Network Adapter information using Invoke-VmScript

    Posted Feb 18, 2018 06:35 PM

    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).