<#
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
關于如何使報告輸出包括缺少的專案和獎勵的想法如何使主機輸出更具可讀性?
問題是當我嘗試在最后組合輸出變數然后匯出到檔案時。我只是不知道如何糾正它。
uj5u.com熱心網友回復:
這是我所做的。如果有人有更好的答案,請告訴我。
跑步 .\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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/346009.html
標籤:电源外壳
上一篇:如何從另一臺服務器中的Powershell連接到LDAP
下一篇:命令列倒數計時器跳過一秒
