<#
Gets the various dns record type for a domain or use -RecordType all for all and -Report to file output.
Use like .get-dnsrecords.ps1 -Name Facebook -RecordType all or .get-dnsrecords.ps1 -name facebook -RecordType MX
#>
param(
[Parameter(Mandatory = $True,
ValueFromPipelineByPropertyName = $True,
HelpMessage = "Specifies the domain.")]
[string]$Name,
[Parameter(Mandatory = $False,
HelpMessage = "Which Record.")]
[ValidateSet('A', 'MX', 'TXT', 'All')]
[string]$RecordType = 'txt',
[Parameter(Mandatory = $false,
HelpMessage = "DNS Server to use.")]
[string]$Server = '8.8.8.8',
[Parameter(Mandatory = $false,
HelpMessage = "Make a csv report in c:Reports")]
[Switch]$Report
)
IF ($Name -notlike "*.*") {
$Name = $Name + '.com'
}
If ($Report) {
$filename = [environment]::getfolderpath("mydocuments") + '' + $($RecordType) + '-' + ($Name.Split('.')[0]) + '.csv'
}
If ($RecordType -eq 'txt' -or $RecordType -eq 'All') {
$TXTRecord = Resolve-DnsName $Name -Type txt -Server $Server -ErrorAction Stop | ForEach-Object {
[PSCustomObject][ordered]@{
name = $_.name
type = $_.Type
ttl = $_.ttl
section = $_.Section
strings = ($_.strings | Out-String).trim()
}
}
If ($RecordType -eq 'txt') {
$TXTRecord
If ($Report) {
$TXTRecord | Export-Csv $filename -NoTypeInformation -Delimiter ','
}
$TXTRecord
Return write-host $filename -ForegroundColor blue
}
}
If ($RecordType -eq 'mx' -or $RecordType -eq 'All' ) {
$MXRecord = Resolve-DnsName $Name -Type mx -Server $Server -ErrorAction Stop | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
NameExchange = $_.nameexchange
}
}
If ($RecordType -eq 'MX') {
If ($Report) {
$MXRecord | Export-Csv $filename -NoTypeInformation
}
$MXRecord
Return Write-Host $filename -ForegroundColor blue
}
}
If ($RecordType -eq 'a' -or $RecordType -eq 'All' ) {
$ARecord = Resolve-DnsName $Name -Type A -Server $Server -ErrorAction Stop | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
IP4Address = $_.IP4Address
}
}
If ($RecordType -eq 'a') {
If ($Report) {
$ARecord | Export-Csv $filename -NoTypeInformation
}
$ARecord
Return write-host $filename -ForegroundColor blue
}
}
If ($Report) {
$TXTRecord | Export-Csv $filename -NoTypeInformation
$MXRecord | Select-Object name, Type, ttyl, section, NameExchange | Export-Csv $filename -NoTypeInformation -Append -Force
$ARecord | Select-Object name, Type, ttyl, section, IP4Address | Export-Csv $filename -NoTypeInformation -Append -Force
}
$TXTRecord
$MXRecord
$ARecord
Return Write-Host $filename -ForegroundColor blue
运行.get-dnsrecords.ps1 -Name Facebook -RecordType all -Report
示例报告输出文件如下所示,主机爆炸也很难看。
"name","type","ttl","section","strings"
"Facebook.com","TXT","3600","Answer","String"
"Facebook.com","TXT","3600","Answer","String"
"FaceBook.com","MX","21001","Answer",
"FaceBook.com","MX","21001","Answer",
"FaceBook.com","A","20","Answer",
Mx缺少NameExchange
记录缺失IP4Address
关于如何使报告输出包含缺失项和奖金的想法如何使主机输出更具可读性?
问题是当我试图在最后组合输出变量,然后导出到文件。我只是不知道如何纠正它。
我是这样做的。如果有人有更好的答案,请告诉我。
运行.get-dnsrecords.ps1 -Name Facebook -RecordType all -Report
关键行如下:
$final = $TXTRecord | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
strings = $_.strings
NameExchange = $_.nameexchange
IP4Address = $_.IP4Address
}
}
$final += $MXRecord | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
strings = $_.strings
NameExchange = $_.nameexchange
IP4Address = $_.IP4Address
}
}
$final += $ARecord | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
strings = $_.strings
NameExchange = $_.nameexchange
IP4Address = $_.IP4Address
}
}
$final | Export-Csv $filename -NoTypeInformation
下面的完整代码
<#
Gets the various dns record type for a domain or use -RecordType all for all and -Report to file output.
Use like .get-dnsrecords.ps1 -Name Facebook -RecordType all or .get-dnsrecords.ps1 -name facebook -RecordType MX
#>
param(
[Parameter(Mandatory = $True,
ValueFromPipelineByPropertyName = $True,
HelpMessage = "Specifies the domain.")]
[string]$Name,
[Parameter(Mandatory = $False,
HelpMessage = "Which Record.")]
[ValidateSet('A', 'MX', 'TXT', 'All')]
[string]$RecordType = 'all',
[Parameter(Mandatory = $false,
HelpMessage = "DNS Server to use.")]
[string]$Server = '8.8.8.8',
[Parameter(Mandatory = $false,
HelpMessage = "Make a csv report in c:Reports")]
[Switch]$Report
)
IF ($Name -notlike "*.*") {
$Name = $Name + '.com'
}
If ($Report) {
$filename = [environment]::getfolderpath("mydocuments") + '' + $($RecordType) + '-' + ($Name.Split('.')[0]) + '.csv'
}
If ($RecordType -eq 'txt' -or $RecordType -eq 'All') {
$TXTRecord = Resolve-DnsName $Name -Type txt -Server $Server -ErrorAction Stop | ForEach-Object {
[PSCustomObject][ordered]@{
name = $_.name
type = $_.Type
ttl = $_.ttl
section = $_.Section
strings = ($_.strings | Out-String).trim()
}
}
If ($RecordType -eq 'txt') {
$TXTRecord
If ($Report) {
$TXTRecord | Export-Csv $filename -NoTypeInformation -Delimiter ','
}
$TXTRecord
Return write-host $filename -ForegroundColor blue
}
}
If ($RecordType -eq 'mx' -or $RecordType -eq 'All' ) {
$MXRecord = Resolve-DnsName $Name -Type mx -Server $Server -ErrorAction Stop | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
NameExchange = $_.nameexchange
}
}
If ($RecordType -eq 'MX') {
If ($Report) {
$MXRecord | Export-Csv $filename -NoTypeInformation
}
$MXRecord
Return Write-Host $filename -ForegroundColor blue
}
}
If ($RecordType -eq 'a' -or $RecordType -eq 'All' ) {
$ARecord = Resolve-DnsName $Name -Type A -Server $Server -ErrorAction Stop | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
IP4Address = $_.IP4Address
}
}
If ($RecordType -eq 'a') {
If ($Report) {
$ARecord | Export-Csv $filename -NoTypeInformation
}
$ARecord
Return write-host $filename -ForegroundColor blue
}
}
If ($Report) {
$final = $TXTRecord | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
strings = $_.strings
NameExchange = $_.nameexchange
IP4Address = $_.IP4Address
}
}
$final += $MXRecord | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
strings = $_.strings
NameExchange = $_.nameexchange
IP4Address = $_.IP4Address
}
}
$final += $ARecord | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Type = $_.type
TTL = $_.ttl
Section = $_.section
strings = $_.strings
NameExchange = $_.nameexchange
IP4Address = $_.IP4Address
}
}
$final | Export-Csv $filename -NoTypeInformation
}
$TXTRecord
$MXRecord
$ARecord
Return Write-Host $filename -ForegroundColor blue