PowerShell Scripts DNS_DHCP_Cleanup
I denne scriptpakken er det seks Powershell script og to Batch script. Batch scriptene
brukes av Powershell scriptene til å utføre enkelte handlinger. Tre av Powershell scriptene
er "Check" script som samler informasjon til bruk for "Clean" scriptene,
som er de andre tre av de seks Powershell scriptene.
Se kommentarene i scriptene for mer informasjon.
DNSDEL.bat |
---|
::echo "Confirm Yes or No (Y/N)."
dnscmd %3 /NodeDelete %1 %2 /tree /f 2>> Z:\Rydd_DNS\DNS-err.log >> Z:\Rydd_DNS\DNS-OK.log
IF %ERRORLEVEL%==0 GOTO EOF
echo "DNS ble ikke slettet med argumentene %1 og %2, og dns-server: %3." >> Z:\Rydd_DNS\DNS-err.log
exit 1
:EOF
echo "DNS ble slettet med argumentene %1 og %2, og dns-server: %3" >> Z:\Rydd_DNS\DNS-OK.log
exit 0 |
CleanDHCP.ps1 |
---|
##Atle Holm - 2009
#Reads from the result of CheckDHCP.ps1 and deletes all reservations listed there
$DAY = (get-date -displayHint date).day
$MONTH = (get-date -displayHint date).month
$YEAR = (get-date -displayHint date).year
if ($DAY -lt 10) {$DAY = "0" + $DAY}
if ($MONTH -lt 10) {$MONTH = "0" + $MONTH}
$USERNAME = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).name).split("\")[1]
$COMPUTERNAME = (Get-wmiObject -class win32_computerSystem).name
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY -type directory
}
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH -type directory
}
Write-Output "[$DATE $USERNAME] - CleanDHCP.ps1 $args" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH\CleanDHCP-STAT.log
$dhcpServer = "10.68.68.68"
Get-Content $args | % {
$IPMAC = $_.split(":")[1]
$scope = $_.split(":")[2]
Write-Output "Scope: $scope"
$IP = $IPMAC.split("-")[0].trimstart(" ")
Write-Output "IP: $IP"
$MAC = $IPMAC.split("-")[1].substring(0,12)
Write-Output "MAC: $MAC"
netsh dhcp server $dhcpServer scope $scope delete reservedip $IP $MAC
if ($LASTEXITCODE -eq "0") {
Write-Output "Sletting av $IP og $MAC på $dhcpServer under scopet $scope var vellykket!" >> `
\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\ERASE_DHCP_OK.log
} else {
Write-Output "Sletting av $IP og $MAC på $dhcpServer under scopet $scope var ikke vellykket!" >> `
\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\ERASE_DHCP_NA.log
}
} |
ReverseDel.bat |
---|
::echo "Confirm Yes or No (Y/N)."
::%1 er dns server
::%2 er suffix/dns-zone
::%3 er to første IP'er, reverse
::%4 er DNS navnet det gjelder
::%5 er hele IP til den hosten som skal få sin PTR slettet
:: Feks: dnscmd 10.54.54.54 /RecordDelete 233.10.in-addr.arpa 31.4 PTR cgawf030n.domene.net
dnscmd %1 /RecordDelete %2 %3 PTR %4 /f
IF %ERRORLEVEL%==0 GOTO EOF
echo "PTR for %5 ble ikke slettet med argumentene %4, %3, og %2, og dns-server: %1" >> Z:\Rydd_DNS\IP-err.log
exit 1
:EOF
echo "PTR for %5 ble slettet med argumentene %4, %3, og %2, og dns-server: %1" >> Z:\Rydd_DNS\IP-OK.log
exit 0 |
CleanDNS.ps1 |
---|
##Atle Holm - 2009
#Reads from the result of the script: CheckDNS.ps1
#Deletes all DNS records listed there
$DAY = (get-date -displayHint date).day
$MONTH = (get-date -displayHint date).month
$YEAR = (get-date -displayHint date).year
if ($DAY -lt 10) {$DAY = "0" + $DAY}
if ($MONTH -lt 10) {$MONTH = "0" + $MONTH}
$USERNAME = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).name).split("\")[1]
$COMPUTERNAME = (Get-wmiObject -class win32_computerSystem).name
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY -type directory
}
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH -type directory
}
Write-Output "[$DATE $USERNAME] - CleanDNS.ps1 $args" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH\CleanDNS-STAT.log
Get-Content $args | % {
$field = $_.split(" ")[1]
$lookup = nslookup $field
if ($lookup.count -gt 3) {
$FQDN = $lookup[0].split(":")[1].trimstart(" ")
$suffixSplit = $FQDN.split(".")
$dnsserver = $lookup[1].split(":")[1].trimstart(" ")
$suffix = $()
$count = $suffixSplit.count
$suffix += $suffixSplit[$count - 2]
$suffix += "."
$suffix += $suffixSplit[$count - 1]
\\(PLACE_TO_BAT)\DNSDEL.bat $suffix $field $dnsserver
} else {
$temp = $field
Write-Output "DNS-navnet $temp ble ikke funnet, og kan derfor ikke slettes. Det er nok slettet fra før." `
>> "\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\EDBOpersrv-DNS-err.log"
}
} |
CleanReverseDNS.ps1 |
---|
##Atle Holm - 2009
#Reads from the result of CheckReverseDNS.ps1 and deletes all reverse DNS lookup records
$DAY = (get-date -displayHint date).day
$MONTH = (get-date -displayHint date).month
$YEAR = (get-date -displayHint date).year
if ($DAY -lt 10) {$DAY = "0" + $DAY}
if ($MONTH -lt 10) {$MONTH = "0" + $MONTH}
$USERNAME = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).name).split("\")[1]
$COMPUTERNAME = (Get-wmiObject -class win32_computerSystem).name
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY -type directory
}
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH -type directory
}
Write-Output "[$DATE $USERNAME] - CleanReverseDNS.ps1 $args" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH\CleanReverseDNS-STAT.log
$DNSsrv = "10.54.54.54"
Get-Content $args | % {
$field = $_.split(" ")[1]
$lookup = nslookup $field
$IP = $field
if ($lookup.count -gt 3) {
$FQDN = $lookup[3].split(":")[1].trimstart(" ")
$IPS = $field.split(".")
$dnsserver = $lookup[1].split(":")[1].trimstart(" ")
$suffix = $()
$suffix += $IPS[1]
$suffix += "."
$suffix += $IPS[0]
$suffix += ".in-addr.arpa"
$addr = $IPS[3]
$addr += "."
$addr += $IPS[2]
\\(PLACE_TO_BAT)\ReverseDel.bat $DNSsrv $suffix $addr $FQDN $IP
} else {
$temp = $field
Write-Output "IP $temp ble ikke funnet, og kan derfor ikke slettes. Det er nok slettet fra før." `
>> "\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\IP-err.log"
}
} |
CheckReverseDNS.ps1 |
---|
##Atle Holm - 2009
#Reads from a csv file in the format DNS-Servername;IP
#The script then checks if the IP has a reverse lookup record in DNS.
#The result lists all reverse lookup records that are found or not.
#The result can later be used to run CleanReverseDNS.ps1 for deletion of these reverse lookup zones
$DAY = (get-date -displayHint date).day
$MONTH = (get-date -displayHint date).month
$YEAR = (get-date -displayHint date).year
if ($DAY -lt 10) {$DAY = "0" + $DAY}
if ($MONTH -lt 10) {$MONTH = "0" + $MONTH}
$USERNAME = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).name).split("\")[1]
$COMPUTERNAME = (Get-wmiObject -class win32_computerSystem).name
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY -type directory
}
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH -type directory
}
Write-Output "[$DATE $USERNAME] - CheckReverseDNS.ps1 $args" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH\CheckReverseDNS-STAT.log
Get-Content $args | % {
$field = $_.split(";")
$lookup = nslookup $field[1]
#$lookup
$temp = $field[1]
if ($lookup.count -gt 3) {
$FQDN = $lookup[3].split(":")[1].trimstart(" ")
$suffixSplit = $FQDN.split(".")
$suffix = $()
$count = $suffixSplit.count
$suffix += $suffixSplit[$count - 2]
$suffix += "."
$suffix += $suffixSplit[$count - 1]
Write-Output "IP $temp ble funnet, tilhørende domene $suffix." >> "\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\IPCHECK_Funnet.log"
} else {
Write-Output "IP $temp ble ikke funnet." >> "\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\IPCHECK_NA.log"
}
} |
CheckDNS.ps1 |
---|
##Atle Holm - 2009
#Reads from a csv file in the format DNS-Servername;IP
#The script will then create a result in \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY where it lists all active DNS names
#The result listing is later used to delete the records that are active via the script CleanDNS.ps1
$DAY = (get-date -displayHint date).day
$MONTH = (get-date -displayHint date).month
$YEAR = (get-date -displayHint date).year
if ($DAY -lt 10) {$DAY = "0" + $DAY}
if ($MONTH -lt 10) {$MONTH = "0" + $MONTH}
$USERNAME = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).name).split("\")[1]
$COMPUTERNAME = (Get-wmiObject -class win32_computerSystem).name
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY -type directory
}
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH -type directory
}
Write-Output "[$DATE $USERNAME] - CheckDNS.ps1 $args" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH\Check_DNS-STAT.log
Get-Content $args | % {
$field = $_.split(";")
$lookup = nslookup $field[0]
#$lookup
$temp = $field[0]
if ($lookup.count -gt 3) {
$FQDN = $lookup[3].split(":")[1].trimstart(" ")
$suffixSplit = $FQDN.split(".")
$suffix = $()
$count = $suffixSplit.count
$suffix += $suffixSplit[$count - 2]
$suffix += "."
$suffix += $suffixSplit[$count - 1]
Write-Output "DNS-navnet $temp ble funnet, tilhørende domene $suffix." >> "\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\CHECK_Found.log"
} else {
Write-Output "DNS-navnet $temp ble ikke funnet." >> "\\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\CHECK_NA.log"
}
} |
CheckDHCP.ps1 |
---|
##Atle Holm - 2009
#Reads from a csv file in the format dhcp-Servername;IP
#The script then checks if the IP's are reserved or not in DNS.
#The result from this script lists all reserved and non-reserverd IP adresses from
#all DHCP servers in the csv file that is given as an argument to this script.
#This result can then be used when running
#CleanReverseIP.ps1 to unreserve all the reserved IP's from theyr DHCP scopes
$DAY = (get-date -displayHint date).day
$MONTH = (get-date -displayHint date).month
$YEAR = (get-date -displayHint date).year
if ($DAY -lt 10) {$DAY = "0" + $DAY}
if ($MONTH -lt 10) {$MONTH = "0" + $MONTH}
$USERNAME = (([System.Security.Principal.WindowsIdentity]::GetCurrent()).name).split("\")[1]
$COMPUTERNAME = (Get-wmiObject -class win32_computerSystem).name
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY -type directory
}
if ((Test-Path -type container \\(PLACE_TO_LOG)\$YEAR-$MONTH) -eq $false) {
New-Item \\(PLACE_TO_LOG)\$YEAR-$MONTH -type directory
}
function Pause ($Message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
}
Write-Output "[$DATE $USERNAME] - CheckDHCP.ps1 $args" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH\CheckDHCP-STAT.log
$DHCPserver = "10.68.68.68"
Get-Content $args | % {
$field = $_.split(";")
Write-Output "Field: $field"
$currentIP = $field[1].trimstart(" ")
Write-Output "Current IP: $currentIP"
$currentIPs = $currentIP.split(".")
Write-Output "Current IPs: $currentIPs"
$scope = $currentIPs[0] + "." + $currentIPs[1] + "." + $currentIPs[2] + "." + "0"
Write-Output "Scope: $scope"
$reservedServers = netsh dhcp server $DHCPserver scope $scope show reservedip
if ($reservedServers -ne "The command needs a valid Scope IP Address.") {
$matched = $false
foreach ($reservedServer in $reservedServers) {
if ($reservedServer.contains("-") -and $reservedServer.contains(".")) {
$reservedMACs = $reservedServer.substring("27")
$reservedMACs = $reservedMACs.split("-")
$reservedMAC = $()
foreach ($MAC in $reservedMACs) {
$reservedMAC += $MAC
}
Write-Output "Evaluating Reserved MAC: $reservedMAC"
$reservedIP = $reservedServer.split("-")[0].trimEnd(" ")
$reservedIP = $reservedIP.trimstart(" ")
Write-Output "Evaluating Reserved IP and MAC: $reservedIP $reservedMAC"
if ($reservedIP -eq $currentIP) {
Write-Output "`t$reservedIP matched $currentIP"
$correctMAC = $reservedMAC
$matched = $true
} else {
Write-Output "`t$reservedIP did not match $currentIP"
}
}
}
if ($matched -eq $true) {
Write-Host "The following is reserved in DHCP: $currentIP-$correctMAC Scope:$scope" -Foregroundcolor Yellow
Write-Output "The following is reserved in DHCP: $currentIP-$correctMAC Scope:$scope" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\CHECK_DHCP_Found.log
} else {
Write-Host "The following is not reserved in DHCP: $currentIP Scope:$scope" -Foregroundcolor Yellow
Write-Output "The following is not reserved in DHCP: $currentIP Scope:$scope" >> \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\CHECK_DHCP_NA.log
}
pause
} else {
Write-Output "$scope: This is not a valid scope." >> \\(PLACE_TO_LOG)\$YEAR-$MONTH-$DAY\CHECK_DHCP_NA.log
}
} |
CheckScopes.ps1 |
---|
#Atle Holm - 2009
#Dette scriptet sjekker befoolkningen på alle scopes på gitt DHCP server
$DHCPServer = "10.68.68.68"
$scopeSamling = netsh dhcp server $DHCPServer show scope
$count = 0
$totalPopulation = 0
foreach($scope in $scopeSamling) {
if ($scope.contains(".")) {
$scopeIP = ($scope.split("-")[0].trimEnd(" ")).trimStart(" ")
Write-Output "Working with: $scopeIP"
$scopeList = netsh dhcp server $DHCPServer scope $scopeIP show clients
if ($scopeList[$scopeList.count - 3] -ne "The command needs a valid Scope IP Address." -and $scopeList[$scopeList.count - 3].contains("version")) {
$temp = "Working with: " + $scopeList[$scopeList.count - 3]
Write-Output $temp
$scopePopulation = $scopeList[$scopeList.count - 3].split(":")[1].substring(1,1)
$count++
#Seems as if the server does not like to be flooded
if($count -gt 20) {
$count = 0
Start-Sleep -m 10000
}
Write-Output "This scope is populated by the following number of inhabitants: $scopePopulation`n"
$totalPopulation += $scopePopulation
if ($scopePopulation -eq "0") {
Write-Output "Scope: $scopeIP has a scope-population of $scopePopulation" >> .\DHCP_scopes_NA.log
} else {
Write-Output "Scope: $scopeIP has a scope-population of $scopePopulation" >> .\DHCP_scopes_OK.log
}
}
}
}
Write-Output "There has bee counted up $totalPopulation clients." >> .\DHCP_scopes_OK.log |
|