# Fixing reverse DNS

**URL:** <https://forums.powershell.org/t/fixing-reverse-dns/15234>\
**Category:** PowerShell Help\
**Created:** [November 15, 2020, 9:58am UTC](https://forums.powershell.org/t/fixing-reverse-dns/15234 "2020-11-15T09:58:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![psinha4569](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psinha4569/32/4164_2.png) [@psinha4569](https://forums.powershell.org/u/psinha4569)\
**Post date:** [November 15, 2020, 9:58am UTC](https://forums.powershell.org/t/fixing-reverse-dns/15234/1 "2020-11-15T09:58:28Z")

</div>

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](http://workstation1.famoso.com),169.120  
[workstation2.famoso.com](http://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](http://workstation1.famoso.com),120.169  
[workstation2.famoso.com](http://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](http://workstation1.famoso.com),120.169  
[workstation2.famoso.com](http://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/]

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [November 16, 2020, 9:18am UTC](https://forums.powershell.org/t/fixing-reverse-dns/15234/2 "2020-11-16T09:18:59Z")

</div>

```
#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).

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 16, 2020, 2:34pm UTC](https://forums.powershell.org/t/fixing-reverse-dns/15234/3 "2020-11-16T14:34:57Z")

</div>

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'
    }
}
```

---

<div class="post-metadata">

**Author:** ![psinha4569](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psinha4569/32/4164_2.png) [@psinha4569](https://forums.powershell.org/u/psinha4569)\
**Post date:** [November 17, 2020, 5:19pm UTC](https://forums.powershell.org/t/fixing-reverse-dns/15234/4 "2020-11-17T17:19:09Z")

</div>

Thanks, both of these examples worked for me.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/fixing-reverse-dns/15234/5 "2024-05-16T20:28:20Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


