Popular Posts

Thursday, January 4, 2018

Set DNS server list on a set of clients using SCCM collections


  1. Create a package that calls the following PowerShell script

$logfile = 'C:\Windows\Temp\SetDNS.log'
function writetolog([string] $txt) {        $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    Add-content $logfile -value $Stamp':'$txt
}

try{
    $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
    $DNSServers = $tsenv.Value("DNSservers")    write-host "Retrieved DNS servers list successfully"    writetolog("Retrieved DNS servers list successfully : " + $DNSServers)    $DNSServers = $DNSServers -split ','

}
catch{
    write-host "Cannot retrieve DNS servers list"    writetolog("Retrieved DNS servers list successfully")    exit -1     }

try{
    $Interface = Get-WMIObject Win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq “True” -and $_.IPAddress -like "53.*"} -ErrorAction Stop
    write-host "Connected to WMI succesfully and interfaces retrieved"    writetolog("Connected to WMI succesfully and interfaces retrieved")
    Foreach($NIC in $Interface) {
        try{                  $NIC.SetDNSServerSearchOrder($DNSServers)            write-host "DNS servers configured successfully"            writetolog("DNS servers configured successfully")            exit 0
        }        catch{
            write-host "Cannot set DNS servers"            writetolog("Cannot set DNS servers")            exit -1
        }
    }
}
catch{        write-host "Cannot connect to WMI"    writetolog("Cannot connect to WMI")    return -1
}

2. Create a task sequence that installs the above package using Install Software task

3. Create a collection, add the clients that you need to set the DNS for
4. On that collection, set a collection variable called DNSservers and add the list of DNS servers separated by comma



5. Finally advertise the task sequence on this collection