0

I would like to get from the user 2 variables: $ip1 $ip2

and run a loop between their IP address. for example:

$ip1 = 192.168.1.10   
$ip2 = 192.168.1.200

The script assume its a class C, the i need to strip everything and get 3 variables:

$IP = 192.168.1.
$FirstIP = 10
$LastIp = 200

At first i thought about substring ($ip.length - 3), that could give me XXX, .XX, X.X (Where X is a number) Depends on the last Octat.. Any idea how can it be done nicely?

3
  • 2
    I personally like using -replace : '192.168.1.100' -replace '\.\d+$' Commented Jun 3, 2020 at 8:22
  • First: Thank you very much it works :), if its not trouble, can you translate to words what it means: '\.\d+$' ? Commented Jun 3, 2020 at 8:29
  • 1
    Yes. It is regex match for literal . (\.) and a digit (\d) one or more times (+) followed by end of string ($) Commented Jun 3, 2020 at 8:34

1 Answer 1

2

You could split the string by dot and use range operator to create an array from last elements:

$ip1.Split('.')[-1]..$ip2.Split('.')[-1] | foreach{$_}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.