Last active
August 29, 2015 14:07
-
-
Save mbrownnycnyc/8f2b098622a40b5c13c1 to your computer and use it in GitHub Desktop.
script to set DNS servers of all IP enabled NICs on a host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx | |
function set-dnsservers { | |
param( | |
[string[]]$dnsservers, | |
[string]$computername | |
) | |
#you must filter by ip address, otherwise it will affect all ipenabled NICs, which will include any iSCSI | |
$wmi = get-wmiobject -ComputerName $computername win32_networkadapterconfiguration -filter "ipenabled='true'" | where {$_.ipaddress -like "192.168.50*" } | |
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa393295%28v=vs.85%29.aspx | |
$dnsmethodreturnvalue = $($wmi.SetDNSServerSearchOrder($dnsservers)).returnvalue | |
if ($dnsmethodreturnvalue -ne "0"){ | |
write-error "SetDNSServerSearchOrder win32_networkadapterconfiguration function failed with error $dnsmethodreturnvalue. http://msdn.microsoft.com/en-us/library/windows/desktop/aa393295%28v=vs.85%29.aspx" | |
} | |
else { | |
write-output "DNS servers $dnsservers has been set on $computername" | |
} | |
} | |
#example enumerating all VMs on a vCenter server (note that I am usign the `Name` property of VMs, as we name our VMs the hostname of the server | |
Connect-VIServer -Server $vCenter -User $user -Password$ | |
$vmnames = $(get-vm -name VMnameprefix-*).name | |
$dnsservers = "192.168.0.1","192.168.0.2","192.168.0.3","192.168.0.4" | |
foreach ($vm in $vmnames){ | |
set-dnsservers -computername $vm -dnsservers $dnsservers | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment