FamilyTechYmas

miércoles, 20 de enero de 2021

Bulk replace DNS entries if old values found

This code will allow you to bulk replace the DNS entries on Windows Server 2008R2+ based on comparison between current machine values, and the ones defined in the variable $oldDNS

It logs all needed information in case it works smooth or if any error comes up.

Input: computers names must be in the file "C:\temp\comp_list.txt" in single column

Remember, run as admin 😉


#=============================================================================

# SCRIPT NAME     :     Detect_old_DNS_IP_and_change.ps1

#

# AUTHOR          :     Cristian Marius Precub

# CREATION DATE   :     2021/20/01

# RELEASE         :     v1.0

# USAGE SYNTAX    :    

# NORMAL RETURN   :    

# ERROR RETURN    :    

#

#=============================================================================

#                 - DISCLAIMER -

# This sample script is not supported under any Microsoft standard support

# program or service. The sample script is provided AS IS without warranty of

# any kind. Microsoft further disclaims all implied warranties including,

# without limitation, any implied warranties of merchantability or of fitness

# for a particular purpose. The entire risk arising out of the use or

# performance of the sample scripts and documentation remains with you. In no

# event shall Microsoft, its authors, or anyone else involved in the creation,

# production, or delivery of the scripts be liable for any damages whatsoever

# (including, without limitation, damages for loss of business profits, business

# interruption, loss of business information, or other pecuniary loss) arising

# out of the use of or inability to use the sample scripts or documentation,

# even if Microsoft has been advised of the possibility of such damages.

#=============================================================================

#                 - SCRIPT DESCRIPTION -

# This script gets the DNS entries from any active, ipv4 configured network

# adapters and compares them with the entries from the specified object $OldDNS

# If coincidence in any of the entries, then it will replace them to the values

# of $newdns

#

#=============================================================================

#                 - RELEASE NOTES -

# v1.0 - 2021/20/01 - Cristian Marius Precub  - Script Creation

#

# !!!!!!IMPORTANT!!!!!!

# Uncomment line 72 to actually perform the changes

#

#=============================================================================

#Requires -version 2.0

 

$log="C:\temp\DNS_Change.log"

$cred=Get-Credential

 

$computers=get-content "C:\temp\comp_list.txt"

foreach ($computer in $computers) {

"----------------------------------------------------------------------" >> $log

"$computer" >> $log

try{

$remote_commands=Invoke-Command -ComputerName $computer -Credential $cred -ArgumentList "$log","$computer" -ScriptBlock {

$OldDNS=@("10.15.15.2","192.16.15.6")

 

#check if TEMP folder is present and create if not.

if (-not(test-path "C:\temp")) {

$(date).ToString() + " Temp not existing. Creating it..." | Write-Output

try{

new-item "C:\temp" -Type Directory -ErrorAction Stop

$(date).ToString() + " Temp folder created" >> $args[0]

} catch {

$(date).ToString() + " **ERROR** " + $($PSItem.ToString()) | Write-Output

$(date).ToString() + " **ERROR** " + $($_.Exception.Message)| Write-Output

}

}

#get all adapters with IP configured

try{

$adapters=Get-WmiObject -Class Win32_NetworkAdapterConfiguration `

     -Filter IPEnabled=TRUE `

     -ErrorAction Stop

$(date).ToString() + " Fetching adapters" | Write-Output

} catch {

    $(date).ToString() + " **ERROR getting adapters** " + $($PSItem.ToString())| Write-Output

    $(date).ToString() + " **ERROR getting adapters** " + $($_.Exception.Message) | Write-Output

}

 

foreach ($dnsvalue in $adapters.DNSServerSearchOrder) {

$newdns=@("25.13.10.11","143.12.1.15") | Sort-Object {Get-Random}

if ($olddns -contains $dnsvalue) {

     $(date).ToString() + " OLD DNS Entries detected. Changing to new ones.."| Write-Output

     try{

        #$adapters.SetDNSServerSearchOrder($newdns)

        $(date).ToString() + " DNS entries updated" | Write-Output

     } catch {

         $(date).ToString() + " **ERROR** " + $($PSItem.ToString()) | Write-Output

         $(date).ToString() + " **ERROR** " + $($_.Exception.Message) | Write-Output

     }

    } else {

    $(date).ToString() + " DNS entries are correct. No change performed." | Write-Output

    } #end of if

    } # end of foreach dnsvalue

 

} -ErrorAction Stop # end of scriptblock

} catch {

    $(date).ToString() + " **ERROR invoking command** " + $($PSItem.ToString())  >> $log

    $(date).ToString() + " **ERROR invoking command** " + $($_.Exception.Message)  >> $log

    $error[0]  >> $log

}

$remote_commands >> $log

"----------------------------------------------------------------------" >> $log

}#end of foreach computer

 


1 comentario:

  1. Making Money - Work/Tennis: The Ultimate Guide
    The way you would expect from https://deccasino.com/review/merit-casino/ betting on the tennis matches of tennis is to jancasino bet on the player you หารายได้เสริม like most. But worrione you also need https://deccasino.com/review/merit-casino/ a different

    ResponderEliminar

¡Comenta aquí!