none
Renaming network adapters remotely on a target server using MAC address RRS feed

  • General discussion

  • Hi everyone !

    Been at this for days and it's driving me nuts because I have a feeling it's something simple. I've tried many methods but none of them seem to work, finally I even tried using a psexec wrapper, running psexec to use a .bat file with netsh renaming the adapters but no dice either. Would someone please have a look at this and tell me what I'm doing wrong ? Even if you could point me in the write direction it would be appreciated. (Whole script is here: http://pastebin.com/W6VwTs8P) The other methods I'm trying are commented underneath so 134-142.

    Here are two of the methods I'm trying out:

    (Note that the 2nd one using Invoke-PsExec is using this function: http://www.powershelladmin.com/wiki/Invoke-PsExec_for_PowerShell which I call from the beginning of my script)

    Invoke-Command -ComputerName $ComputerName -Credential $usercreds -ScriptBlock { $nic_name = Get-NetAdapter | Where-Object {$_.MacAddress -eq "$UserMacAddress"} | Select Name; netsh interface set interface name = "$nic_name" newname = "$NewUserNicName" }
                    
    $CommitNewNicName = Invoke-PsExec -Cn $ComputerName -Credential $usercreds -Command '$nic_name = Get-NetAdapter | Where-Object {$_.MacAddress -eq "$UserMacAddress"} | Select Name; netsh interface set interface name = "$nic_name" newname = "$NewUserNicName" ' -IsLongPSCommand

    Thank you in advance for your time
    Thursday, February 25, 2016 9:17 PM

All replies

  • This is the only way:

    Get-NetAdapter | ?{$_.MacAddress -eq '20-99-99-90-99-99'} | Rename-NetAdapter -NewName 'MyEthernet'


    \_(ツ)_/


    • Edited by jrv Friday, February 26, 2016 12:21 AM
    Thursday, February 25, 2016 9:52 PM
  • Thank you for your response !

    Therefore there is no way to execute it remotely it must be done on the system itself ?

    How about this then: (was the extra hyphen a mistake? on Rename-NetAdapter ?

    $CommitNewNicName = Invoke-PsExec -Cn $ComputerName -Credential $usercreds -Command ' Get-NetAdapter | ?{$_.MacAddress -eq '$UserInputMac'} | Rename-NetAdapter -NewName $UserInputNicName ' -IsPSCommand

    Friday, February 26, 2016 12:11 AM
  • You cannot use PsExec to execute that code remotely.

    Yes it was a typo.

    You cannot use single quotes around PowerShell variables.  They only work with constant strings.


    \_(ツ)_/


    • Edited by jrv Friday, February 26, 2016 12:22 AM
    Friday, February 26, 2016 12:21 AM
  • Thank you, my fundamentals are weak I've actually signed up for classes coming up in a month :)

    And so you're telling me there's no way to do this remotely ? As you can see I'm trying to design a utility that gives you the option to rename the adapter, set static IPs or set the target NIC to DHCP. Maybe if this was done in batch code using netsh and then called and executed with psexec would that work ? I've tried that attempt too but I have a feeling the psexec wrapper I've integrated into my code isn't very good.

    Either way sorry to be a pain about this I just feel there MUST be a way to accomplish this with a bit of ingenuity.


    Friday, February 26, 2016 12:57 PM
  • All of your systems should be using DHCP with unlimited leases.  Only the DHCP server needs to be static.

    You can rename the adapter in the registry but this is also a bad practice. You can turn on DHCP remotely but the system will have to be restarted and you will lose the connection and it will appear to have failed.

    Don't turn on DHCP before you set the DHCP scope correctly for all DNS and other required parameters and set the default lease period to infinite/permanent.  This will automatically grab all MacAddresses as the adapters are converted.


    \_(ツ)_/

    Friday, February 26, 2016 1:04 PM
  • Ok I should've been more specific I didn't expect the conversation to expand to this point. There's a bigger picture in mind. First off the reason I want the option to assign DHCP is a 'just in case' measure should the user need it before assigning the statics or if something goes wrong. (The VM is already on DHCP when it is initially deployed, there's a specific segment it's on and it uses PXE to boot and install the general server image they use)

    This revolves around mainly VM deployment. The users get requests through sharepoint on an excel showing how many processors, RAM etc they want on the VM. (Sometimes physical as well).

    First step was to design a utility that audits the system and makes sure you've provisioned what was requested. Most of this was done. The next step was this utlity to assign static IPs / rename NICs and format new disks through powershell because using the VMware console as you know is laggy and painful. Finally the ultimate objective is to be able to deploy VMs directly from powershell by retrieving the the specs from the excel file and provisioning the VM accordingly. It's... ambitious I know. But small steps :)

    So all the way back now to renaming adapters. Why is it considered bad practice to rename the adapter in the registry ? Also would this be the only way to accomplish this remotely ? If so are there any real drawbacks ? As for restarting the system at absolute worst it can be done via vsphere.


    Friday, February 26, 2016 1:16 PM
  • There is no BP as it is not recommended.

    netsh can probably rename the adapter.  Have you looked at it?

    VMWare has PowerShell support and can mange the VM adapters with CmdLets from the host system.  The internal name has to be done during provisioning.  Post in VMWare forum for assistance with this.


    \_(ツ)_/

    Friday, February 26, 2016 1:24 PM
  • Here are some examples: https://blogs.technet.microsoft.com/heyscriptingguy/2014/01/14/renaming-network-adapters-by-using-powershell/

    WMI seems to be able to do this but it doesn't work on W8 and later from what I can see.


    \_(ツ)_/

    Friday, February 26, 2016 1:27 PM
  • ok thank you very much will do
    Friday, February 26, 2016 1:28 PM
  • Nope - I am wrong.  The WMI method still works:

    PS >$adap=gwmi win32_networkadapter -filter 'Macaddress = "20:6A:99:90:9E:EB"'
    PS > $adap.NetConnectionID='MyEthernet3'
    PS > $adap.put()


    \_(ツ)_/



    • Edited by jrv Friday, February 26, 2016 1:32 PM
    Friday, February 26, 2016 1:31 PM
  • Yeah tried that already I've been everywhere. It doesn't like the PUT statement :( I try and psexec it but that won't work either sigh.

    Friday, February 26, 2016 1:32 PM
  • Note that, when changing IP or DHCP you will likely lose connection. Yu can reclaim the adapter after the change via the machine name and mac.  In Win 7 and later the changes will take effect but some services may not like the change so a restart may be necessary.

    DNS may not get updated quickly under some scenarios.  You may have to wait a bit.


    \_(ツ)_/

    Friday, February 26, 2016 1:38 PM
  • Yeah tried that already I've been everywhere. It doesn't like the PUT statement :( I try and psexec it but that won't work either sigh.

    You have to be running elevated if testing locally.  It will not work under PsExec.


    \_(ツ)_/


    • Edited by jrv Friday, February 26, 2016 1:41 PM
    Friday, February 26, 2016 1:41 PM
  • It's a PSexec wrapper, it imports your creds, so I don't think that's the issue.. It seems to be trying to talk to the verbose for some reason. Maybe if I used another wrapper? Also I see a lot of scripts that can import cred objects to psexec in powershell.

    This is the script: http://pastebin.com/4nyYZfL6 (I'm working on Option #2 so lines 121-143)

    This is myfunctions.ps1 I call the wrapper from:  http://pastebin.com/jw3nVXV4

    Friday, February 26, 2016 2:13 PM
  • It won't work with PsExec no matter what you try.  Use WMI.  You cannot use WMI locally without running from an elevated prompt. You can use it remotely with no issue as long as you are an Admin on the remote system.  If you are not then it will also fail.


    \_(ツ)_/

    Friday, February 26, 2016 2:41 PM
  • WMI won't work either because it requires PUT and PUT doesn't seem to like to work remotely try it. It produces an error.

    The reason I mention PsExec is because I was thinking of putting netsh commands in a .bat file and triggering on the target system with psexec. Thing is I actually tried that already but no good either, this is the batch script I tried to use:

    REM @echo off
    setlocal enableDelayedExpansion
    set MACLIST=00:50:56:93:25:0E AA-BB-CC-DD-EE-FF
    for /f "delims=" %%a in ('getmac /fo csv /nh /v') do (
        set line=%%a&set line=!line:"=,!
        for /f "delims=,,, tokens=1,3" %%b in ("!line!") do (
            set name=%%b
            set mac=%%c
            call set mactest=%%MACLIST:!mac!=%%
            if not "!MACLIST!"=="!mactest!" (
                netsh interface set interface name="!name!" newname="newNetworkName1"
                netsh int ip set address "newNetworkName1" static 192.168.0.101 255.255.255.0 0.0.0.0
            )
        )
    )
    pause

    Of course the second problem it produces is that it needs to pull 2 varibles from the powershell script, the User target mac address and the new name the user wishes to give the NIC
    Friday, February 26, 2016 3:30 PM
  • WMI works fine on Windows 7,8, 10 when run from PowerShell with or without credentials.  I have just tested it on all of those systems.


    \_(ツ)_/

    Friday, February 26, 2016 3:32 PM
  • You're talking about this method correct ?

    $wmi = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionID = 'Ethernet'"

    $wmi.NetConnectionID = 'RenamedConnection'

    $wmi.Put()

    This produces the following error for me, although maybe I'm looking at the wrong property ?

    SCRIPT:

    ~~~~~

                    $UserMacAddress = Read-Host -Prompt 'Enter the MAC Address of the NIC you wish to rename: '
                    Write-Host "`n"
                    $NewUserNicName = Read-Host -Prompt 'Enter the new name you wish to give the NIC: '
                    Write-Host "`n"
                    
                      
                    $wmi = Get-WmiObject -ComputerName $ComputerName -Credential $usercreds -Class Win32_NetworkAdapter -Filter "macaddress -eq $UserMacAddress"

                    $wmi.NetConnectionID = $NewUserNicName

                        $wmi.Put()

    ERROR CODE:

    ~~~~~~~~~

    Get-WmiObject : Invalid query "select * from Win32_NetworkAdapter where macaddress -eq 00:50:56:93:25:0E"
    At C:\Users\dpapadam\Desktop\testing\IPsetter.ps1:144 char:12
    +                 $wmi = Get-WmiObject -ComputerName $ComputerName -Credential $usercreds -Cla ...
    +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
        + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    WARNING: The property 'NetConnectionID' cannot be found on this object. Verify that the property exists and can be set.

    Friday, February 26, 2016 4:03 PM
  • What version oof Windows is on the remote computer?  I also cannot see the exact code you used so it is meaningless for troubleshooting.

    Remember that pseudo adapters cannot be changed as they are created by the OS.  You are likely not querying the physical external adapter.


    \_(ツ)_/

    Friday, February 26, 2016 4:09 PM
  • ... -Class Win32_NetworkAdapter -Filter "macaddress -eq $UserMacAddress"

    This might be confusing, but when you use -Filter with Get-WmiObject, you are specifying a WQL query, which uses a different syntax than PowerShell. Start by reading the description for the -Filter parameter in the help:


    PS C:\> help Get-WmiObject -full

    In other words, you are getting the error "invalid query" because you need to use the = character, not -eq (-eq is the PowerShell equality operator, not the WQL equality operator).


    -- Bill Stewart [Bill_Stewart]

    Friday, February 26, 2016 4:40 PM
    Moderator
  • Good catch Bill.

    This is what I posted above.  It has the correct syntax.

    Also ewhen using a variable you must do this:

    $mac='20:6A:99:90:9E:EB'

    PS >$adap=gwmi win32_networkadapter -filter "Macaddress = '$mac'"
    PS > $adap.NetConnectionID='MyEthernet3'
    PS > $adap.put()

    Note the way the quotes are applied and n ote that the mac is withcolons and NOT dashes.


    \_(ツ)_/


    • Edited by jrv Friday, February 26, 2016 5:56 PM
    Friday, February 26, 2016 5:55 PM
  • Thanks unfortunately still doesn't work. I did:

                    $adap=Get-WmiObject win32_networkadapter -ComputerName $ComputerName -credential $usercreds -filter "Macaddress = '$UserMacAddress'"
                    $adap.NetConnectionID='$NewUserNicName'
                    $adap.put()

    which yields:

    WARNING: Exception calling "Put" with "0" argument(s): "Provider is not capable of the attempted operation "

    Originally when I had gotten the command right this was the error was getting which led me to all the other crazy tricks I've been attempting. FYI I'm actually discussing this at the same time on the VM forum, eventually I will use their cmdets I'm taking the courses tonight. However I have a feeling you're onto something maybe there's a way around this error ?

    This target server is 2003. However the VMs we will be deploying are 2k8 and up. I'm trying to make this work on all OSes
    Friday, February 26, 2016 8:32 PM
  • Check with VMWare.  Their adapter amy not be able to be renamed.  Remember a VM has  a virtual adapter.  It can be renamed in Hyper-V with WMI and with the VM CmdLets.  It is likely that VMWare has a rename function.  The adapter is injected into the VM as a piece of virtual hardware.  It is managed by the vendors tools and not by Windows.

    \_(ツ)_/

    Friday, February 26, 2016 8:35 PM
  • Just tried this on 2k8 and it works. Guess 2k3 has to be something more clever like netsh but with the ability to use creds. Accidently named the NIC $NewUserNICName lol. Removing the quotes...
    Friday, February 26, 2016 8:37 PM
  • I never said anything about 2k3 or XP.  The rename is available ,only on Vista and later.  You should have noted you were using an obsolete system.


    \_(ツ)_/

    Friday, February 26, 2016 8:40 PM
  • You have to learn to read the documentation:

    This is the support for the class.  Note that it is Vista.  This version of the class does not exist in XP/WS2003.  The CIM components are all new as of Vista.

    Minimum supported client

    Windows Vista

    Minimum supported server

    Windows Server 2008

    Namespace

    Root\CIMV2

    MOF

    CIMWin32.mof

    DLL

    CIMWin32.dll

    \_(ツ)_/

    Friday, February 26, 2016 8:47 PM
  • You are correct I apologize. If it's any consolation however I learned a lot ! :)
    Friday, February 26, 2016 9:00 PM
  • No problem.  I should have realized your issues were pointing at an old OS as the source of the problem.

    We should always check compatibility when results are odd.


    \_(ツ)_/

    Saturday, February 27, 2016 4:50 PM