Skip to content

Instantly share code, notes, and snippets.

@thechainercygnus
Created June 30, 2021 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thechainercygnus/7968dbcae29a35298fbffd91f5257223 to your computer and use it in GitHub Desktop.
Save thechainercygnus/7968dbcae29a35298fbffd91f5257223 to your computer and use it in GitHub Desktop.
Simple script to check IP Address against a target IP and stop a service if there is a match.
# Set these to match your needs
$ServiceTarget = Get-Service -Name ""
$MatchTarget = "10","15","86"
# This big Foreach is just looping through every interface on the machine looking for names like "Ethernet"
# If you need to prioritize a different interface, or change interface names on systems, update the -Like string accordingly.
foreach ($Interface in (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -like "Ethernet*"})) {
# Because we're working with numbers, convert each octet to an int and stick it in an array.
$IPArray = ($Interface.IPAddress).Split(".") | ForEach-Object {[int]$_}
# Loop through the $MatchTarget count and evaluate Target vs Actual. Increment $MatchCount if match.
for ($i = 0; $i -lt $MatchTarget.Count; $i++) {
if ($IPArray[$i] -eq $MatchTarget[$i]) {
$MatchCount += 1
}
}
# If the Match is good, stop the service. Can be changed to start, for other uses.
if ($MatchCount = $MatchTarget.Count) {
Stop-Service $ServiceTarget
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment