Author Posts

October 29, 2015 at 1:37 pm

I need some help on reordering my string value for ReverseDNS.

Currently String: 75.20.24.10
I want it to be: 10.24.20.75

I tried use regex to switch the string in reverse but it does all the values:
ex: 01.42.02.75

October 29, 2015 at 1:51 pm

split and then reassemble in reverse

$IPOctects = "75.20.24.10" -split "\."
$IP = "$($IPOctects[3]).$($IPOctects[2]).$($IPOctects[1]).$($IPOctects[0])"
$IP
10.24.20.75

October 29, 2015 at 1:56 pm

Thanks Curtis, that's exactly what I was trying to accomplish

October 29, 2015 at 1:58 pm

Or split into an array, reverse the array, and join string again

PS C:\> $String = '75.20.24.10'
PS C:\> $String
75.20.24.10

PS C:\> $Array = $String.Split('.')
PS C:\> [array]::Reverse($Array)
PS C:\> $Reversed = $Array -join '.'

PS C:\> $Reversed
10.24.20.75

October 29, 2015 at 2:01 pm

Or

$IP = "75.20.24.10".split(".")
[array]::Reverse($IP)
$IP -join "."
10.24.20.75

October 29, 2015 at 2:02 pm

ah, bet me to the other option Christian 😀

October 29, 2015 at 2:19 pm

I was actually going to suggest the reverse method also. However I got stuck for a couple of minutes due to the fact that the static reverse method does not return anything (void) and the signature does not suggest that is takes a [ref] variable ( static void Reverse(array array)). As it is a void, it should not return anything, however how does that work without [ref]?

October 29, 2015 at 2:21 pm

Is there a way to have the input as a variable? Instead of a static entry of 75.20.24.10, the variable may contain more than one entry.

$IPs = '75.20.24.10','76.20.24.10'

October 29, 2015 at 2:44 pm

but of course there is. Here is an advanced function that supports pipeline input as well:

Cheers

Tore