Last active
August 3, 2020 08:39
-
-
Save Jonatantwn/4653cabfe8320cc461fe137c29591898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 |
That will do it. That was around the solution I was going to provide to you once I had your samples with it being in a broken state so I could tell you why. I am glad you got it figured out1
Dave
From: Jonatantwn <notifications@github.com>
Sent: Friday, July 31, 2020 5:31 AM
To: Jonatantwn <Jonatantwn@noreply.github.com>
Cc: Dave <dgoldman@dgoldman.org>; Mention <mention@noreply.github.com>
Subject: Re: Jonatantwn/Powershell scanning tool - website DNS data
@Jonatantwn commented on this gist.
…________________________________
@dgoldman-msft<https://github.com/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
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://gist.github.com/4653cabfe8320cc461fe137c29591898#gistcomment-3399222>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJQPRPFTADO3GLOVWBOUZ2TR6KFMBANCNFSM4PODB6TA>.
So am i!
Thanks for assisting :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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$c1) of $ (@($m).lines)" -CurrentOperation $DomainName -PercentComplete (($c1 / @($m).lines) * 100)$DomainName -Type NS -DnsOnly -EA 1 | Where-Object { $ .QueryType -eq 'NS' }
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 $(
try {
$NSRecords = Resolve-DnsName -Name
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