Option Explicit On Error Resume Next Dim objFso, objTextStream Dim strPathFile Dim NomPC, objWMIService Dim objItem, colItems, strComputer Dim errEnable, strDNS strDNS = array("*.*.*.*","*.*.*.*","*.*.*.*")'Tableau de chaîne, la premiére case du tableau contien l'adresse dns principale etc strPathFile = "C:\monchemin\toto.txt" ' Chemin ou ce trouve la liste des nom de serveurs Set objFso = CreateObject("Scripting.FileSystemObject") ' Création d'objet pour la lecture du fichier txt Set objTextStream = objFso.OpenTextFile(strPathFile, 1) Do While Not objTextStream.AtEndOfStream ' Boucle pour lister le fichier txt strComputer = objTextStream.ReadLine Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2") 'utilisation d'un appel GetObject pour se connecter au WMI root\cimv2. 'Recuperation du nom du poste Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)'Execution d'une requete dans le systéme en vue de récuperer le nom 'utilisation de la méthode ExecQuery pour mettre en route la classe Win32_Service. For Each objItem in colItems 'Je parcour chaque objet rétourner par la requête jusqu'a ce que je retrouve caption pour affecter le resultat a NomPC NomPC = objItem.Caption Next Set objWMIService = Nothing 'Libere le contenu de objWMIService pour le remettre a 0 Set colItems = Nothing 'Libere le contenu de colItems pour le remettre a 0 Set objWMIService = getObject("winmgmts:\" & NomPC & "\root\cimv2") Set colItems objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled true",,48) ' Requête For Each objItem in colItems 'configure les adresses DNS errEnable = objItem.SetDNSServerSearchOrder(strDNS) Next Loop
On Error Resume Next ' Constants for FileSystemObject Const FOR_READING = 1 Const FOR_WRITING = 2 Const FOR_APPENDING = 8 ' Paths to input and output files. Input file must be text file with name ' of accessible machine, comma, and valid domain name on each line. strFileInput = "c:\scripts\hosts.txt" strFileOutput = "c:\scripts\newhosts.csv" ' Domain name and DNS servers corresponding to subdomains strTopDomain = "fabrikam.com" arrDNSSrv1 = Array("192.168.0.1", "192.168.0.2", "192.168.0.3") 'hr arrDNSSrv2 = Array("192.168.0.2", "192.168.0.3", "192.168.0.1") 'it arrDNSSrv3 = Array("192.168.0.3", "192.168.0.1", "192.168.0.2") 'ac ' Create a Script Runtime FileSystemObject. Set objFSO = CreateObject("Scripting.FileSystemObject") ' If the input file exists, dump contents of input file into a string. strInput = GetInput(strFileInput) ' Get computer info by breaking input stream into array at line breaks. arrHosts = Split(strInput, VbCrLf) ' Check to see if the output file exists. If so, open it for appending. ' If not, create it and open it for writing. If objFSO.FileExists(strFileOutput) Then Set objOutputFile = objFSO.OpenTextFile (strFileOutput, FOR_APPENDING) Else Set objOutputFile = objFSO.CreateTextFile(strFileOutput) End If If Err <> 0 Then Wscript.Echo "Unable to open " & strFileOutput & " for output." WScript.Quit End If ' Write header for file. objOutputFile.WriteLine "Inventory of computers moved to new domains." & _ VbCrLf & "Taken " & Now & VbCrLf & VbCrLf & String(120, "-") & VbCrLf For Each strHost In arrHosts ' Get names and domains by breaking each computer info array at commas. arrComputers = Split(strHost, ",") strComputer = arrComputers(0) strSubDomain = arrComputers(1) strDomain = strSubDomain & "." & strTopDomain WScript.Echo VbCrLf & "Host: " & strComputer & VbCrLf ' Ping remote computer. If no response display error message and end script blnPingSuccess = PingClient(strComputer) If blnPingSuccess = True Then ' Connect to the WMI service. Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2") If Err = 0 Then ' Write header for each computer. objOutputFile.WriteLine "Host:,," & strComputer & VbCrLf & VbCrLf & _ ", Settings before operations:" & VbCrLf ' Get current settings and write to output file. GetSettings objOutputFile.WriteLine VbCrLf & "," & String(60, "-") ' Change domain and DNS search order. ChangeDNS ' Get settings after changes and write to output file. objOutputFile.WriteLine VbCrLf & "," & String(60, "-") & VbCrLf & _ VbCrLf & ",Settings after operations:" & VbCrLf GetSettings objOutputFile.WriteLine VbCrLf & String(120, "-") & VbCrLf Else objOutputFile.WriteLine "Host: " & strComputer & VbCrLf _ & VbCrLf & "Error connecting to WMI service." & VbCrLf & _ "Error Number: " & Err.Number & VbCrLf & _ "Error Source: " & Err.Source & VbCrLf & _ "Error Description: " & Err.Description & VbCrLf & VbCrLf & _ String(120, "-") & VbCrLf Err.Clear End If Else objOutputFile.WriteLine "Host: " & strComputer & VbCrLf _ & VbCrLf & ",Unable to connect." & VbCrLf & VbCrLf & _ String(120, "-") & VbCrLf End If Next ' Write footer for file and close. objOutputFile.WriteLine String(64, "=") & VbCrLf objOutputFile.Close WScript.Echo "Inventory complete. Data written to " & strFileOutput & "." '************************************************************************ Function GetInput(strFileInput) ' Check to see if the input file exists. ' If so, dump contents of input file into a string. If objFSO.FileExists(strFileInput) Then Set objInputFile = objFSO.GetFile(strFileInput) If objInputFile.Size > 0 Then Set objInputFile = objFSO.OpenTextFile(strFileInput, FOR_READING) strInputStream = objInputFile.ReadAll objInputFile.Close GetInput = strInputStream Else Wscript.Echo strFileInput & " is empty." WScript.Quit End If Else WScript.Echo strFileInput & " does not exist on this computer." WScript.Quit End If End Function '************************************************************************ Function PingClient(strComputer) Set objShell = CreateObject("WScript.Shell") Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer) strPingResults = LCase(objExec.StdOut.ReadAll) If InStr(strPingResults, "reply from") Then PingClient = True Else PingClient = False End If End Function '************************************************************************ Sub GetSettings Set colCompSystems = objWMIService.ExecQuery _ ("SELECT * FROM Win32_ComputerSystem") For Each objCompSystem in colCompSystems objOutputFile.WriteLine VbCrLf & ",Domain:,," & objCompSystem.Domain Next Set colNicConfigs = objWMIService.ExecQuery("SELECT " & _ "* FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True") For Each objNicConfig In colNicConfigs objOutputFile.WriteLine VbCrLf & ",Network Adapter " & _ objNicConfig.Index & VbCrLf & _ ",," & objNicConfig.Description & VbCrLf & _ ",,DNS Domain:,," & objNicConfig.DNSDomain strRow = ",,DNS Server Search Order:,," If Not IsNull(objNicConfig.DNSServerSearchOrder) Then For Each strDNSServer In objNicConfig.DNSServerSearchOrder strRow = strRow & strDNSServer & ",," Next End If objOutputFile.WriteLine strRow strRow = ",,IP Addresses:,," If Not IsNull(objNicConfig.IPAddress) Then For Each strIPAddress In objNicConfig.IPAddress strRow = strRow & strIPAddress & ",," Next End If objOutputFile.WriteLine strRow strRow = ",,Subnet Masks:,," If Not IsNull(objNicConfig.IPSubnet) Then For Each strIPSubnet In objNicConfig.IPSubnet strRow = strRow & strIPSubnet & ",," Next End If objOutputFile.WriteLine strRow Next End Sub '************************************************************************ Sub ChangeDNS Set colNicConfigs = objWMIService.ExecQuery("SELECT " & _ "* FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True") ' Test that child domain from input file is valid. errSubDomain = False Select Case strSubDomain Case "hr" arrDNSServers = arrDNSSrv1 Case "it" arrDNSServers = arrDNSSrv2 Case "ac" arrDNSServers = arrDNSSrv3 Case Else errSubDomain = True End Select If errSubDomain = False Then For Each objNicConfig In colNicConfigs objOutputFile.WriteLine VbCrLf & ",Network Adapter " & _ objNicConfig.Index & VbCrLf & _ ",," & objNicConfig.Description & VbCrLf & VbCrLf & _ ",,Attempting to make changes in DNS configuration ..." intDomain = objNicConfig.SetDNSDomain(strDomain) If intDomain = 0 Then objOutputFile.WriteLine ",,Assigned new domain." intServers = objNicConfig.SetDNSServerSearchOrder _ (arrDNSServers) If intServers = 0 Then objOutputFile.WriteLine ",,Assigned new DNS server " & _ "search order." Else objOutputFile.WriteLine ",,Unable to assign new DNS " & _ "server search order." End If Else objOutputFile.WriteLine ",,Unable to assign new domain or " & _ "DNS server search order." End If Next Else objOutputFile.WriteLine "Subdomain" & strSubDomain & _ "could not be found." End If End Sub