Génération de fichier de log

bozoteam01 Messages postés 14 Date d'inscription jeudi 2 juillet 2009 Statut Membre Dernière intervention 18 septembre 2009 - 16 juil. 2009 à 08:08
bozoteam01 Messages postés 14 Date d'inscription jeudi 2 juillet 2009 Statut Membre Dernière intervention 18 septembre 2009 - 18 sept. 2009 à 11:23
Bonjour,

Je travail sur un script pour l'automatisation de la configuration des adresses de dns pour plusieurs serveurs.
Le script fonctionne bien je souhaiterais maintenant générer une log qui me renseigne (en txt ou xls peut importe) les serveurs n'ayant pas étaient configuré en gros je voudrais vérifier que mon script et bien passer sur tous les serveurs sinon il me faut le nom du serveur qui n'est pas passer.

Merci par avance.

Voila mon script :

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Code VBS - Automatisation de la configuration DNS ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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

3 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
16 juil. 2009 à 09:08
Allez, je colorie un peu la chose (c'est automatique, bozoteam01, dommage de s'en priver....)

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



Renfield Admin CodeS-SourceS - MVP Visual Basic
0
bozoteam01 Messages postés 14 Date d'inscription jeudi 2 juillet 2009 Statut Membre Dernière intervention 18 septembre 2009
14 août 2009 à 12:14
J'ai trouver un code sur http://www.microsoft.com/technet/scriptcenter/topics/networking/05_atnc_dns.mspx

Qui remplace le nom de domaine avec la gestion de sous domaine ainsi qu'en fonction de se sous domaine il renseigne les DNS

Il utilise un fichier txt (ici hosts.txt) comprenant le nom de machine suivis du nom de domaine ex:

Client1,hr

"hr" étant le sous domaine du domaine fabrikam.com (dans l'exemple ci dessous)

Puis qui renseigne dans une log les erreurs rencontrés (ici newhosts.csv)

Je voudrais modifier ce script pour qu'il ne prennent plus en compte le domaine et le sous domaine simplement renseigner le dns via le script hosts.txt et envoyer les infos dans le fichier newhosts.csv

J'ai tenté de supprimer les lignes correspondants au parametrage et a la lecture du domaine mais cela ne fonctionne.

Si vous avez des solutions à me communiquez je suis prenneur

 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

 
0
bozoteam01 Messages postés 14 Date d'inscription jeudi 2 juillet 2009 Statut Membre Dernière intervention 18 septembre 2009
18 sept. 2009 à 11:23
up je suis toujours bloqué sur mon problème.
0
Rejoignez-nous