PowerShell script to replace 3rd octet of IP and mask on a group of computers.
-
Thursday, December 06, 2012 10:29 PM
We have a need to change the third octet of the IP address and the subnet mask on a large group of computers, the computer names can be provided from a file and these are statically assigned IP configurations.
I would like to use PowerShell as much as possible so I searched but did not find any script with this exact combination. Can someone please point me in the right direction, or perhaps suggest another practical method.
thank you,
All Replies
-
Thursday, December 06, 2012 10:49 PMModerator
The best way would be to use DHCP and not use a script at all.
Bill
- Proposed As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Monday, December 10, 2012 7:27 PM
- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Tuesday, January 22, 2013 9:18 PM
-
Thursday, December 06, 2012 11:48 PM
We have a need to change the third octet of the IP address and the subnet mask on a large group of computers, the computer names can be provided from a file and these are statically assigned IP configurations.
I would like to use PowerShell as much as possible so I searched but did not find any script with this exact combination. Can someone please point me in the right direction, or perhaps suggest another practical method.
thank you,
What have you tried?
Dpoing this remotely is almost never successful. Whiul you are at it harvest teh MAC addresses and set up reservations in DHCP under ethe new IP subnet then use one of teh many scripts here to reset teh adapters to use DHCP. Yuo will need to test the setiup carefully to weed out any mistakes. After that it will be painless.
WMI has EnableStatic can update the address.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa390383(v=vs.85).aspx
¯\_(ツ)_/¯
-
Tuesday, December 11, 2012 11:57 PM
Unfortunately that is not possible, although these are workstations they all have static IPs and that cannot change to DHCP, it looks like we will have to do it the old fashion way.
thanks,
-
Wednesday, December 12, 2012 12:03 AM
I tested the following script to change the Gateway and Mask but encountered the "RPC server is unavailable..." error.
$VMs = Get-VM | Where {$_.Name -ilike 'VM1'}
$Gw = "10.252.228.1"
$SubnetMask = "255.255.248.0"foreach ($vm in $VMs) {
$IP = (Get-VMGuest -VM $vm).IPAddress
$NIC = Get-WMIObject -computername $vm.Name Win32_NetworkAdapterConfiguration | `
where {$_.DHCPEnabled -ilike "False" -and $_.IPAddress -ne $null}
$NIC.SetGateways($Gw)
$NIC.EnableStatic($IP, $SubnetMask)
}The above script is a PowerCLI script, as I mentioned, tried one line at a time but the "Get-WMIObject" line fails with the RPC error.
I will check your suggestions above.
thank you,
-
Wednesday, December 12, 2012 12:06 AM
Unfortunately that is not possible, although these are workstations they all have static IPs and that cannot change to DHCP, it looks like we will have to do it the old fashion way.
thanks,
Yeah - Everyone used to say that until we showed them how to use DHCP correctly. Once a year or less I meet an Admin or Tech who knows how to set up fied addresses by MAC. All modern system accept this.
¯\_(ツ)_/¯
-
Wednesday, December 12, 2012 6:46 PMModerator
Unfortunately that is not possible ... that cannot change to DHCP
Why is it not possible to use DHCP?
Bill
-
Thursday, January 03, 2013 12:55 AM
DHCP is not an option at this point. I was looking for a specific answer though. Thank you.
-
Thursday, January 03, 2013 12:56 AM
Sorry, I cannot discuss the reasons publicly. I agree that DHCP would be a lot easier, just not an option at this point.
thank you,
-
Thursday, January 03, 2013 2:16 AM
You can use WMI and substring functions to replace the third octet. This can be done in POwerSHell, VBScript, batch and other scritping languages.
Happy New Year ¯\_(ツ)_/¯
-
Thursday, January 03, 2013 3:06 PMModerator
Hi,
I doubt someone will write the needed code for you to do this, especially since there's already a recommended and supported way to do what you want (DHCP). Remember that this isn't a free code-dispensing service.
Bill
- Edited by Bill_StewartMicrosoft Community Contributor, Moderator Thursday, January 03, 2013 3:07 PM

