-
-
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 |
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 $(
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
So am i!
Thanks for assisting :-)
Ok so the first question I have is what version of PowerShell are you using? Windows PowerShell 5, 6 or 7? This matters because depending on the version some commands might have changed.
You have one try catch for both functions, however Resolve-DNSName returns a DnsRecord object and Invoke-WebRequest returns a BasicHtmlWebResponseObject object. In your catch you are handling both outputs with a PSCustomObject that won't line up. I would wrap both of them in a try catch so you can see what is coming back from both objects on the return and then process from there if you have good data
For the try catches I would try this:
try { $NSRecords = Resolve-DnsName -Name$DomainName -Type NS -DnsOnly -EA 1 | Where-Object { $ .QueryType -eq 'NS' }
}
catch{
[PSCustomObject]@{
Origin = $FileName
Rmatch = $Rmatch
DomainName = $DomainName
Resolved = $false
NameServer = '?'
Domainfinal = '?'
'A-Record' = '?'
'MX-Record' = '?'
Status = $.Exception.Message
}
try{
$DomainFinal = (Invoke-WebRequest -Uri $DomainName -UseBasicParsing -ErrorAction SilentlyContinue).BaseResponse.ResponseUri.AbsoluteUri -replace "https://" -replace "http://" -replace "www." -replace ".dk/", ".dk" -replace ".com/", ".com"
}
catch{ Do something for your IWR error where you can print out your StatusCode, etc.
}
Note by default PowerShell will set the $ErrorActionPreference to Continue which will just log errors to the console. This is ok because this is a non-terminating error. You have the opton to change that to -Stop which will force a terminating error, hit the catch to do something so you can get more information about your error so we know how to handle it properly which will get you around the issue.
Can you provide me what your Bachtest looks like for testing?