Shahar Weiss
Shahar Weiss

Reputation: 149

Powershell Extract ip's from string

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?

Upvotes: 0

Views: 663

Answers (1)

AdamL
AdamL

Reputation: 13141

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{$_}

Upvotes: 2

Related Questions