top of page

PowerShell | Set DNS Settings on All Systems

#======================
#Get List of Computers
#======================

$MyComputers=Get-ADComputer -filter * -Properties * | Where{($_.Enabled -eq $True) -and ($_.Operatingsystem -like "Windows*")} | Select -ExpandProperty Name 
#====================
#Instantiate Array
#===================

$MyArray=@()
$MyFails=@()

#===============================
#Check Access to Each Computer
#===============================

Foreach($Comp in $MyComputers){       
If((Invoke-Command -ErrorAction SilentlyContinue –ComputerName $comp –ScriptBlock {1}) –eq 1){
$MyArray+=$Comp
}Else{
$MyFails+=$Comp
}
}

#====================================================
# Export Pass failed for Further Review
#====================================================

$MyArray | Out-File C:\temp\PassedDNSSystems.txt
$MyFails | Out-File C:\temp\FailedDNSSystems.txt

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

# -------Main Command Execute on Remote Machines---------

#====================================================
$MyCommand={
# Update Double Quoted and Commma Separated DNS Servers by IP
$newDNSServers = "127.0.0.1","172.31.47.11","192.168.0.1"
# Get all network adapters that already have DNS servers set
$adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DNSServerSearchOrder -ne $null}
# Set the DNS server search order for all of the previously-found adapters
$adapters | ForEach-Object {$_.SetDNSServerSearchOrder($newDNSServers)}
#Get New DNS Settings and Return in Object
$MyNS=Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {($_.DNSServerSearchOrder -ne $null)} | Select -ExpandProperty DNSServerSearchOrder
#===================================
# Pull DNS Settings add to Object
#===================================

$Obj=New-Object PSObject
$Obj |Add-Member NoteProperty ServerName ($env:COMPUTERNAME)
$Obj |Add-Member NoteProperty DNS_Settings ($MyNS)
Return $Obj
}
# End Main Command
#===============================================
# ------ Execution & Export to File ------------
#===============================================

$MyReturnValues=Invoke-Command $MyArray -ScriptBlock $MyCommand
$MyReturnValues | Select ServerName, DNS_Settings | Where{$_.ServerName -ne $NULL} | Export-Csv C:\temp\NewDNS.csv -NoTypeInformation -Append

bottom of page