Change DHCP IP to STATIC IP

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.

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?

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 - PowerShell and WMI - technique 102 page 349.

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

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{}

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 - TCP/IP Alternative Configurations: pt IV reset to static address | Richard Siddaway's Blog

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