Fixing reverse DNS

Hi, I’m trying to fix our missing DNS Reverse lookups with Powershell.

I have a CSV file with the headers ‘Name’ and last two octets of an ‘IP’ address that looks like below:

name,ip
workstation1.famoso.com,169.120
workstation2.famoso.com,169.121

My goal is to reverse the IP value so it displays as 120.169

Then create a new object with the Name and reversed IP.

name,iprev
workstation1.famoso.com,120.169
workstation2.famoso.com,121.169

Then import the Name and reversed IP using the command below
[crayon-5fb49a6b2a73d134542913/]
The problem I am having is creating the new object with Name and reversed IP so I can capture that in a variable.

I figured out how to reverse the IP:
[crayon-5fb49a6b2a745009075351/]
How can i create a new object with Name and IPrev that looks like below?

name,IPrev
workstation1.famoso.com,120.169
workstation2.famoso.com,121.169

I would like to capture that to a variable $servers1 and | to Add-DnsServerResourceRecordPtr

****the commands below can add, get, and remove a single PTR record from DNS
[crayon-5fb49a6b2a747983151124/]

#Creating Test Environment
$yourcsv = New-TemporaryFile
@'
name,ip
workstation1.famoso.com,169.120
workstation2.famoso.com,169.121
'@ |
Set-Content $yourcsv.FullName

# Create $servers objects with 3rd and 4th octet reversed.
$servers = Import-Csv $yourcsv |
            Select-Object -Property name, @{name="ip";e={($_.ip -split '\.')[1] + "." + ($_.ip -split '\.')[0]}}

In this example I used the -split operator to split the octets into an array then reversed the last two. The backslash is required because we want the literal . and it is a regular expression (. is any character).

Here’s another option using regular expression

$csv | ForEach-Object {
    [PSCustomObject]@{
        Name  = $_.name
        Iprev = $_.iprev -replace '(\d{3}).(\d{3})','$2.$1'
    }
}

Note the period matches any character. We could’ve escaped it to be a literal period as well

$csv | ForEach-Object {
    [PSCustomObject]@{
        Name  = $_.name
        Iprev = $_.iprev -replace '(\d{3})\.(\d{3})','$2.$1'
    }
}

Thanks, both of these examples worked for me.