VB Script for changing WINS and DNS on remote machines

Asked By 70 points N/A Posted on -
qa-featured

Does any one know script on vb net to change WINS and DNS on remote machines

SHARE
Answered By 5 points N/A #135390

VB Script for changing WINS and DNS on remote machines

qa-featured

Dear user,

You can copy this code and paste it in your VB project. You will need WMI to use this code:

' IMPORTANT !!!! Change these constants !!!!
Const STR_SERVERS = "server1 server2 "  ' Enter your servers here, separated by a space
Const STR_NEWDNS1 = "192.168.1.1"
Const STR_NEWDNS2 = "192.168.1.2"
Const STR_NEWWINS1= "192.168.1.3"
Const STR_NEWWINS2= "192.168.1.4"
 
' ///////////////////////////////////////////////////////////////////////////////////////////////
Do
    cKeuze = MakeChoise( "Choose: Report or Change (R/C)?" )
Loop until cKeuze = "C" Or cKeuze = "R"
WScript.Echo
If( cKeuze = "C" ) Then
    Change( STR_SERVERS )
Else
    Report( STR_SERVERS )
End If
WScript.Echo( "Check out https://www.activexperts.com/ for more samples and components" )
' ///////////////////////////////////////////////////////////////////////////////////////////////

' ///////////////////////////////////////////////////////////////////////////////////////////////
Sub Change( strParamServers )
    arrServers = Split( strParamServers, " " )
    For i = 0 To UBound( arrServers )
        ShowDnsWins arrServers( i )    
        Do
            cKeuze = MakeChoise( "  Change DNS/WINS and reboot (y/n)?" )
        Loop until cKeuze = "Y" Or cKeuze = "N"    
        If( cKeuze = "Y" ) Then
            SetDnsWins arrServers( i )        
            RebootServer( arrServers( i ) )
        End If
           
        WScript.StdOut.Write( vbCrlf )
    Next
End Sub
' ///////////////////////////////////////////////////////////////////////////////////////////////
Sub Report( strParamServers )
    arrServers = Split( strParamServers, " " )
    For i = 0 To UBound( arrServers )
        ShowDnsWins arrServers( i )
    Next
End Sub
' ///////////////////////////////////////////////////////////////////////////////////////////////
Sub ShowDnsWins( strServer )
    strWinMgmt = "winmgmts:{impersonationLevel=impersonate}!//"& strServer &""
    Set objNICs = GetObject( strWinMgmt ).InstancesOf( "Win32_NetworkAdapterConfiguration" )
    WScript.StdOut.Write( strServer & ": " & vbCrlf )
    For Each objNIC In objNICs
        If objNIC.IPEnabled Then
                WScript.StdOut.Write( "  " & objNIC.Description & ": " & vbCrlf & "    " )
                n = 1
                For Each strDns In objNIC.DNSServerSearchOrder
                    WScript.StdOut.Write "DNS" & n & ":" & strDns & " "
                    n = n + 1
                Next
                WScript.StdOut.Write( vbCrlf )
        End If
    Next
    WScript.StdOut.Write( vbCrlf )
End Sub
' ///////////////////////////////////////////////////////////////////////////////////////////////
Sub SetDnsWins( strServer )
    strWinMgmt = "winmgmts:{impersonationLevel=impersonate}!//"& strServer &""
    Set objNICs = GetObject( strWinMgmt ).InstancesOf( "Win32_NetworkAdapterConfiguration" )

    WScript.StdOut.Write( "  Set DNS for NIC: " )
    For Each objNIC In objNICs
        If objNIC.IPEnabled Then
            objNIC.SetWINSServer STR_NEWWINS1,STR_NEWWINS2
            objNIC.SetDNSServerSearchOrder Array(STR_NEWDNS1,STR_NEWDNS2)
            WScript.StdOut.Write objNIC.Description & "  "    
        End If
    Next
    WScript.StdOut.Write( vbCrlf )
End Sub
' ///////////////////////////////////////////////////////////////////////////////////////////////
Sub RebootServer( strServer )
    Set OpSysSet = GetObject("winmgmts:{(RemoteShutdown)}//" & strServer & "/root/cimv2").ExecQuery("Select * from Win32_OperatingSystem where Primary=True")
    WScript.StdOut.Write( "  Reboot: " )        
    For Each OpSys In OpSysSet
        WScript.StdOut.Write OpSys.Name
        OpSys.Reboot()
    Next
    WScript.StdOut.Write( vbCrlf )
End Sub
' ///////////////////////////////////////////////////////////////////////////////////////////////
Function MakeChoise( strMesg )
    WScript.StdOut.Write(strMesg)
    WScript.StdIn.Read(0)
    strChoise = WScript.StdIn.ReadLine()
    MakeChoise = UCase( Left( strChoise, 1 ) )
End Function

Check out this website for more help:

https://www.activexperts.com/files/vbscript-powershell-component/manual.htm

Hope this will help you.

Thank you.

Related Questions