Skip to content

Instantly share code, notes, and snippets.

@Jonatantwn
Last active August 3, 2020 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonatantwn/4653cabfe8320cc461fe137c29591898 to your computer and use it in GitHub Desktop.
Save Jonatantwn/4653cabfe8320cc461fe137c29591898 to your computer and use it in GitHub Desktop.
$FileList = (Get-ChildItem 'C:\Users\Dern\Desktop\DNSSCAN\Batchtest').fullname
$c1 = 0
$lineamount =
$m = Get-Content -Path $FileList | Measure-Object -line
@($m).lines
$myOutput = @() # Declare empty array
foreach ($FileName in $FileList) {
$myOutput += foreach ($DomainName in (Get-Content $FileName)) {
try {
$NSRecords = Resolve-DnsName -Name $DomainName -Type NS -DnsOnly -EA 1 | Where-Object { $_.QueryType -eq 'NS' }
$DomainFinal = (Invoke-WebRequest -Uri $DomainName -UseBasicParsing -ErrorAction Continue).BaseResponse.ResponseUri.AbsoluteUri -replace "https://" -replace "http://" -replace "www." -replace ".dk/", ".dk" -replace ".com/", ".com"
[PSCustomObject]@{
Resolved = $true
Rmatch = $DomainName -in $Domainfinal
DomainName = $DomainName
Domainfinal = $DomainFinal
NameServer = $NSRecords.NameHost | select -first 1
'A-Record' = (Resolve-DnsName $DomainName -Type A).IPAddress | select -first 1
'MX-Record' = (Resolve-DnsName $DomainName -Type MX).NameExchange | select -first 1
Origin = $FileName
Status = $_.Exception.Message
}
$c1++
Write-Host "Processing $DomainName"
Write-Progress -Id 0 -Activity 'Checking servers' -Status "Processing $($c1) of $(@($m).lines)" -CurrentOperation $DomainName -PercentComplete (($c1 / @($m).lines) * 100)
} catch {
[PSCustomObject]@{
Origin = $FileName
Rmatch = $Rmatch
DomainName = $DomainName
Resolved = $false
NameServer = '?'
Domainfinal = '?'
'A-Record' = '?'
'MX-Record' = '?'
Status = $_.Exception.Message
}
}
}
}
$myOutput | Select-Object -Property DomainName,
@{n='Domainfinal'; e={$_.'Domainfinal' -join ', '}},
@{n='Rmatch'; e={$_.'Rmatch' -join ', '}},
Resolved,
@{n='NameServer';e={$_.NameServer -join ', '}},
@{n='A-Record'; e={$_.'A-Record' -join ', '}},
@{n='MX-Record'; e={$_.'MX-Record' -join ', '}},
Status,
Origin |
Export-Csv -NoTypeInformation -Path C:\Users\Dern\Desktop\DNSSCAN\Output\output.csv
# or
$myOutput | Out-GridView
@Jonatantwn
Copy link
Author

@dgoldman-msft

Hi again!

I got some feedback from Powershell.org aswell with the following solution that resolved the issue :-)

This is now the used script:

$FileList = (Get-ChildItem 'C:\Users\Dern\Desktop\DNSSCAN\Batchtest').fullname
$c1 = 0

$lineamount =
$m = Get-Content -Path $FileList | Measure-Object -line
@($m).lines

$myOutput = @() # Declare empty array
foreach ($FileName in $FileList) {
$myOutput += foreach ($DomainName in (Get-Content $FileName)) {
$c1++
Write-Host "Processing $DomainName"
Write-Progress -Id 0 -Activity 'Checking servers' -Status "Processing $($c1) of $(@($m).lines)" -CurrentOperation $DomainName -PercentComplete (($c1 / @($m).lines) * 100)
try {
$NSRecords = Resolve-DnsName -Name $DomainName -Type NS -DnsOnly -EA 1 | Where-Object { $.QueryType -eq 'NS' }
try {
$DomainFinal = (Invoke-WebRequest -Uri $DomainName -UseBasicParsing -EA 1).BaseResponse.ResponseUri.AbsoluteUri
$DomainFinal = $DomainFinal -replace 'https://' -replace 'http://' -replace 'www.' -replace '.dk/','.dk' -replace '.com/','.com'
# Both command successful:
[PSCustomObject]@{
Resolved = $true
Rmatch = $DomainName -in $Domainfinal
DomainName = $DomainName
Domainfinal = $DomainFinal
NameServer = $NSRecords.NameHost | select -first 1
'A-Record' = (Resolve-DnsName $DomainName -Type A).IPAddress | select -first 1
'MX-Record' = (Resolve-DnsName $DomainName -Type MX).NameExchange | select -first 1
Origin = $FileName
Status = 'OK'
}
} catch {
# Resolve-DNSName successful, but Invoke-WebRequest failed:
[PSCustomObject]@{
Resolved = $true
Rmatch = '?'
DomainName = $DomainName
Domainfinal = '?'
NameServer = $NSRecords.NameHost | select -first 1
'A-Record' = (Resolve-DnsName $DomainName -Type A).IPAddress | select -first 1
'MX-Record' = (Resolve-DnsName $DomainName -Type MX).NameExchange | select -first 1
Origin = $FileName
Status = $
.Exception.Message
}
}
} catch {
# Resolve-DNSName command failed:
[PSCustomObject]@{
Resolved = $false
Rmatch = '?'
DomainName = $DomainName
Domainfinal = '?'
NameServer = '?'
'A-Record' = '?'
'MX-Record' = '?'
Origin = $FileName
Status = $_.Exception.Message
}
}
}
}

$myOutput | Select-Object -Property DomainName,
@{n='Domainfinal'; e={$.'Domainfinal' -join ', '}},
@{n='Rmatch'; e={$
.'Rmatch' -join ', '}},
Resolved,
@{n='NameServer';e={$.NameServer -join ', '}},
@{n='A-Record'; e={$
.'A-Record' -join ', '}},
@{n='MX-Record'; e={$_.'MX-Record' -join ', '}},
Status,
Origin |
Export-Csv -NoTypeInformation -Path C:\Users\Dern\Desktop\DNSSCAN\Output\output.csv

or

$myOutput | Out-GridView

@dgoldman-msft
Copy link

dgoldman-msft commented Jul 31, 2020 via email

@Jonatantwn
Copy link
Author

So am i!

Thanks for assisting :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment