Change DNS remotely using PowerShell

Hi Experts,

I've been trying to create a script that will change DNS settings of all servers.

What I've done was to:

1. Export a csv (data.csv) containing the following

a. Server Names

b. NIC Names (I've created this through powershell query also). I need this because there are some servers that have multiple NIC cards.

c. Primary DNS

d. Secondary DNS

2. I created variables getting the details from the CSV data.

My problem is: when I use the command below using the variables, the script is not working. But when I specify the details, it is working.

Any advice?

TIA

Below is my script.

$data = import-CSV C:\temp\DNS\Data.csv
$server = $data.server
$nic = $data.nic
$dns1 = $data.dns1
$dns2 = $data.dns2

$OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" -computername $server

# For Windows 2008 R2
if (($OS.Version -eq "6.1.7601"))
{
invoke-command -computername $server
{
netsh interface ip set dns $nic static $dns1
netsh interface ip add dns $nic $dns2 index=2
}
}

# For Windows 2012 / 2012 R2
if (($OS.Version -eq "6.2.9200") -or ($OS.Version -eq "6.3.9600"))

{
invoke-command -computername $server {Set-DNSClientServerAddress -InterfaceAlias $nic -ServerAddresses $dns1,$dns2}
# When i use below command, it is working
# invoke-command -computername TestServer {Set-DNSClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 1.1.1.1,2.2.2.2}
}

August 28th, 2015 3:21am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics