Change DHCP IP to STATIC IP

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of jeevan b jeevan b 3 years ago.

  • Author
    Posts
  • #14159
    Profile photo of jeevan b
    jeevan b
    Participant

    Hello Don,

    I am looking for a script to change DHCP IP to static for multiple servers.

    the script i am trying it is just disconnecting the session.

  • #14162
    Profile photo of
    Anonymous

    Hi Jeevan,

    Eric here, Can you please provide what work you have done so far by pasting your script content in code tags in a reply so that we can review it?

  • #14163
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    What I suspect is happening is that you are attempting to this remotely and when you change the IP address on the remote server you break the network connection to that server including any remoting, CIM or DCOM connections. I covered this in PowerShell and WMI – http://www.manning.com/siddaway2/ – technique 102 page 349.

    If you can post the code you are using we can probably advise

  • #14246
    Profile photo of jeevan bobba
    jeevan bobba
    Participant

    Hello Eric. Richard

    below is the script i tried for changing DHCP ip to static. but it is not working.


    [cmdletbinding()]
    Param (
    [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
    )

    begin{}
    Process {
    foreach ($Computer in $ComputerName) {
    if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration
    -ComputerName $Computer | ? {$_.IPEnaled}
    foreach ($Network in $Networks) {
    $IPAddress = $Network.IpAddress[0]
    $SubnetMask = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers = $Network.DNSServerSearchOrder
    $IsDHCPEnabled = $false
    If($network.DHCPEnbled) {
    $IsDHCPEnabled = $true
    }
    $MACAddress = $Network.MACAddress
    $OutputObj = New-Object - Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name
    ComputerName -Value $Comouter.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress
    -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name
    SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway
    -Value $DefaultGateway
    $OutputObj | Add-Member -MemberType NoteProperty -Name
    IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers
    -Value $DNSServers
    $OutputObj | Add-Member -MemberType NoteProperty -Name
    MACAddress -Value $MACAddress
    $ObjectObj
    }
    }
    }
    }

    end{}

  • #14261
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    All your script is doing is collecting data on the network adapter configuration. I can't see any where that you are setting IP addresses. You have to use the methods on the WMI class to perform the change.

    Also you are building your output object in a very inefficient manner. This code is neater and quicker


    [cmdletbinding()]
    Param (
    [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername
    )
    begin{}
    Process {
    foreach ($Computer in $ComputerName) {
    if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer |
    where {$_.IPEnabled} |
    select PSComputerName, IpAddress, IPSubnet, DefaultIPGateway, DNSServerSearchOrder,
    DHCPEnabled, MacAddress
    }
    }
    }
    end{}

    To set the NIC to have a static IP address you need to use these methods

    EnableStatic
    SetGateways
    SetDNSServerSearchOrder

    You can examples of them in use here – http://richardspowershellblog.wordpress.com/2012/05/20/tcpip-alternative-configurations-pt-iv-reset-to-static-address/

  • #14665
    Profile photo of jeevan b
    jeevan b
    Participant

    I tryed that option but no luck. can you help me with some other methord

You must be logged in to reply to this topic.