none
change ip address of remote computer from dynamic to static

    Question

  • from the txt file I have

     

    HOST;IPADDR;NETMASK;GW;DNS1;DNS2

    pa-vcs-int;10.50.1.108;255.255.0.0;10.50.0.5;10.50.1.56;10.50.1.42

     

    from my ps script

     

    -----------------------

     

    function Set-STATIC {

      param($HOST,$IPADDR,$NETMASK,$GW,$dns1,$dns2)

     

    #Get NICS via WMI

      $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name where {$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}

      foreach($NIC in $NICs) {

        $DNSServers = $dns1,$dns2

        $NIC.SetDNSServerSearchOrder($DNSServers)

        $NIC.SetDynamicDNSRegistration("TRUE")

       $IPADDR = ($NIC.IPAddress[0])

        $GW = $NIC.DefaultIPGateway

        $NETMASK = $NIC.IPSubnet[0]

        $NIC.EnableStatic($IPADDR, $NETMASK)

        $NIC.SetGateways($GW)

      }

    }

     

     

    but it does not work.

     

    any ideas?

    Monday, December 12, 2011 10:58 PM

Answers

  • You lost connectivity when you change IP, SetGateways does not have a major impact if you are not working on different subnets.

    I do not know why it dont works for you perhaps you need to have something in your environment which causes problems. In my test environment is everything ok, yes it return error that lost connection but you may ignore it by | out-Null. Finally adress is chenged from DHCP to new static IP.

    PS. It works witout $NIC.put()

    Last attempt, not profesional but try test it not via WMI:

    function Set-Static 
    {
     param(
      $Name,
      $Ipaddr,
      $subnetmask,
      $defaultGW,
      $dns1,
      $dns2
     )
     
     
     #Get NICS via WMI
     $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name -Filter "IPEnabled=TRUE"
      
     foreach($NIC in $NICs) 
     {
      $NetConnectionID = Get-WmiObject Win32_NetworkAdapter -ComputerName $Name | Where {$_.name -eq $NIC.Description} | select -expandProperty NetConnectionID 
      $cmd1 = "netsh int ip set dnsservers '$NetConnectionID' static $dns1 primary"
      $cmd2 = "netsh int ip add dnsservers '$NetConnectionID' $dns2"
      $cmd3 = "netsh int ip set address '$NetConnectionID' static $Ipaddr $subnetmask $defaultGW 1"
    
      Invoke-Command -ComputerName $Name -ScriptBlock{param($cmd) Invoke-Expression $cmd} -ArgumentList $cmd1 | Out-Null
      Invoke-Command -ComputerName $Name -ScriptBlock{param($cmd) Invoke-Expression $cmd} -ArgumentList $cmd2 | Out-Null
      Invoke-Command -ComputerName $Name -ScriptBlock{param($cmd) Invoke-Expression $cmd} -ArgumentList $cmd3 -sessionoption (new-pssessionoption -OperationTimeout 6000 ) -ErrorAction SilentlyContinue | Out-Null
      
     } #end foreach
    } #end function
     
    Import-Csv "C:\servers.csv" -UseCulture | %{ 
     Set-Static -Name $_.Name -Dns1 $_.dns1  -Dns2 $_.dns2 -Ipaddr $_.ipaddr -subnetmask $_.subnetmask -defaultGW $_.defaultGW 
    } #end %
    
    

    Wednesday, December 14, 2011 5:21 PM

All replies

  • try tossing in a $nic.put() at the end of the loop, that should push the
    settings to the system.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, December 13, 2011 12:20 AM
  • I cant get it working. here is what I have

     

     

    function Set-STATIC {

     

    param($NAME,$IPADDR,$NETMASK,$GW,$dns1,$dns2)

     

    #Get NICS via WMI

     

    $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name where {$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}

     

    foreach($NIC in $NICs) {

     

    $DNSServers = $dns1,$dns2

    $NIC.SetDNSServerSearchOrder($DNSServers)

    $NIC.SetDynamicDNSRegistration("TRUE")

    $IPADDR = ($NIC.IPAddress[0])

    $GW = $NIC.DefaultIPGateway

    $NETMASK = $NIC.IPSubnet[0]

    $NIC.EnableStatic($IPADDR, $NETMASK)

    $NIC.SetGateways($GW)

    $NIC.Put()

    }

    }

     

    $array = get-content "C:\servers.txt"

    ForEach (($NAME,$IPADDR,$NETMASK,$GW,$dns1,$dns2)) in $array {

     

    Set-STATIC -NAME $_.NAME -IPADDR $_.IPADDR -NETMASK $_.NETMASK -GW $_.GW -DNS1 $_.DNS1 -DNS2 $_.DNS2

     

    }

     

     

    but the error I get is

     

     

    Missing variable name after foreach.

    At C:\dynamic_to_static.ps1:24 char:10

    + ForEach ( <<<< ($NAME,$IPADDR,$NETMASK,$GW,$dns1,$dns2)) in $array {

        + CategoryInfo          : ParserError: (VariableToken:TokenId) [], ParseException

        + FullyQualifiedErrorId : MissingVariableNameAfterForeach

     


    • Edited by JonDoe321 Tuesday, December 13, 2011 1:31 AM
    Tuesday, December 13, 2011 1:31 AM
  • Defining your $NICS variable like the following will get you a little closer...

     

    $NICs = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $NAME | where-object{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true})


    Just a humble SysAdmin
    Tuesday, December 13, 2011 2:16 AM
  • Changing your NIC from dynamic to static will disrupt LAN communications briefly, probably causing your script to fail if it is being run against a remote computer.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    • Edited by Bigteddy Tuesday, December 13, 2011 10:43 AM
    Tuesday, December 13, 2011 10:42 AM
  • HI Joe,

    Is it possible that one of your hosts had more than one NICs on DHCP ?

    Tuesday, December 13, 2011 2:24 PM
  • to that point, you should probably put in some logic that makes sure it only
    works on one nic (break loop once you find one valid one)
     
     

    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, December 13, 2011 2:28 PM
  • OK, if you have only switch from dhcp to static you may use this:
    function Set-STATIC 
    {
     param(
      $NAME,
      $dns1,
      $dns2
     )
     
     $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $NAME | where {$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true}
     
     foreach($NIC in $NICs) 
     {
      if($NIC.DHCPEnabled -eq $true)
      {
       $DNSServers = $dns1,$dns2
       $NIC.SetDNSServerSearchOrder($DNSServers)
       $NIC.SetDynamicDNSRegistration("TRUE")
       $IPADDR = ($NIC.IPAddress[0])
       $GW = $NIC.DefaultIPGateway
       $NETMASK = $NIC.IPSubnet[0]
       $NIC.EnableStatic($IPADDR, $NETMASK)
       $NIC.SetGateways($GW)
      }
     }
    }
    
    $array = Import-Csv "C:\servers.txt" -Delimiter ";"
    $array | % {Set-STATIC -NAME $_.HOST -DNS1 $_.DNS1 -DNS2 $_.DNS2}
    
    

    Servers.txt:
    HOST;DNS1;DNS2
    pa-vcs-int;10.50.1.56;10.50.1.42
    Then you don’t need IPADDR;NETMASK;GW in file. But if you want declare own static address you must choose with NIC will get with IP and it will be more advanced.
    Tuesday, December 13, 2011 2:59 PM
  • Hi I need to chnage

    ip address

    netmask

    default gateway

    dns1

    dns2 

     

    all to static from dhcp.

     

    So far I have only been able to change it to static but it leaves the DG empty and loses connectivity to the remote host. 

     

    what should i do?

     

    function Set-Static {

      param($name,$Ipaddr,$subnetmask,$defaultGW,$dns1,$dns2)

     

    #Get NICS via WMI

    $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name -Filter "IPEnabled=TRUE"

    foreach($NIC in $NICs) {

    $DNSServers = $dns1,$dns2

    $NIC.SetDNSServerSearchOrder($DNSServers)

    $NIC.SetDynamicDNSRegistration("TRUE")

    $NIC.EnableStatic($Ipaddr,$subnetmask)

    $NIC.SetGateways($defaultGW)

    $NIC.put()

      }

    }

     

    Import-Csv "C:\servers.csv" -UseCulture | %{

      Set-Static -Name $_.Name -Dns1 $_.dns1  -Dns2 $_.dns2 -ipaddr $_.ipaddr -subnetmask $_.subnetmask -defaultGW $_.defaultGW | Out-Null

    }

    Wednesday, December 14, 2011 8:12 AM
  • Changing your NIC from dynamic to static will disrupt LAN communications briefly, probably causing your script to fail if it is being run against a remote computer.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')

    No-one took any notice when I posted this yesterday.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Wednesday, December 14, 2011 9:53 AM
  • Changing your NIC from dynamic to static will disrupt LAN communications briefly, probably causing your script to fail if it is being run against a remote computer.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')

    No-one took any notice when I posted this yesterday.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


    I test it remotly between two machines:
    1) W7 with one NIC
    2) W08R2 with two additional NIC on DHCP.
    I had no interruptions when changing IP from DHCP to Static with my script (but IP address was still the same). Both NICs have been changed correctly.

    Wednesday, December 14, 2011 10:15 AM
  • I test it remotly between two machines:
    1) W7 with one NIC
    2) W08R2 with two additional NIC on DHCP.
    I had no interruptions when changing IP from DHCP to Static with my script (but IP address was still the same). Both NICs have been changed correctly.
    OK, I stand corrected.  But then why is the OP's script failing?

    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Wednesday, December 14, 2011 12:00 PM
  • I think I know what's wrong. Everything works when I try change dhcp to static but using the same IP. When I try change it to different IP then yes is disrupt, because I still have in dns old ip. Misled me the first Jon's script. Some solution is change order methods: first change gateway and next enablestatic.

    function Set-Static 
    {
     param(
      $Name,
      $Ipaddr,
      $subnetmask,
      $defaultGW,
      $dns1,
      $dns2
     )
    
     #Get NICS via WMI
     $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name -Filter "IPEnabled=TRUE"
     
     foreach($NIC in $NICs) 
     {
      $DNSServers = $dns1,$dns2
      $NIC.SetDNSServerSearchOrder($DNSServers)
      $NIC.SetDynamicDNSRegistration("TRUE")
      $NIC.SetGateways($defaultGW)
      $NIC.EnableStatic($Ipaddr,$subnetmask)
      
     } #end foreach
    } #end function
     
     
     
    Import-Csv "C:\servers.csv" -UseCulture | %{ 
      Set-Static -Name $_.Name -Dns1 $_.dns1  -Dns2 $_.dns2 -Ipaddr $_.ipaddr -subnetmask $_.subnetmask -defaultGW $_.defaultGW 
    } #end %
    
    


    We also lose connectivity but everything will be changed for this NIC.

    But as I said it will work only when we have one NIC!

    Wednesday, December 14, 2011 12:27 PM
  • I trid it but it does not work. IP is still set at dhcp and only the dns gets changed. The script ends up with RPC errors
    Wednesday, December 14, 2011 3:54 PM
  • i think the dg cannot be set. All I can do remotely is set the static ip, dns netmask

     

    function Set-Static {
      param($name,$Ipaddr,$subnetmask,$defaultGW,$dns1,$dns2)

    #Get NICS via WMI
    $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name -Filter "IPEnabled=TRUE"
    foreach($NIC in $NICs) {
    $DNSServers = $dns1,$dns2
    $NIC.SetDNSServerSearchOrder($DNSServers)
    $NIC.SetDynamicDNSRegistration("TRUE")
    $NIC.SetGateways($defaultGW)
    $NIC.EnableStatic($Ipaddr,$subnetmask)
    $NIC.put()
      }
    }

    Import-Csv "C:\servers.csv" -UseCulture | %{
      Set-Static -Name $_.Name -Dns1 $_.dns1  -Dns2 $_.dns2 -ipaddr $_.ipaddr -subnetmask $_.subnetmask -defaultGW $_.defaultGW| Out-Null
    }

     

     

    basically the $NIC.SetGateways cannot be run because changing the IP address causes a loss of connectivity to the hostname

     

     

    Wednesday, December 14, 2011 4:11 PM
  • You lost connectivity when you change IP, SetGateways does not have a major impact if you are not working on different subnets.

    I do not know why it dont works for you perhaps you need to have something in your environment which causes problems. In my test environment is everything ok, yes it return error that lost connection but you may ignore it by | out-Null. Finally adress is chenged from DHCP to new static IP.

    PS. It works witout $NIC.put()

    Last attempt, not profesional but try test it not via WMI:

    function Set-Static 
    {
     param(
      $Name,
      $Ipaddr,
      $subnetmask,
      $defaultGW,
      $dns1,
      $dns2
     )
     
     
     #Get NICS via WMI
     $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name -Filter "IPEnabled=TRUE"
      
     foreach($NIC in $NICs) 
     {
      $NetConnectionID = Get-WmiObject Win32_NetworkAdapter -ComputerName $Name | Where {$_.name -eq $NIC.Description} | select -expandProperty NetConnectionID 
      $cmd1 = "netsh int ip set dnsservers '$NetConnectionID' static $dns1 primary"
      $cmd2 = "netsh int ip add dnsservers '$NetConnectionID' $dns2"
      $cmd3 = "netsh int ip set address '$NetConnectionID' static $Ipaddr $subnetmask $defaultGW 1"
    
      Invoke-Command -ComputerName $Name -ScriptBlock{param($cmd) Invoke-Expression $cmd} -ArgumentList $cmd1 | Out-Null
      Invoke-Command -ComputerName $Name -ScriptBlock{param($cmd) Invoke-Expression $cmd} -ArgumentList $cmd2 | Out-Null
      Invoke-Command -ComputerName $Name -ScriptBlock{param($cmd) Invoke-Expression $cmd} -ArgumentList $cmd3 -sessionoption (new-pssessionoption -OperationTimeout 6000 ) -ErrorAction SilentlyContinue | Out-Null
      
     } #end foreach
    } #end function
     
    Import-Csv "C:\servers.csv" -UseCulture | %{ 
     Set-Static -Name $_.Name -Dns1 $_.dns1  -Dns2 $_.dns2 -Ipaddr $_.ipaddr -subnetmask $_.subnetmask -defaultGW $_.defaultGW 
    } #end %
    
    

    Wednesday, December 14, 2011 5:21 PM