VMware Cloud Community
piyushranusri
Enthusiast
Enthusiast

PowerCli script to update DNS IP

have more than 200 windows vm's on different Vlan and now the scnerio came for us to update new DNS primary and Secondary IP for second nic.. we have 2 nic on each vm.

any script to update this settings please.

0 Kudos
3 Replies
LucD
Leadership
Leadership

That is in fact something that you would need to do inside the guest OS with the netsh command.

You can launch such a command inside the guest OS with the Invoke-VMScript cmdlet (provided you have VMware Tools installed).

First you will need to get the name of the 2nd NIC inside the guest OS

netsh interface show interface

Then with that name, you can set the DNS servers.

For example

netsh interface ip set dnsservers <interfacename> static 10.0.0.1

netsh interface ip add dnsservers <interfacename> 10.0.0.2

Jase did a script a while back, see Update VMware Windows Guest DNS and WINS through PowerCLI


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
piyushranusri
Enthusiast
Enthusiast

some how i got this as workaround,

tell me if this is good. i will be checking this tomorrow and share the result

$VIServer = Read-Host "Enter vCenter Server Name or IP: "
Connect-VIServer $VIServer
 
$HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter ESX host credentials", "", "")
$GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials", "", "")
$PrimaryDNS = Read-Host "Primary DNS: "
$SecondaryDNS = Read-Host "Secondary DNS: "
$PrimaryOldWINS = Read-Host "Old Primary WINS: "
$SecondaryOldWINS = Read-Host "Old Secondary WINS: "
$PrimaryWINS = Read-Host "Primary WINS: "
$SecondaryWINS = Read-Host "Secondary WINS: "
 
get-vm |  %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" -ScriptText "netsh interface ip set dns ""Local Area Connection"" static $PrimaryDNS" }
get-vm |  %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" -ScriptText "netsh interface ip add dns ""Local Area Connection"" $SecondaryDNS" }
get-vm |  %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" -ScriptText "netsh interface ip delete wins ""Local Area Connection"" $PrimaryOldWINS" }
get-vm |  %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" -ScriptText "netsh interface ip delete wins ""Local Area Connection"" $SecondaryOldWINS" }
get-vm |  %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" -ScriptText "netsh interface ip add wins ""Local Area Connection"" $PrimaryWINS" }
get-vm |  %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" -ScriptText "netsh interface ip add wins ""Local Area Connection"" $SecondaryWINS index=2" }

0 Kudos
LucD
Leadership
Leadership

That is in essence the same as the commands I gave earlier, but you will have the first find the internal name of the 2nd NIC.
But once you have that name and replace the "Local Area Connection" with that name, that script should work.

Just leave out the part that sets the IP address of the NIC


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos