locked
Assigning DHCP IP manually once allocated using powershell script RRS feed

  • Question

  • Hi There

    In order to avoid client's going back to DHCP once IP is configured after provisioning. I am trying to automate manual assignment. As per our setup, Windows VMs are auto provisioned through the API on openstack platform and public IPs are configured through DHCP. Now as the client's go back to DHCP after every few minutes, any time there is any issue with DHCP server, all client's loose their IPs and the availability goes down. Thus I am trying to write a script which should first note the IP configuration in variables and then disable dhcp in client and then write the same variable on the network interface configurations thus the IP assigment is changed to manual and client does not have to go back to dhcp server again. Below is the script at the moment and I am stuck at one point.

    $IP = Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 | Select IPAddress
    $Mask = Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 | Select PrefixLength
    $Gateway = Get-WmiObject win32_NetworkAdapterConfiguration | Select DefaultIPGateway
    $IPType = "IPv4"
    $Dns = "8.8.8.8"
    $adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
    If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
     $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
    }
    If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
     $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
    }
    Set-NetIPInterface -DHCP Disabled
    $adapter | New-NetIPAddress -AddressFamily $IPType -IPAddress $IP -PrefixLength $Mask.string -DefaultGateway $Gateway
    $adapter | Set-DnsClientServerAddress -ServerAddresses $Dns

    When I execute the above, It gives me an error

    New-NetIPAddress : Invalid parameter IPv4Address @{IPAddress=xx.xx.xx.xx}
    At line:1 char:1
    + New-NetIPAddress -AddressFamily $IPType -IPAddress $IP -PrefixLength $Mask.strin ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (MSFT_NetIPAddress:ROOT/StandardCimv2/MSFT_NetIPAddress) [New-NetIPAddr
       ess], CimException
        + FullyQualifiedErrorId : Windows System Error 87,New-NetIPAddress

    Any advise would be highly appreciated.



    Monday, January 28, 2019 7:20 AM

Answers

  • Please format your code as code here in the forum. Thanks.

    you have to either expand the pure IP address when you get it withGet-NetIPAddress ...

    $IP = Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 | Select-Object -ExpandProperty IPAddress

    or you have to specify only the attribute when you provide it to New-NetIPAddress ...

    $adapter | New-NetIPAddress -AddressFamily $IPType -IPAddress $($IP.IPAddress) -PrefixLength $Mask.string -DefaultGateway $Gateway

    !!  one of the solutions ... not both!  ;-) :-D

    I could imagine if you have more than one IP addresses on the interface you query you even have to choose one of it to use it later on.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Monday, January 28, 2019 7:49 AM
    • Marked as answer by Basheerssyed Monday, January 28, 2019 8:21 AM
    Monday, January 28, 2019 7:46 AM

All replies

  • Please format your code as code here in the forum. Thanks.

    you have to either expand the pure IP address when you get it withGet-NetIPAddress ...

    $IP = Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 | Select-Object -ExpandProperty IPAddress

    or you have to specify only the attribute when you provide it to New-NetIPAddress ...

    $adapter | New-NetIPAddress -AddressFamily $IPType -IPAddress $($IP.IPAddress) -PrefixLength $Mask.string -DefaultGateway $Gateway

    !!  one of the solutions ... not both!  ;-) :-D

    I could imagine if you have more than one IP addresses on the interface you query you even have to choose one of it to use it later on.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Monday, January 28, 2019 7:49 AM
    • Marked as answer by Basheerssyed Monday, January 28, 2019 8:21 AM
    Monday, January 28, 2019 7:46 AM
  • Thank you very much!! Below is my updated script which worked perfectly fine!

    $IP = Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 | Select-Object -ExpandProperty IPAddress
    $Mask = Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 | Select-Object -ExpandProperty PrefixLength
    $Gateway = Get-WmiObject win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty DefaultIPGateway
    $IPType = "IPv4"
    $Dns = "8.8.8.8"
    $adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
    If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
     $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
    }
    If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
     $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
    }
    Set-NetIPInterface -DHCP Disabled
    $adapter | New-NetIPAddress -AddressFamily $IPType -IPAddress $IP -PrefixLength $Mask -DefaultGateway $Gateway
    $adapter | Set-DnsClientServerAddress -ServerAddresses $Dns

    Thanks again.


    Monday, January 28, 2019 8:21 AM
  • Please, format you code as code. Use the code posting tool provided on the edit bar of the post editor for it. It's not that hard.  :-/

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, January 28, 2019 8:25 AM
  • Done. I wasn't actually aware of it. Will take care from next time.

    Thanks.


    Monday, January 28, 2019 8:35 AM
  • Great. I'm proud of you.

    Thanks.  ;-) :-D


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, January 28, 2019 8:45 AM
  • hehe :)
    Monday, January 28, 2019 8:54 AM
  • Set DHCP to retain addresses for an "Infinite" time.  This setting does exactly what you are trying to do.  IPs will not be refreshed repeatedly and will not be lost when the DHCP server is unavailable.


    \_(ツ)_/

    Monday, January 28, 2019 10:45 AM
  • Thanks, Will consider this option as well.
    Monday, January 28, 2019 10:53 AM
  • Thanks, Will consider this option as well.

    That is how nearly all trained in networking would do it.  DHCP is designed to work as you need it to work you just need to learn how it is intended to be used.

    I haven't touched a static IP or manually configured and interface for over two decades. Doing this allows us to make global network changes without having to reconfigure any hosts, printers and other devices.  It works exactly like a static IP but can be remotely changed.


    \_(ツ)_/

    Monday, January 28, 2019 11:06 AM
  • Thanks, Will consider this option as well.

    That is how nearly all trained in networking would do it.  DHCP is designed to work as you need it to work you just need to learn how it is intended to be used.

    I haven't touched a static IP or manually configured and interface for over two decades. Doing this allows us to make global network changes without having to reconfigure any hosts, printers and other devices.  It works exactly like a static IP but can be remotely changed.


    \_(ツ)_/

    Thank you for the additional information. DHCP is currently managed by a separate team of ours who take care of openstack deployments where DHCP resides and I don't have enough control over it. I was given with this task to find out a way to avoid downtime in case of such failure while the other team is working on having redundant DHCP servers. Your suggestion is valuable and I will surely pass it to the team.
    Monday, January 28, 2019 11:13 AM
  • If the correct solution requires inter-departmental cooperation then you should escalate this through management.  The DHCP change is trivial and can be updated in minutes once the DHCP techs are tasked.

    \_(ツ)_/

    Monday, January 28, 2019 11:16 AM
  • I should also note that the DHCP people may have already forced your addresses to be protected and setting them as static can create other issues in the future.  Major network reconfigurations like you are planning must be validated through the network techs and designers to avoid conflict.


    \_(ツ)_/

    Monday, January 28, 2019 11:19 AM