none
Remotely renaming a network adapter name

    Question

  • I am trying to build a PowerShell script that will remotely rename the default network adapter names (Local Area Connection ...) to meaningful names.  Cisco has a PowerShell interface into its UCS that allows me to get the NIC name and MAC address for each NIC assigned to a service profile.  I can readily gather this information from any system on which I have installed their PowerTool PowerShell module.  So, I can remotely obtain the name I want the NIC to have and the unique identifier of the MAC.  (BTW, this is for Windows Server 2008.  I know things change in Windows Server 2012, but I need a solution for current software.)

    I know that I can do a netsh interface set interface name="Local Area Connection" newname="PrivateNetwork" locally on a machine to change the name.  I can also execute that remotely with a winrs command.  But, there are two problems here.  First, I have to know the old name before I can assign a new name.  It's easy enough to get the MAC address, but I haven't found a way to extract the existing name to use in the netsh command.  (Yes, I can output to a file and parse the file, that I'm trying to avoid that).  Secondly, I want to be able to issue the command without the remote machine being part of a domain.  Trying to use winrm to set up access by https seems a bit onerous, too.  (Is it obvious yet that I'm looking for an 'easy' solution. <grin>)

    A friend showed me another way to use PowerShell with the shell.application and the .namespace(0x31) of the new-object to be able to change the name, but his example also works locally.  So, here are some of the pieces I am using, but I'm hoping that someone has some idea about how I can write information to the remote host in a relatively 'easy' manner.

    I first use PowerTool (Cisco's PowerShell module) to extract desired NIC name and its assigned MAC address.  I use Get-WMIobject Win32_NetworkAdapterConfiguration -computername $ServerName to obtain the MAC addresses of server $ServerName (Yes, this assumes DHCP was used to assign an IP address and the name was automatically registered in DNS).  I can also extract the existing name out of Get-WMIobject information.  So, I have a MAC address that can be used as a key/index to associate the 'soft' information I have from Cisco's UCS with the 'hard' information that exists on the deployed server.  Now I want to be able to change the name.

    I am creating the physical machines with a common Admin password, so if there is a way to pass username/pwd, that is okay.  I know I can write to the server if both my workstation and the target server are part of the same domain.  I just can't do it with the netsh command, at least I haven't found a way.  Nor have I found a way to use the shell.application to a remote system.

    So, the bottom line is that I would like to be able to work on a non-domain joined system and write to it to change the NIC name on the that non-domain joined system.

    Thanks for any pointers on this.


    tim

    Tuesday, June 26, 2012 7:56 PM

Answers

  • Well, that's not PS, but batch

    Hope it helps

    for /f "tokens=3,4,5 delims= " %a in ('ipconfig ^| findstr "Local" ^| findstr /V "Tunnel"') do; set c=%c&&set con=%c:~0,-1%&&netsh interface set interface name="%a %b %con%" newname="NewName"

    It parses the output of ipconfig,while looking for the word Local (may be local area connection, l.a.c1, l.a.c2 and forth), and excludes everythin that contains the word "tunnel", for known reason and then uses netsh to rename the specified interface. you can use psexec to mass remote rename this way:

    :go

    for /f %%t in (pcs.txt) do; set host=%%t&&call :subr0

    :subr0

    psexec.exe \\%host% -u username -p passwd cmd.exe /c "for /f "tokens=3,4,5 delims= " %a in ('ipconfig ^| findstr /V "Local" ^| findstr "Tunnel"') do; set c=%c&&set con=%c:~0,-1%&&netsh interface set interface name="%a %b %con%" newname="NewName""

    exit /b 11

    as well you can acces the remote machine even even it's not part of a domain, but u need to have it's local admin accout. you can use

    ... psexec.exe -u %host%\localadmin -p localadminpassword...





    Wednesday, June 27, 2012 8:32 AM
  • Yes, I made that assumption, and it didn't surprise me that I couldn't go from a workgroup to a domain-joined sysstem.  What seemed strange was the other direction - from a domain-joined machine I could not modify the workgroup machine.  That seems like it should be very similar to the workgroup to workgroup as there is no common security principal.

    tim

    The logon you have to do both ways. It is a domain account login and both require packet provicy (secured authentication).

    The domain has to logged into to join and to remove or rename.  The domian owns teh computer once it is joined.  You cannot disjoin without domain credentials that are authorized to do that.  It has always been that way eben if yu do it manually.

    This is why I  try to getr admins to join the 21st centurty and use MDT.  I haev done all of those old headache things.  There si never a good reason to do that naymore.

    You can also set the final machine name when you install the image.  You do not have to rename the machine.  Just image to the correct name or rather install to the correct name. 


    ¯\_(ツ)_/¯

    Thursday, June 28, 2012 8:24 PM

All replies

  • Well, that's not PS, but batch

    Hope it helps

    for /f "tokens=3,4,5 delims= " %a in ('ipconfig ^| findstr "Local" ^| findstr /V "Tunnel"') do; set c=%c&&set con=%c:~0,-1%&&netsh interface set interface name="%a %b %con%" newname="NewName"

    It parses the output of ipconfig,while looking for the word Local (may be local area connection, l.a.c1, l.a.c2 and forth), and excludes everythin that contains the word "tunnel", for known reason and then uses netsh to rename the specified interface. you can use psexec to mass remote rename this way:

    :go

    for /f %%t in (pcs.txt) do; set host=%%t&&call :subr0

    :subr0

    psexec.exe \\%host% -u username -p passwd cmd.exe /c "for /f "tokens=3,4,5 delims= " %a in ('ipconfig ^| findstr /V "Local" ^| findstr "Tunnel"') do; set c=%c&&set con=%c:~0,-1%&&netsh interface set interface name="%a %b %con%" newname="NewName""

    exit /b 11

    as well you can acces the remote machine even even it's not part of a domain, but u need to have it's local admin accout. you can use

    ... psexec.exe -u %host%\localadmin -p localadminpassword...





    Wednesday, June 27, 2012 8:32 AM
  • Thanks.  You gave me an answer - but not exactly what I was looking for.  I hve done so little in batch scripting, that trying to adapt your script to key on matching the MAC address from the ipconfig/all command to my source of MAC addresses is more than I want to take on.

    However, I had totally forgotten about psexec's ability to execute remotely!  So, I have a PowerShell routine in which I already created the logic to match MAC addresses from two sources in preparation for issuing the netsh command.  Therefore, I simply add psexec as the engine to remotely execute it, and I'm ready to go.

    Thanks for the tip.


    tim

    Wednesday, June 27, 2012 2:45 PM
  • Much easier to do with WMI.  Youcan just remotely rename the adapther and get the MAC at teh same time.

    If you are using PowerSHell why not do it all in PowerShell.  WMI is a remoting technology and, since W98 it has been installed on every system.  Comnbine WMUI with PowerSHell and you have a very powerful engine for remote management of most system items.


    ¯\_(ツ)_/¯

    Wednesday, June 27, 2012 3:01 PM
  • Much easier to do with WMI.  Youcan just remotely rename the adapther and get the MAC at teh same time.

    If you are using PowerSHell why not do it all in PowerShell.  WMI is a remoting technology and, since W98 it has been installed on every system.  Comnbine WMUI with PowerSHell and you have a very powerful engine for remote management of most system items.


    ¯\_(ツ)_/¯

    The problem I've run into with WMI and network adapters is that they don't call them "Local Area Connection", etc, in WMI.  There they're called "Intel Pro 1000" or something similar. 

    So it's easier to use netsh, which understands names like "Local Area Connection", and this is in fact the attribute you want to change.


    Grant Ward, a.k.a. Bigteddy

    Wednesday, June 27, 2012 3:12 PM
  • It's possible to get the adapter's GUI display name in WMI from the Win32_NetworkAdapter NetConnectionID property. This property was added as a read-only property in XP/2003 and is read-write in Vista/Server 2008 and newer. However, IIRC, you can't change this property for a remote computer. (This may have been fixed since the last time I tried this, though.)

    Bill

    Wednesday, June 27, 2012 3:26 PM
  • On WS2003 and earlier we can use WMI to find the adapter then use WIn32_Process.Create to run netsh locally. 

    With PowerShell you can test this at a prompt until you work out the best method for executing the change.

    The following works fine on Vista and later systems.

    $nic=gwmi win32_networkadapter -computer ws101 -filter 'deviceid=7'
    $nic.NetConnectionID='LAN'
    $nic.Put()

    The MAC can be gotten first and from that the id of the adapter then grab the adapter.

    I just trested this against an XP machine.  I used PS Remoting to runnetsh.  It works fine.

    Win32_Process.Create can be used as well as PsExec. 

    I can retrieve the interface by AMC and get its name to use in the netsh command.


    ¯\_(ツ)_/¯



    • Edited by jrv Wednesday, June 27, 2012 4:48 PM
    Wednesday, June 27, 2012 4:37 PM
  • Here eis how to get teh exact interface by MAC address.  We need to filter for a not null id becuse the 'PacketScheduler' has the same MAC but no Net ID.

    gwmi win32_networkadapter -computer ws005 -filter 'macaddress="00:16:76:84:CB:4E"
    AND NetConnectionID IS NOT NULL'|
    select name,macaddress,NetConnectionID

    The same WMI query will work in VBScript, and WMIC.


    ¯\_(ツ)_/¯

    Wednesday, June 27, 2012 4:59 PM
  • Thanks for the very informative and useful discussion.

    It appears that Get-WMIobject Win32_NetworkAdapter will work fine within a domain or workgroup to workgroup, but when going into a workgroup computer from a domain-joined workstation or trying it to a domain-joined computer from a workgroup computer, I am receiving an Access is denied error.


    tim

    Thursday, June 28, 2012 6:42 PM
  • Thanks for the very informative and useful discussion.

    It appears that Get-WMIobject Win32_NetworkAdapter will work fine within a domain or workgroup to workgroup, but when going into a workgroup computer from a domain-joined workstation or trying it to a domain-joined computer from a workgroup computer, I am receiving an Access is denied error.


    tim

    If it is joined you must use a username and password that has rights on the computer object in AD.


    ¯\_(ツ)_/¯

    Thursday, June 28, 2012 6:50 PM
  • Yes, I made that assumption, and it didn't surprise me that I couldn't go from a workgroup to a domain-joined sysstem.  What seemed strange was the other direction - from a domain-joined machine I could not modify the workgroup machine.  That seems like it should be very similar to the workgroup to workgroup as there is no common security principal.

    tim

    Thursday, June 28, 2012 8:11 PM
  • Yes, I made that assumption, and it didn't surprise me that I couldn't go from a workgroup to a domain-joined sysstem.  What seemed strange was the other direction - from a domain-joined machine I could not modify the workgroup machine.  That seems like it should be very similar to the workgroup to workgroup as there is no common security principal.

    tim

    The logon you have to do both ways. It is a domain account login and both require packet provicy (secured authentication).

    The domain has to logged into to join and to remove or rename.  The domian owns teh computer once it is joined.  You cannot disjoin without domain credentials that are authorized to do that.  It has always been that way eben if yu do it manually.

    This is why I  try to getr admins to join the 21st centurty and use MDT.  I haev done all of those old headache things.  There si never a good reason to do that naymore.

    You can also set the final machine name when you install the image.  You do not have to rename the machine.  Just image to the correct name or rather install to the correct name. 


    ¯\_(ツ)_/¯

    Thursday, June 28, 2012 8:24 PM