Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Deploy Network Setting

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,28319
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Deploy Network Setting
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Deploy Network Setting - 11/24/2005 4:58:15 PM   
  yrh4401

 

Posts: 4
Score: 0
Joined: 11/24/2005
Status: offline
'********************************************************************
'* File:           DeployNetworkSetting.VBS
'* Created Date: 2004/11/26
'* Author         : Ray Yen
'* Version        :   1.0
'*
'* Main Function:
'*
'* Usage:
'   For Example : cscript DeployNetworkSetting.vbs
'Registry
'
'WINS 9x
'[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\NetTrans\0000]
'"NameServer1"="168.192.1.11"
'"NameServer2"="168.192.1.21"
'"NodeType" ="8"
'
'DNS 9x
'[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP]
'"EnableDNS"="1"
'"NameServer"="203.65.100.21,203.65.100.22"
'"HostName"="fareast'
'
'********************************************************************
Const HKEY_CLASSES_ROOT  = &H80000000
Const HKEY_CURRENT_USER  = &H80000001
Const HKEY_LOCAL_MACHINE  = &H80000002
Const HKEY_USERS   = &H80000003
Const HKEY_CURRENT_CONFIG  = &H80000005
Const REG_SZ  = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD  = 4
Const REG_MULTI_SZ = 7
'For Environment Variables
Dim strEnvOSName
Dim strEnvOSVersion
Dim strEnvOSLanguage
Dim strEnvOSPatch
Dim strEnvOSPatchVersion
Dim strEnvIEVerion
Dim strEnvOEVersion
'DNS Server Registry Settings
Dim DNSServerArray
   DNSServerArray = Array("192.168.1.200","192.168.1.201")
  
Dim strDNSEnable   
strDNSEnable = "1"
Dim strDNSServerList
strDNSServerList = "192.168.1.200,192.168.1.201"

Dim strHostName 
strHostName = "TestHostName"

Dim strDomain 
strDomain = "TestDomain"
'WINS Server Registry Settings
'WINSPrimaryServer or WINSSecondaryServer or WINS server.
strWINS1 = "172.16.1.122"
strWINS2 = "172.16.1.132"
'Proxy Server Client Setting
Dim strProxyServer
Dim strProxyEnable
Dim strProxyOverride
strProxyServer = "bbb.com:80"
strProxyEnable = 1    'DWORD
strProxyOverride = "<local>"
Dim strTestMode
'======== Customize variables =============
'strTestMode=True      'Show Debug Message
strTestMode=False      'Don't Show Debug Message
'Detect client environment
Call CheckOS()
Call Main()
Wscript.Quit
'===================================
' Main Sub
'Note :
' Value : 1 = Permit , 5 = Not Accept
'===================================
Sub Main()
Dim strComputer, strRegistryLoc, strRegistryName
Dim strRegistryFullPath
Dim subkey
Dim vntSubKeyListArray
Dim strRegistryType
Dim strResult
'For DNS Checking
Dim blnChkDNSFlag
blnChkDNSFlag = Fasle
Dim blnResult
Dim strExistedValue
strComputer = "."      'Or IP Address   
If strEnvOSName = "W9X" Then

 PrintDebug "=== This computer is WIN9X OS ==="

 'Check WINS server and write setting
 'WINS not to implement this time  
 
 'Check DNS Server and write setting 
 'Step 1-1 : Checking DNS Enable
 
  'Setting Registry String
  strRegistryType = "HKEY_LOCAL_MACHINE"
  strRegistryLoc = "System\CurrentControlSet\Services\VxD\MSTCP"   
  strRegistryName = "EnableDNS" 
  'Check exist registry
  strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
  PrintDebug "strRegistryFullPath = " & strRegistryFullPath 
  
  strExistedValue = fnRegRead(strRegistryFullPath)
  PrintDebug "Read Existed Registry Value = " & strExistedValue 
  
  If strExistedValue <> strDNSEnable Then   
   PrintDebug "DNS status not enabled!"    
   'Enable the DNS Server    
   strRegistryName = "EnableDNS"
   strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
   blnResult = fnRegWrite(strRegistryFullPath,strDNSEnable,"REG_SZ")
   PrintDebug "DNS Enabled Process: " & blnResult     
  Else
   PrintDebug "DNS Already Enabled!"                    
  End If
 
 'Step 1-2 : Checking DNS Server Settings
    
  'Setting Registry String
  strRegistryType = "HKEY_LOCAL_MACHINE"
  strRegistryLoc = "System\CurrentControlSet\Services\VxD\MSTCP"   
  strRegistryName = "NameServer" 
  'Check exist registry
  strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
  PrintDebug "strRegistryFullPath = " & strRegistryFullPath 
  
  strExistedValue = fnRegRead(strRegistryFullPath)
  PrintDebug "Read Existed Registry Value = " & strExistedValue  
  
  If strExistedValue <> strDNSServerList Then   
  
   PrintDebug "DNS information is not update!"    
   'Write DNS Server List
   strRegistryName = "NameServer"
   strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
   blnResult = fnRegWrite(strRegistryFullPath,strDNSServerList,"REG_SZ")
   PrintDebug "Write DNS Server List Process : " & blnResult
     
   'Write DNS HostName
   'strRegistryName = "HostName"
   'strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
   'blnResult = fnRegWrite(strRegistryFullPath,strHostName,"REG_SZ")
   'PrintDebug "Write DNS HostName Process : " & blnResult
   'Write Domain Name
   'strRegistryName = "Domain"
   'strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
   'blnResult = fnRegWrite(strRegistryFullPath,strDomain,"REG_SZ")
   'PrintDebug "Write DNS HostName Process : " & blnResult
  Else   
   PrintDebug "DNS Server already updated!"
  End If  
 
 'Step 2 : Checking WINS Name Server 
 'WINS not to implement this time  
  
else
 
 'Windows 2000/XP/NT
 
 PrintDebug "=== This computer is 2000/XP/NT OS ==="
 'Check DNS Server and write setting 
 
 strResult = W2KDNSWINSConfig() 
 
 If strResult = False Then
  PrintDebug "There was an error configuring the DNS Server!"
  'msgbox "There was an error configuring the network adapter", vbCritical, "Error"
 Else
  PrintDebug "Configuring the DNS Server success!"   
 End If 
 'Check WINS server and write setting
 'WINS not to implement this time

End If

'========================================================
'Step 3 : Checking IE Proxy Setting

'Setting Registry String
strRegistryType = "HKEY_CURRENT_USER"
   strRegistryLoc = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"   
strRegistryName = "ProxyServer" 
'Check exist registry
strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
PrintDebug "strRegistryFullPath = " & strRegistryFullPath 

strExistedValue = fnRegRead(strRegistryFullPath)
PrintDebug "Read Existed Registry Value = " & strExistedValue

'If client proxy server setting <> setting value the write the registry to client
If Trim(strExistedValue) <> strProxyServer Then

 'Write ProxyServer
 strRegistryName = "ProxyServer"
 strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
 blnResult = fnRegWrite(strRegistryFullPath,strProxyServer,"REG_SZ")
 PrintDebug "Write ProxyServer : " & blnResult
 
 'Write ProxyEnable
 strRegistryName = "ProxyEnable"
 strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
 blnResult = fnRegWrite(strRegistryFullPath,strProxyEnable,"REG_DWORD")
 PrintDebug "Write ProxyEnable : " & blnResult
   
 'Write ProxyOverride
 strRegistryName = "ProxyOverride"
 strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
 blnResult = fnRegWrite(strRegistryFullPath,strProxyOverride,"REG_SZ")
 PrintDebug "Write ProxyOverrider : " & blnResult
  
End If

PrintDebug "Script Running Done!"
End Sub
'=========================
'Write Windows
'=========================
Function W2KDNSWINSConfig()
Dim WshShell, bKey
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WSHEnv = WSHShell.Environment

Set cards = GetObject("winmgmts:").InstancesOf ("Win32_NetworkAdapterConfiguration")
'myNet = Array("255.255.255.0")
On Error resume next
 
  For each card in cards
   ' find first ip enabled card and set the ip address
   If card.DHCPEnabled  = false then 'to judge the DHCP enable or not.
 If card.IPenabled = true then
  'WINS not to implement this time
  'ret = card.SetWinsServer(strWINS1, strWINS2)
  ret = card.SetDNSServerSearchOrder(DNSServerArray)
 Exit for
 End If
   End If
  Next
   If err.Number <> 0 then
   'msgbox "There was an error configuring the network adapter", vbCritical, "Error"
   W2KDNSWINSConfig = False
Else
   W2KDNSWINSConfig = True    
   End if
 
on error goto 0

set cards = nothing

End Function
'=========================
'Read Registry
'=========================
Function fnRegRead(strRegistry)
On Error Resume Next
   Dim wshShell
   Dim bKey
  
   Set wshShell = Wscript.CreateObject("WScript.Shell")
   bKey = wshShell.RegRead(strRegistry)
  
   fnRegRead = bKey
  
   If Err.Number <> 0 Then
       Msg = "Error # " & CStr(Err.Number) & " was generated by " & Err.Source & Chr(13)
       PrintScreen "Error Message : " & Msg
       PrintScreen "Error Description : " & Err.Description
       Err.Clear       
       fnRegRead = 0   
   End If     
End Function
'=========================
'Write Registry
'strRegType : REG_SZ....
'=========================
Function fnRegWrite(strRegistry , intValue, strRegType)
On Error Resume Next
   Dim wshShell
  
   Set wshShell = Wscript.CreateObject("WScript.Shell")
   wshShell.RegWrite strRegistry, intValue, strRegType
 
   fnRegWrite = True
  
   If Err.Number <> 0 Then
       Msg = "Error # " & CStr(Err.Number) & " was generated by " & Err.Source & Chr(13)
       PrintScreen "Error Message : " & Msg
       PrintScreen "Error Description : " & Err.Description
       Err.Clear               
       fnRegWrite = False
   End If
End Function
'=========================
'Get Remote  Registry value
'GetDWORDValue Method of the StdRegProv Class
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/getdwordvalue_method_in_class_stdregprov.asp
'=========================
Function GetRemoteRegValue(strComputer,strKeyPath,strValueName,strRegistryType)
On Error Resume Next
Dim oReg
Dim dwValue
Dim intCount
Dim blnResult 

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") 
blnResult = oReg.GetDWORDValue(strRegistryType,strKeyPath,strValueName,dwValue)

   GetRemoteRegValue = dwValue
  
End Function
'=========================
'Get Local Registry value
'=========================
Function RetriveRegistry(strKeyPath,strValueName,strRegistryType)
On Error Resume Next
Dim strComputer
Dim dwValue
Dim blnResult
   strComputer = "."
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
blnResult = oReg.GetDWORDValue(strRegistryType,strKeyPath,strValueName,dwValue)
RetriveRegistry = dwValue

End Function
'===========================================================================
'List Registry Subkey (Return Array)
'===========================================================================
Function vntEnumSubKey(strComputer,strKeyPath,strRegistryType)
On Error Resume Next
Dim oReg
   Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
oReg.EnumKey strRegistryType, strKeyPath, arrSubKeys 
vntEnumSubKey = arrSubKeys

End Function 

'=======================================================================
' For Get OS Version
'=======================================================================
Sub CheckOS()
On Error Resume Next
Dim SP6a
Dim WshShell, CurrentVersion, CSDVersion,WshSysEnv,ProductLanguage
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")

'PrintDebug "WshSysEnv('OS') = " & UCase(WshSysEnv("OS"))
'Windows 9X
If  UCase(WshSysEnv("OS"))<>"WINDOWS_NT" Then
 strEnvOSName="W9X"
 strEnvOSVersion=""
 strEnvOSLanguage=""
 strEnvOSPatch = ""
 strEnvOSPatchVersion = ""
 Set WshSysEnv = Nothing
 Set WshShell = Nothing
 Exit Sub
End If
'Get Current Version
CurrentVersion = WshShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
CSDVersion=WshShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CSDVersion")
ProductLanguage = WshShell.regread("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\Language\InstallLanguage")

'PrintDebug "----------------------------------------"
'PrintDebug "CurrentVersion = " & CurrentVersion
'PrintDebug "CSDVersion = " & CSDVersion
'PrintDebug "ProductLanguage = " & ProductLanguage
'PrintDebug "----------------------------------------"
   Select Case ProductLanguage
    Case "0409"  'Eng
  strEnvOSLanguage="EN"
 Case "0404"  '”Éé“’†•¶
  strEnvOSLanguage="ZH"
   End Select
      
   strEnvOSVersion=CurrentVersion   
  
   'PrintDebug "strEnvOSLanguage = " & strEnvOSLanguage
   'PrintDebug "strEnvOSVersion =  " & strEnvOSVersion
'Windows XP
If CurrentVersion="5.1" Then

 strEnvOSName="WXP"    
   
   Select Case CSDVersion
  Case "Service Pack 1"
   strEnvOSPatch = "SP1"
   strEnvOSPatchVersion = "1"   
  Case  "Service Pack 2"
   strEnvOSPatch = "SP2"
   strEnvOSPatchVersion = "2"   
  Case Else
   strEnvOSPatch = ""
   strEnvOSPatchVersion = ""  
 End Select    
  
  'PrintDebug "strEnvOSName = " & strEnvOSName
  'PrintDebug "strEnvOSPatch = " & strEnvOSPatch
  'PrintDebug "strEnvOSPatchVersion = " & strEnvOSPatchVersion
  Set WshSysEnv = Nothing
  Set WshShell = Nothing
 Exit Sub
End If
'Win2000
If CurrentVersion="5.0" Then 
  
  strEnvOSName="W2K"
   Select Case CSDVersion
  Case "Service Pack 1"
   strEnvOSPatch = "SP1"
   strEnvOSPatchVersion = "1"   
  Case  "Service Pack 2"
   strEnvOSPatch = "SP2"
   strEnvOSPatchVersion = "2"   
  Case  "Service Pack 3"
   strEnvOSPatch = "SP3"
   strEnvOSPatchVersion = "3"   
  Case  "Service Pack 4"
   strEnvOSPatch = "SP4"
   strEnvOSPatchVersion = "4"   
  Case Else
   strEnvOSPatch = ""
   strEnvOSPatchVersion = ""  
 End Select    
  'Exception
  'If CSDVersion="" or InStr(CSDVersion,"Service Pack 1")<>0 Then
  ' strEnvOSPatch = "SP1"
  ' strEnvOSPatchVersion = "1"
  'End If
 
 'PrintDebug "strEnvOSName = " & strEnvOSName
 'PrintDebug "strEnvOSPatch = " & strEnvOSPatch
 'PrintDebug "strEnvOSPatchVersion = " & strEnvOSPatchVersion
 Set WshSysEnv = Nothing
 Set WshShell = Nothing
 Exit Sub
End If

'Windows NT 4.0
If CurrentVersion="4.0" Then
  'Checking Windows NT SP6a Exist
  SP6a=WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\Q246009\Installed")   
  strEnvOSName="NT4"
   Select Case CSDVersion
  Case "Service Pack 1"
   strEnvOSPatch = "SP1"
   strEnvOSPatchVersion = "1"   
  Case  "Service Pack 2"
   strEnvOSPatch = "SP2"
   strEnvOSPatchVersion = "2"   
  Case  "Service Pack 3"
   strEnvOSPatch = "SP3"
   strEnvOSPatchVersion = "3"   
  Case  "Service Pack 4"
   strEnvOSPatch = "SP4"
   strEnvOSPatchVersion = "4"   
  Case  "Service Pack 5"
   strEnvOSPatch = "SP5"
   strEnvOSPatchVersion = "5"   
  Case  "Service Pack 6"
   strEnvOSPatch = "SP6"
   strEnvOSPatchVersion = "6"  
  Case Else
   strEnvOSPatch = ""
   strEnvOSPatchVersion = ""  
 End Select    

 If  CSDVersion="Service Pack 6" and SP6a="1" Then
  strEnvOSPatch = "SP6A"
  strEnvOSPatchVersion = "6.1"
 End If
 
 'PrintDebug "strEnvOSName = " & strEnvOSName
 'PrintDebug "strEnvOSPatch = " & strEnvOSPatch
 'PrintDebug "strEnvOSPatchVersion = " & strEnvOSPatchVersion
 Set WshSysEnv = Nothing
 Set WshShell = Nothing
 Exit Sub
End If
'Windows 2003
If CurrentVersion="5.2" Then
 
   strEnvOSName="W3K"
   Select Case CSDVersion
  Case "Service Pack 1"
   strEnvOSPatch = "SP1"
   strEnvOSPatchVersion = "1"   
  Case  "Service Pack 2"
   strEnvOSPatch = "SP2"
   strEnvOSPatchVersion = "2"   
  Case Else
   strEnvOSPatch = ""
   strEnvOSPatchVersion = ""  
 End Select    
  'PrintDebug "strEnvOSName = " & strEnvOSName
  'PrintDebug "strEnvOSPatch = " & strEnvOSPatch
  'PrintDebug "strEnvOSPatchVersion = " & strEnvOSPatchVersion
  Set WshSysEnv = Nothing
  Set WshShell = Nothing
 Exit Sub
End If

Set WshSysEnv = Nothing
Set WshShell = Nothing
End Sub

'=============== Function Area ===========================
'
'=============== Function Area ===========================
Sub WriteLine(strLine)
On Error Resume Next
    objLogFile.WriteLine strLine
End Sub
Sub PrintScreen(strMessage)
Wscript.Echo strMessage
End Sub
Sub PrintDebug(strMessage)
If strTestMode=True Then
 Wscript.Echo strMessage
End If
End Sub
Sub Backup
'Setting Registry String
strRegistryType = "HKEY_CURRENT_USER"
   strRegistryLoc = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"   
strRegistryName = "ProxyServer" 
'Check exist registry
strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
PrintDebug "strRegistryFullPath = " & strRegistryFullPath 

strExistedValue = fnRegRead(strRegistryFullPath)
PrintDebug "Read Existed Registry Value = " & strExistedValue

'If client proxy server setting <> setting value the write the registry to client
If Trim(strExistedValue) <> strProxyServer Then

 'Write ProxyServer
 strRegistryName = "ProxyServer"
 strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
 blnResult = fnRegWrite(strRegistryFullPath,strProxyServer,"REG_SZ")
 PrintDebug "Write ProxyServer : " & blnResult
 
 'Write ProxyEnable
 strRegistryName = "ProxyEnable"
 strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
 blnResult = fnRegWrite(strRegistryFullPath,strProxyEnable,"REG_DWORD")
 PrintDebug "Write ProxyEnable : " & blnResult
   
 'Write ProxyOverride
 strRegistryName = "ProxyOverride"
 strRegistryFullPath = strRegistryType & "\" & strRegistryLoc & "\" & strRegistryName
 blnResult = fnRegWrite(strRegistryFullPath,strProxyOverride,"REG_SZ")
 PrintDebug "Write ProxyOverrider : " & blnResult
  
End If
End Sub
 
 
Post #: 1
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> Deploy Network Setting Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts