none
RenewDHCPLease giving 169 address every other time... RRS feed

  • Question

  • Running the following code to refresh the IP address for a NIC on a laptop:

    $ethernet=Get-WmiObject–ClassWin32_NetworkAdapterConfiguration|Where{ $_.IpEnabled -eq$true-and$_.DhcpEnabled -eq$true-and$_.IPAddress -ne$Null}  
    
    foreach($nicin$ethernet) {
    
    	$nic.ReleaseDHCPLease()
      	$nic.RenewDHCPLease() 
    }


    Every other time I run this I get a 169. IP address instead of a 10. address like I should.  Any ideas why?  So, I run it the first time it gives me a 10.x.x.x address.  The next time a 169.x.x.x address and then the next time a 10.x.x.x address.  When I run it I get a Return Value of 0 for the Release and an 84 for Renew.  I know what Microsoft says that means but, makes no sense.  If I run it again I get a Return Value of 83 for Release and a 0 for Renew and I get a valid 10.x.x.x address.





    Wednesday, January 24, 2018 1:35 PM

Answers

  • Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True' |
    	ForEach-Object{
    		$results = $_.RenewDHCPLease()
    		if($results.ReturnValue -ne 0){ Write-Host "Failed: ($results.ReturnValue)"  }
    	}
    

    This would be even better because you need to test for errors.


    \_(ツ)_/

    Friday, January 26, 2018 7:05 PM

All replies

  • First don't post colorized code.  Use the code posting tool.  Please edit you post and fix this.

    \_(ツ)_/

    Wednesday, January 24, 2018 5:01 PM
  • If you release the adapter then that address will be displayed.  Don't release just "Renew"

    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True' |
    	ForEach-Object{$_.RenewDHCPLease()}

    The new address will not be immediately available if you release the adapter. 


    \_(ツ)_/


    • Edited by jrv Wednesday, January 24, 2018 5:08 PM
    Wednesday, January 24, 2018 5:07 PM
  • Did not intend to use color. That's how it pasted. I changed it and inserted code.  Sorry for the mistake.
    Thursday, January 25, 2018 2:46 PM
  • No you didn't fix it you just changed it.  The code is still unreadable.  Copy to notepad ad post using the code posting tool provided.

    If you make it hard for others to understand the code or the question you will not be likely to get a good answer.  It is a good idea to learn the forum tools and posting etiquette for each forum you use.


    \_(ツ)_/


    • Edited by jrv Thursday, January 25, 2018 5:00 PM
    Thursday, January 25, 2018 4:59 PM
  • I understand and YES I did change it.  I did copy it to notepad. Then chose to post code and said source was Powershell.  Then pasted code into the window.  It was Black and white when I pasted it and turned color and realigned itself when I hit submit.  Will try again.

    Friday, January 26, 2018 12:29 PM
  • You are not using notepad.  Notepad++ maybe.  Use Windows notepad and use code posting tool.


    \_(ツ)_/

    • Proposed as answer by The Grim Friday, January 26, 2018 6:59 PM
    Friday, January 26, 2018 6:57 PM
  • This is what you code should be and this is how to paste it:

    $ethernet = Get-WmiObject –Class Win32_NetworkAdapterConfiguration | 
    	Where{
    		$_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true -and $_.IPAddress -ne $Null
    	}
    
    foreach ($nic in $ethernet) {	
    	#$nic.ReleaseDHCPLease()
    	$nic.RenewDHCPLease()
    }
    

    This is the better way to do this:

    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True' |
    	ForEach-Object{$_.RenewDHCPLease()}
    
    


    \_(ツ)_/

    Friday, January 26, 2018 7:03 PM
  • Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True AND DhcpEnabled=True' |
    	ForEach-Object{
    		$results = $_.RenewDHCPLease()
    		if($results.ReturnValue -ne 0){ Write-Host "Failed: ($results.ReturnValue)"  }
    	}
    

    This would be even better because you need to test for errors.


    \_(ツ)_/

    Friday, January 26, 2018 7:05 PM