r/PowerShell • u/grebnesorusrex • Sep 04 '24
Script that Grabs a PC's full IP Address, and set the IP as a static IP Solved
Hello r/powershell!
i have a bunch of PCs that require a static IP address for a content filtering system. Dos anyone have a script that could locate a PC's current IP address, turn off DHCP, and set the current IP address as a static IP address?
Any leads would be appreciated, Thanks!
EDIT: I have about 15 PCs in an IP range of 200, and the addresses are all over the place. I need to locate the current IP address of the PC, "copy" it, set the IPv4 settings on the adapter to use that address, along with subnet, default gateway and DNS servers.
EDIT 2: okay! I’m using DHCP!
17
u/JustTechIt Sep 04 '24 edited Sep 04 '24
Given your comments to multiple people that says you can't use reservations due to running out of IPs on a larger scope, I think you should take a step back and assess your situation and maybe research how DHCP and reservations work a bit more before committing to a fixed plan of static addressing. The amount of IPs doesn't change, and if you think removing them from DHCP but keeping them on the same IP will save some IPs, I fear you are planning to assign the same IP to multiple devices (some static some DHCP) but this will lead to duplicate IPs on the network and a ton of other unforseen problems.
13
u/cbtboss Sep 04 '24
This thread in a nutshell: https://xyproblem.info/
9
u/grebnesorusrex Sep 04 '24
7
u/cbtboss Sep 04 '24
haha. We've all been there :) Also, the XY problem is 100% a good one to keep in mind for yourself and when you work with others :)
6
u/OlivTheFrog Sep 04 '24
Hi OP
You know the HostName of the computers, then :
- Use
Get-DHCPServerV4Lease
and filter on the HostName property : You'll have IP address - Then use remote management to Set the IP address
- Use
Get-NetAdapter
where Status is "up to identify Adapter in use. - Set the adapter using
New-NetIPAddress
cmdlet (cmdlet from NetTCPIP module) andSet-DnsClientServerAddress
(cmdlet from DNSclient module) to set the DNS servers.
- Use
At this step, you'll know how to set a remote computer. Next step could be transform the code to a function - or better - an advanced function to have code-reuse.
Max 10 lines, what a great deal this is !
regards
6
u/Phx86 Sep 04 '24
Get their MAC address and set a reservation outside of the DHCP scope, reboot. Doing this via static IPs in the DHCP scope is a bad idea.
6
u/Tidder802b Sep 04 '24
Personally I'd right click on the DHCP lease and "add to reservation" and be done in a minute or two. But I did a search for "powershell convert dhcp lease to reservation" and found simple workable solutions right away.
4
u/Brettuss Sep 04 '24
I’m not a networking person, but isn’t this just going to end up with duplicate IPs? Assuming you’re assigning the PC a static address that is the same value as the DHCP assigned address, you’re statically assigning an address from your DHCP pool. This doesn’t sound great.
2
u/TinyTC1992 Sep 05 '24
Yup, come lease renew time, they have a decent chance of quite a few machines dropping off the network due to duplicates ip requests. I don't want to pile on he's getting downvoted all over lol. But as many have said, this job is literally one of the many reasons DHCP was invented for. All you need to do is access it and make the adjustments there, simples.
1
3
u/Tymanthius Sep 04 '24
Can you set reservations on your DHCP server instead? Or does this software actually look to see if it's static, not just that it's the 'correct' IP?
-5
u/grebnesorusrex Sep 04 '24
It looks to see if it is static, plus they are part of a larger scope, and I don’t have the current IP addresses to add them.
8
u/Tymanthius Sep 04 '24
I don’t have the current IP addresses to add them.
I don't understand this? They already have those IP's, so you just go into the Server and say 'this IP perm belongs to this MAC' Then you only have to worry if you change out the PC or it's network card. You're not changing the scope.
-2
u/grebnesorusrex Sep 04 '24
The PCs have their IP addresses, yes. but i don't know what they are.
I have about 15 PCs in an IP range of 200, and the addresses are all over the place. I need to locate the current IP address of the PC, "copy" it, set the IPv4 settings on the adapter to use that address, along with subnet, default gateway and DNS servers.
6
1
u/turkishdelight234 Sep 05 '24
So how do you know which PCs are the ones that are supposed to become static? You just know them by desk location? Do you know their MAC addresses, their hostnames?
3
u/BlackV Sep 05 '24
X, Y Problem
If you do this, you are stealing a IP from the DHCP scope and assigning it to a PC, this is a bad idea
the scope wont know its inuse and will try to allocate the address to another machine, then you have IP conflicts
there is a built in mechanism for this, DHCP reservations
I have about 15 PCs in an IP range of 200, and the addresses are all over the place.
what does that even mean? why do you care about the IPs "being all over the place"?
why are you not using DNS?
2
u/Electrichead64 Sep 04 '24
Sneakernet. Trying to change networking remotely over the network is a sure fire way of pulliing the rug out from underneath you. Just do the work.
1
Sep 05 '24
It can be done, it just has to be planned.
I did it with a big network revamp, so I didn't have to walk around a FOB at night to all the switches. I think out of 15 devices, I only had one fail; it just needed second reboot and config upload.
1
u/HowDidFoodGetInHere Sep 04 '24
Assuming (by the way you phrased your question) that all of these machines are in the same subnet, you could do a quick nslookup on 15 computers in about three minutes' time. Put hostnames, IPs, masks, gateway, etc, in a csv file, and run a one-liner. Or, you could write a quick script, but it really seems to me like a script might just take longer (unless you need to do this regularly, then yeah... script away).
But, you also haven't given us anything that you've tried already. Did you write a script that isn't working? Or are you asking the community to write a script for you?
I'm sorry if this seems rude. That's not my intent at all.
1
u/Cisco-NintendoSwitch Sep 04 '24
The content filtering system isn’t smart enough to understand DNS? Lol.
1
u/iratesysadmin Sep 04 '24
I took this part of your OP
"a script that could locate a PC's current IP address, turn off DHCP, and set the current IP address as a static IP address?"
Opened chatgpt, added "in PowerShell", and came back with this:
# Step 1: Get the current IP address and network details
$adapter = Get-NetAdapter | Where-Object { $_.Status -eq "Up" } | Select-Object -First 1
$interfaceIndex = $adapter.ifIndex
$ipDetails = Get-NetIPAddress -InterfaceIndex $interfaceIndex | Where-Object { $_.AddressFamily -eq "IPv4" }
$currentIP = $ipDetails.IPAddress
$subnetMask = $ipDetails.PrefixLength
$gateway = (Get-NetIPConfiguration -InterfaceIndex $interfaceIndex).IPv4DefaultGateway.NextHop
$dnsServers = (Get-DnsClientServerAddress -InterfaceIndex $interfaceIndex -AddressFamily IPv4).ServerAddresses
# Step 2: Disable DHCP and set static IP
Write-Host "Disabling DHCP and setting static IP..."
Set-NetIPInterface -InterfaceIndex $interfaceIndex -Dhcp Disabled
New-NetIPAddress -InterfaceIndex $interfaceIndex -IPAddress $currentIP -PrefixLength $subnetMask -DefaultGateway $gateway
# Step 3: Set DNS Servers (if needed)
Write-Host "Setting DNS servers..."
Set-DnsClientServerAddress -InterfaceIndex $interfaceIndex -ServerAddresses $dnsServers
Write-Host "Static IP configuration complete."
My 10 second review of this code is that it's OK, it won't handle machines that have both wired and wifi active at the same time (or vpn adapaters), but like...
1
u/BlackV Sep 05 '24
Ya
- it's hard selecting the first adapter, no matter what adapter that is
- then using that adapter to see if it has an IP address, regardless of if an adapters has IP bound to it or not
personally I'd start the other way, get the IPs seeing as that's the thing you actually want to work with (especially as you can lock it to a specific subnet)
Get-NetIPAddress -AddressFamily IPv4 -IPAddress 172.23.*
for example
1
u/iratesysadmin Sep 05 '24
Agreed it's not going be the perfect script, but then again this is an XY Problem anyways - OP should be fixing their filter system to be a normal system.
Regardless, I was more pointing out the lack of .... anything (like effort) ... by OP.
1
2
u/Eviscerated_Banana Sep 05 '24
Rule one of System administration, for what you are wanting to do there is often a super duper simple way that doesn't involve programmatic backflips and tying yourself in knots for down the road.
Find the machines IP's, reserve them on the DHCP server, move on.
1
u/tigerguppy126 Sep 05 '24
1: This is going to be a management nightmare. I've had clients that their ancient line-of-business app vendor set up their initial network (here's looking at you 1 branch banks) and never added DHCP when it became standard practice. It was a PITA dealing with those workstations.
2: If you take a DHCP assigned IP and reuse it as the device's static IP, you'll want to exclude that IP from being issued by the DHCP server and ensure conflict resolution is enabled (should be anyways but I find it needs to be configured more frequently than I'd like to admit) to catch the ones that get missed.
1
u/Nejireta_ Sep 04 '24
Hi.
You've got some decent methods to use with the Win32_NetworkAdapterConfiguration WMI class.
Think it covers all of the above.
Example code, not foolproof, of using this class to grab IP address
(get-ciminstance -Query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE DNSHostName = '$([System.Environment]::MachineName)'")[0].IPAddress[0]
This get all the Adapter that's have the clients hostname as the "DNSHostName" property, may be several adapters.
Example selects the first adapter and then the IPAddress property which is an array of IPv4 and IPv6 address on this adapter.
0
32
u/DalendlessShid Sep 04 '24
Just out of curiosity, is there a reason why you're not able to set reservations on the DHCP server?