Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Converting Powershell to VBS, Setting a WMIObject (DNS)

$
0
0
While we are waiting for our "Deploy Powershell to everything that can run it" initiative to finally break through the change control process, one of our teams has asked us to update the DNS entries on all machines in a specific DC. While the code should be pretty simple in PS, it appears I will need to accomplish the task with VBScript which I have never actually coded in before.
$DNS1 = ""
$DNS2 = ""
$WMICall = Get-WmiObject Win32_NetworkAdapterConfiguration| where {($_.dhcpenabled -match "false") -and ($_.IPAddress -ne $null) -and (($_.Properties.name -match "DnsServerSearchOrder") -and ($_.Properties.Value -ne $null))}
$WMICall.SetDNSServerSearchOrder("$DNS1,$DNS2")
The essence of the script is to look at the network adapters, make sure it isn't a DHCP adapter, make sure it has an IP, and make sure that it already has DNS settings. If those all pass, it should set the new DNS entries. To make this even more fun, all of the test servers I have available to me are on DHCP.
After searching the interwebs I have put together the following script block, but I was wondering if anyone knows a simpler method, or can point out some errors in what I have here:

strComputer = objSysInfo.ComputerName

strDNS1 = "" strDNS2 = "" Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colNetCards = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True") For Each objNetCard in colNetCards arrDNSServers = Array(strDNS1, strDNS2) objNetCard.SetDNSServerSearchOrder(arrDNSServers) Next

Thanks in advance!

(EDIT)
Credit to Mark D. MacLachlan @ The Spider's Parlor for the VBS. All I did was cut the WINS portion out of something he had posted.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>