Hitta felaktiga DNS-poster i Azure DNS – PowerShell-skriptexempel
Följande Azure PowerShell-skript hittar felaktiga DNS-poster i offentliga Azure DNS-zoner.
Kommentar
Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Information om hur du kommer igång finns i Installera Azure PowerShell. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.
<#
1. Install Pre requisites Az PowerShell modules (/powershell/azure/install-az-ps)
2. Sign in to your Azure Account using Login-AzAccount or Connect-AzAccount.
3. From an elevated PowerShell prompt, navigate to folder where the script is saved and run the following command:
.\ Get-AzDNSUnhealthyRecords.ps1 -SubscriptionId <subscription id> -ZoneName <zonename>
Replace subscription id with the subscription id of interest.
Replace ZoneName with the actual zone name.
#>
param(
# subscription if to fetch dns records from
[String]$SubscriptionId = "All",
#filtering zone name
[String]$ZoneName = "All"
)
if ($SubscriptionId -eq "All") {
Write-Host -ForegroundColor Yellow "No subscription Id passed will process all subscriptions"
}
if ($ZoneName -eq "All") {
Write-Host -ForegroundColor Yellow "No Zone name passed will process all zones in subscription"
}
$ErrorActionPreference = "Stop"
$AZModules = @('Az.Accounts', 'Az.Dns')
$AzLibrariesLoadStart = Get-Date
$progressItr = 1;
$ProgessActivity = "Loading required Modules";
$StoreWarningPreference = $WarningPreference
$WarningPreference = 'SilentlyContinue'
Foreach ($module in $AZModules) {
$progressValue = $progressItr / $AZModules.Length
Write-Progress -Activity $ProgessActivity -Status "$module $($progressValue.ToString('P')) Complete:" -PercentComplete ($progressValue * 100)
If (Get-Module -Name $module) {
continue
}
elseif (Get-Module -ListAvailable -Name $module) {
Import-Module -name $module -Scope Local -Force
}
else {
Install-module -name $module -AllowClobber -Force -Scope CurrentUser
Import-Module -name $module -Scope Local -Force
}
$progressItr = $progressItr + 1;
If (!$(Get-Module -Name $module)) {
Write-Error "Could not load dependant module: $module"
throw
}
}
$WarningPreference = $StoreWarningPreference
Write-Progress -Activity $ProgessActivity -Completed
$context = Get-AzAccessToken;
if ($context.Token -eq $null) {
Write-host -ForegroundColor Yellow "Please sign in to your Azure Account using Login-AzAccount or Connect-AzAccount before running the script."
exit
}
$subscriptions = Get-AzSubscription
if ($SubscriptionId -ne "All") {
$subscriptions = $subscriptions | Where-Object { $_.Id -eq $SubscriptionId }
if ($subscriptions.Count -eq 0) {
Write-host -ForegroundColor Yellow "Provided Subscription Id not found exiting."
exit
}
}
$scount = $subscriptions | Measure-Object
Write-Host "Subscriptions found $($scount.Count)"
if ($scount.Count -lt 1) {
exit
}
$InvalidItems = @()
$TotalRecCount = 0;
$ProgessActivity = "Processing Subscriptions";
$progressItr = 1;
$subscriptions | ForEach-Object {
$progressValue = $progressItr / $scount.Count
Select-AzSubscription -Subscription $_ | Out-Null
Write-Progress -Activity $ProgessActivity -Status "current subscription $_ $($progressValue.ToString('P')) Complete:" -PercentComplete ($progressValue * 100)
$progressItr = $progressItr + 1;
$subscription = $_
try {
$dnsZones = Get-AzDnsZone -ErrorAction Continue
}
catch {
Write-Host "Error retrieving DNS Zones for subscription $_"
return;
}
if ($ZoneName -ne "All") {
$dnsZones = $dnsZones | Where-Object { $_.Name -eq $ZoneName }
if ($dnsZones.Count -eq 0) {
Write-host -ForegroundColor Yellow "Provided ZoneName $ZoneName not found in Subscription $_."
return;
}
}
$dnsZones | ForEach-Object {
$allrecs = Get-AzDnsRecordSet -Zone $_
$sZoneName = $_.Name
$nsrecords = $allrecs | Where-Object { $_.RecordType -eq "NS" }
$records = $allrecs | Where-Object { ($_.RecordType -ne 'NS' ) -or ($_.Name -ne '@' ) }
$records | ForEach-Object {
$rec = $_
$Invalid = $false
$endsWith = "*$($rec.Name)"
$nsrecords | ForEach-Object { if ($endsWith -like "*.$($_.Name)") { $Invalid = $true } }
$TotalRecCount++
if ($Invalid) {
Write-Host -ForegroundColor Yellow "$($rec.Name) recordType $($rec.RecordType) zoneName $sZoneName subscription $subscription"
$hash = @{
Name = $rec.Name
RecordType = $rec.RecordType
ZoneName = $sZoneName
subscriptionId = $subscription
}
$item = New-Object PSObject -Property $hash
$InvalidItems += $item
}
else {
# Write-Host -ForegroundColor Green "$($rec.Name) recordType $($rec.RecordType) zoneName $ZoneName subscription $subscription "
}
}
}
}
Write-Progress -Activity $ProgessActivity -Completed
Write-Host "Total records processed $TotalRecCount"
$invalidMeasure = $InvalidItems | Measure-Object
Write-Host "Invalid Count $($invalidMeasure.Count)"
Write-Host "Invalid Records "
Write-Host "==============="
$InvalidItems | Format-Table
Förklaring av skript
Det här skriptet använder följande kommandon för att skapa distributionen. Varje post i tabellen länkar till kommandospecifik dokumentation.
Command | Kommentar |
---|---|
Get-AzDnsZone | Hämtar en offentlig DNS-zon i Azure. |
Get-AzDnsRecordSet | Hämtar en DNS-postuppsättning. |
Nästa steg
Mer information om Azure PowerShell-modulen finns i Azure PowerShell-dokumentationen.