Skip to content

Instantly share code, notes, and snippets.

@Jonatantwn
Last active August 3, 2020 08:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
@dgoldman-msft
Copy link

dgoldman-msft commented Jul 30, 2020

If I may offer a few suggestions to help ya out. I had to do something similar in my code. I would consider using try/catches for both of your statements for Resolve-DNSName and Invoke-WebRequest. I am not a huge fan of try/catch for all code. This way if the first one is a success in resolving the DNS name it can still fail out on the invoke-WebRequest and you can just break it there since you are expecting that type of failure, or add additional flow conditions to handle other types of possible failures like 401, 403, 500, etc. You also have two parameters on the Invoke-WebRequsest that you can use as well -SkipHttpErrorCheck and -Timeout that might be a benefit.

@Jonatantwn
Copy link
Author

Hi there mate!

I tried adding -SkipHttpErrorCheck to the invoke-webrequest, but that just returns me A parameter cannot be found that matches parameter name 'SkipHttpErrorCheck :/

to be honest i am quite new to powershell and i am struggling somewhat to understand the steps you suggest.

Can you give me a short example of how to implement it? :-)

@Jonatantwn
Copy link
Author

If I may offer a few suggestions to help ya out. I had to do something similar in my code. I would consider using try/catches for both of your statements for Resolve-DNSName and Invoke-WebRequest. I am not a huge fan of try/catch for all code. This way if the first one is a success in resolving the DNS name it can still fail out on the invoke-WebRequest and you can just break it there since you are expecting that type of failure, or add additional flow conditions to handle other types of possible failures like 401, 403, 500, etc. You also have two parameters on the Invoke-WebRequsest that you can use as well -SkipHttpErrorCheck and -Timeout that might be a benefit.

@dgoldman-msft

@dgoldman-msft
Copy link

dgoldman-msft commented Jul 30, 2020

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?

@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