locked
Change virtual network adapters name using it macaddress. RRS feed

  • Question

  • Hello.

    I have a bunch of virtualmachines in my lab. Im learning how to cluster/manage/conect them.

    I have lot of problems identifiying the network adapters that belongs to each network.

    I have a cvs file where I wrote all the info regarding to the adapters. is something like:

    """Computername""","""NIC""","""MAC""",DHCP,AddressFamily,IPAddress,PrefixLength,Type,DefaultGateway,DnsServerAddresses
    VM-iSCSI,VnMGNT,00:18:59:00:01:02,true,IPv4,,,Unicast,,
    VM-iSCSI,VniSCSI,00:18:59:00:01:04,false,IPv4,172.16.150.10,24,Unicast,,

    I where NIC is the desired name of the adapter. I was tried some mix of scripts I  but none runs properly.

    The comand I,m using now and runs properly is:

        Rename-NetAdapter -Name * -NewName VnMGT | Where-Object {$_.MacAddress -match "00-18-59-00-01-02"}

    This has a lot of interactions to do, sinca I have to write down the name and the MAC.

    I was wondering to know if is posible to do it comparing the MAC from the adapter with the ones in the list and do the job.

    I was looking on to some post related to this but the scipts are so dificult for me to understant. I spended more tham 3 days to arrive to the comand posted a few lines before.

    This are some of the posts I was reading.

    https://social.technet.microsoft.com/Forums/en-US/a62b8a8c-ca02-4c76-a2b0-fb3f10deb1b5/change-dynamic-ip-to-static-ip-on-remote-machines-through-powershell?forum=winserverpowershell

    https://social.technet.microsoft.com/Forums/en-US/c26ca053-942e-4f31-aa4e-5e68b4d2cad0/compare-computer-macaddress-from-csv-file-to-real-computer-macaddress-and-if-match-then-set?forum=winserverpowershell

    Sunday, August 26, 2018 2:19 PM

Answers

  • Changing something like $item.mac to "$item.MAC" is not going to work. You will need to embed it in a string like this: "$($item.mac)".

    It is empty because the CSV you are importing is incorrectly parsed with PowerShell, this could be due to the formatting of the CSV file and regional settings. Try 

    $list = Import-Csv list.csv -Delimiter ","

    • Marked as answer by David M-A Saturday, September 1, 2018 4:00 PM
    Monday, August 27, 2018 6:26 AM

All replies

  • Is this the idea?  (assuming no triple doublequotes around the csv headers.)  The other problem is get-netadapter has '-' instead of ':' in the mac addresses.

    $list = import-csv list.csv
    foreach ($item in $list) {
    get-netadapter | where macaddress -eq $item.mac |
    rename-netadapter -newname $item.nic -whatif
    }
    • Edited by JS2010 Sunday, August 26, 2018 9:13 PM
    Sunday, August 26, 2018 3:47 PM
  • First thanks a lot for the quoting and ":" instead of "-". Corrections are made to the csv file.

    I post the commands since I get an error.

    This is the csv parsed.

    $list 
    Computername,NIC,MAC,DHCP,AddressFamily,IPAddress,PrefixLength,Type,DefaultGateway,DnsServerAddresses
    VM-ISCSI,VnMGNT,00-18-59-00-01-02,true,IPv4,,,Unicast,,
    VM-ISCSI,VniSCSI,00-18-59-00-01-04,false,IPv4,172.16.150.10,24,Unicast,,


    this are the adapters

    Get-NetAdapter
    
    
    Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
    ----                      --------------------                    ------- ------       ----------             ---------
    changeme_                 Microsoft Hyper-V Network Adapter #8         16 Up           00-18-59-00-01-04        10 Gbps
    changeme                  Microsoft Hyper-V Network Adapter #7         12 Up           00-18-59-00-01-02        10 Gbps

    this is the command and the error.

    foreach ($item in $list) {
      get-netadapter | where macaddress -eq $item.mac | rename-netadapter -newname $item.nic -whatif
    }
    Rename-NetAdapter : No se puede validar el argumento del parámetro 'NewName'. El argumento es null o está vacío. Proporcione un argumento que no sea null o que no esté vacío e intente 
    ejecutar el comando de nuevo.
    En línea: 2 Carácter: 80
    + ... caddress -eq $item.mac | rename-netadapter -newname $item.nic -whatif
    +                                                         ~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Rename-NetAdapter], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Rename-NetAdapter
    I was wondering why 
    $item.nic
    is empty and decide to change it to 
    "$item.NIC"
    Now the commands runs with no error but no change is made. Trying arround I change also 

    $item.mac to "$item.MAC"

    foreach ($item in $list) {  get-netadapter | where macaddress -Match "$item.MAC" | rename-netadapter -newname "$item.NIC" -whatif}

    No changes are made:

    Get-NetAdapter
    
    
    Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
    ----                      --------------------                    ------- ------       ----------             ---------
    changeme_                 Microsoft Hyper-V Network Adapter #8         16 Up           00-18-59-00-01-04        10 Gbps
    changeme                  Microsoft Hyper-V Network Adapter #7         12 Up           00-18-59-00-01-02        10 Gbps

    Thanks anyway :D


    • Edited by David M-A Sunday, August 26, 2018 10:21 PM Parragraf, and code format improvement
    Sunday, August 26, 2018 10:18 PM
  • The csv should look like this after importing.  Take off the -whatif if you want it to take effect.

    PS /Users/js> $list = import-csv list.csv                                                                                                           
    PS /Users/js> $list                                                                                                                                 
    Computername       : VM-iSCSI
    NIC                : VnMGNT
    MAC                : 00-18-59-00-01-02
    DHCP               : true
    AddressFamily      : IPv4
    IPAddress          : 
    PrefixLength       : 
    Type               : Unicast
    DefaultGateway     : 
    DnsServerAddresses : 

    Computername       : VM-iSCSI
    NIC                : VniSCSI
    MAC                : 00-18-59-00-01-04
    DHCP               : false
    AddressFamily      : IPv4
    IPAddress          : 172.16.150.10
    PrefixLength       : 24
    Type               : Unicast
    DefaultGateway     : 
    DnsServerAddresses : 


    • Edited by JS2010 Monday, August 27, 2018 4:53 AM
    Sunday, August 26, 2018 10:34 PM
  • Changing something like $item.mac to "$item.MAC" is not going to work. You will need to embed it in a string like this: "$($item.mac)".

    It is empty because the CSV you are importing is incorrectly parsed with PowerShell, this could be due to the formatting of the CSV file and regional settings. Try 

    $list = Import-Csv list.csv -Delimiter ","

    • Marked as answer by David M-A Saturday, September 1, 2018 4:00 PM
    Monday, August 27, 2018 6:26 AM
  • Note that matching against an empty string or null is always true.

    PS /Users/js> 'hi' -match ''                         
    True
    PS /Users/js> 'hi' -match $null
    True

    • Edited by JS2010 Monday, August 27, 2018 1:12 PM
    Monday, August 27, 2018 1:04 PM
  • Thanks to all of you, now the problems is solved.

    Now the csv is properly imported and the variable is not empty anymore.

    :D

    Saturday, September 1, 2018 4:02 PM