none
Assigning Static IPs Remotely RRS feed

  • Question

  • Hello again !

    So now I'm at the 2nd stage of my script: Assigning static IPs remotely. Would someone be able to confirm if this is the proper way to do this ?

    #Selecting NIC

    $AdapterSelector = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Credential $usercreds -filter "Macaddress = '$UserMacAddress'" | select -ExpandProperty NetConnectionID
     
    $NewIPAddress = Invoke-Command -ComputerName $ComputerName -Credential $usercreds -ScriptBlock {netsh interface ip set address ""$AdapterSelector"" static ""$NewUserIP"" ""$NewUserSubnet"" ""$NewUserGateway"" 1}
     
    $AdapterSelector.SetDNSServerSearchOrder($DNSServers)
    $AdapterSelector.put()

    Thank you very much

    Monday, February 29, 2016 8:26 AM

Answers

  • You are still doing this backwards.  As soon as you call enable static you will lose the connection.  It takes effect immediately.

    $DNSServers = "10.64.61.8","10.20.61.5"
    $SelectedNIC = Get-WmiObject win32_networkadapterconfiguration -ComputerName $ComputerName -credential $usercreds -filter "Macaddress = '$UserMacAddress'"
    $SelectedNIC.SetGateways('$NewUserGateway', 1)
    $SelectedNIC.SetDNSServerSearchOrder('$DNSServers')
    $SelectedNIC.EnableStatic('$NewUserIP', '$NewUserSubnet')
    


    \_(ツ)_/

    • Marked as answer by Dennis Papadam Monday, February 29, 2016 3:50 PM
    Monday, February 29, 2016 2:23 PM

All replies

  • You need to learn how to use a computer to search for things.  It will save you a lot of guesswork.

    https://blogs.technet.microsoft.com/heyscriptingguy/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings/


    \_(ツ)_/

    Monday, February 29, 2016 8:32 AM
  • Yes that's actually the first article I stumbled on which led to subsequent searches. The problem is apparently that doing it that way causes you to lose the connection before the static fully gets assigned. I haven't gotten to test it yet as it's 4am EST here, but wanted to know if this is the correct way to go about it.

    Everyone was recommending to go the netsh route instead. What is your take on the situation ?

    It is important to note that I'm executing these tasks remotely. However if I've misunderstood then I'll use that method instead.
    Monday, February 29, 2016 9:04 AM
  • No matter how you do it you will lose the connection.

    \_(ツ)_/

    Monday, February 29, 2016 9:07 AM
  • Yes but that method causes the script to freeze apparently, because you lose the connection as soon as you try to assign the IP, the subnet or gateway remain blank.

    Either way, I'll go with the technet method first as soon as I get into the office this morning. We'll see if it does in fact produce this problem or not.. Else of course I'll take that method it's far cleaner looking.

    Monday, February 29, 2016 9:13 AM
  • That is correct which is why I toldyu0 that we do tat with DHCP now.

    After the connection is lost WMI will timeout in less than 2 minutes and you can reacquire it by flushing DNS cache and reconnection to get the new address or just use the IP you have just changed it to.

    Remember.  You just completely reset the machines relationship with the network and it has to get reset everywhere.


    \_(ツ)_/

    Monday, February 29, 2016 9:18 AM
  • Interesting. I've got a lot of reading to do today :P
    Monday, February 29, 2016 9:22 AM
  • Hello,

    Using this method generates an error for me. I have a feeling it might be down to my quotes on the variables. " Doesn't work, and from what I understand you're supposed to use ' when dealing with variables.

    I also tried without quotes, and although the IP address and subnet fail it still managed to set the gateway that way. Strange.

    Here's the script:

                    $DNSServers = ("10.64.61.8","10.20.61.5")
                    
                    $SelectedNIC = Get-WmiObject win32_networkadapterconfiguration -ComputerName $ComputerName -credential $usercreds -filter "Macaddress = '$UserMacAddress'"
                    $SelectedNIC.EnableStatic('$NewUserIP', '$NewUserSubnet')
                    $SelectedNIC.SetGateways('$NewUserGateway', 1)
                    $SelectedNIC.SetDNSServerSearchOrder('$DNSServers')

    Here are the return codes:

    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     :
    __DYNASTY        : __PARAMETERS
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    ReturnValue      : 70
    PSComputerName   :

    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     :
    __DYNASTY        : __PARAMETERS
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    ReturnValue      : 71
    PSComputerName   :

    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     :
    __DYNASTY        : __PARAMETERS
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    ReturnValue      : 70
    PSComputerName   :

    Monday, February 29, 2016 2:14 PM
  • Made a few modifications.. I think this might be it. Testing it

                    $SelectedNIC = Get-WmiObject win32_networkadapterconfiguration -ComputerName $ComputerName -credential $usercreds -filter "Macaddress = '$UserMacAddress'"
                    $SelectedNIC.EnableStatic($NewUserIP, $NewUserSubnet)
                    $SelectedNIC.SetGateways($NewUserGateway, 1)
                    $SelectedNIC.SetDNSServerSearchOrder($DNSServers)
                    $SelectedNIC.SetDynamicDNSRegistration("FALSE")

    Monday, February 29, 2016 2:22 PM
  • You are still doing this backwards.  As soon as you call enable static you will lose the connection.  It takes effect immediately.

    $DNSServers = "10.64.61.8","10.20.61.5"
    $SelectedNIC = Get-WmiObject win32_networkadapterconfiguration -ComputerName $ComputerName -credential $usercreds -filter "Macaddress = '$UserMacAddress'"
    $SelectedNIC.SetGateways('$NewUserGateway', 1)
    $SelectedNIC.SetDNSServerSearchOrder('$DNSServers')
    $SelectedNIC.EnableStatic('$NewUserIP', '$NewUserSubnet')
    


    \_(ツ)_/

    • Marked as answer by Dennis Papadam Monday, February 29, 2016 3:50 PM
    Monday, February 29, 2016 2:23 PM
  • Here is the Alpha version of my script. Thank you very much for all your assistance. Hopefully this thread will come in useful to others as it has to me. (I plan to start a lot more :P)

    Now that everything is working the next step will be to clean it up and make it a little more proper.

    To anyone that plans to use this script, please note there is no error level checking yet. Please feel free to comment/add etc

    http://pastebin.com/vGch51aW

    Monday, February 29, 2016 3:13 PM