• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

Scripting

Hi Experts

I have a situation in my work need to change the IP address for few windows 2003 and 2008 servers in C:\Program Files\App\opt.txt( this text file has IP address and that need be change in different IP) . the application work with IP address.
I have a servers list in the CSV or TXT file, I am looking for the PowerShell scrip and that need to execute from workstation. Script goes and changes the IP address in opt.txt in the program file.
Please help me with this scripting … much appreciate.

Thanks

0
Jey_P
Asked:
Jey_P
  • 3
  • 3
1 Solution
 
Chris DentPowerShell DeveloperCommented:
As in you need to read the current IP of the system and put it into that text file?

Chris
0
 
Jey_PIT Security Eng Author Commented:

thanks for your responce, no need to read the IP  and need to change the different IP .

There are one application server in our company and client installed every servers , the client will hold the application server IP in txt file located in C:\.  We are in situation to move the new VLAN and new IP. So instated of login each server and modify the IP, I am looking for the power shell command to modify the IP address number of servers in one shot. Is it possible to do it in PowerShell?  Sorry for the confusion.
0
 
Chris DentPowerShell DeveloperCommented:
Sure, it's not so hard. First, we get the right network interface. Does anything have more than one?

Then our code looks something like this:
$Network = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled=$True"
$Network.EnableStatic("New.IP.Address", "New.Subnet.Mask")
$Network.SetGateways("New.IP.Gateway")

Open in new window

Of course, it'll be a bit tricky doing this over the network, you'll lose connection the moment you change IP. But we can work with that if we make sure whatever script we run is running on the host machine, not over the network.

Will subnet mask and gateway be the same in all cases? And do you need to change any of the other details?

Chris
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Jey_PIT Security Eng Author Commented:
Thanks Chris.

we not going to change the host IP  . I want to replace the old IP to New IP that located in the C:\programfile\.txt .
0
 
Jey_PIT Security Eng Author Commented:
Can you please modify the script bit and help me to complete this task. Only I need to change the IP for application that installed in the txt file located (c:\program files\app\txt.), I am not change the host IP.
I have numerous hosts have same above situation to change the IP. (the IP in CSV format).
0
 
Chris DentPowerShell DeveloperCommented:
Sorry, lost track.

Perhaps we could do it like this.
$OldIP = "1\.2\.3\.4"
$NewIP = "2.3.4.5"

# Run through a list of servers
Get-Content YourList.txt | ForEach-Object {
  $File = Get-Content "\\$_\Program Files\App\opt.txt"
  $File -Replace $OldIP, $NewIP
  $File | Set-Content "\\$_\Program Files\App\opt.txt"
}

Open in new window

$OldIP above is a regular expression, that's why each of the dots has a \ before it. That format should remain, but the IP should be updated to reflect the one we're swapping out.

Does that do what you need?

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now