Script pour modifier des entrées DNS d'une carte réseau
Bonjour,
Je recherche à faire un script pour modifier la configuration DNS des cartes réseaux.
L'idée est donc de déterminer les cartes réseaux présents sur le poste, puis de supprimer toutes les entrées DNS pour les remplacer par d'autres.
On m'a dit que cela n'était pas possible à réaliser.
Avez-vous une idée comment je pourrai faire cela ?
J'ai retrouvé un de mes scripts qui change les DNS d'un poste.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
'
rep = Msgbox ("Info : A utilisé en mode ligne de commande seulement (cscript.exe ChangeDNSDistant.vbs)." & vbCrLf & "executer cmd en administrateur" & vbCrLf & vbCrLf & "Voulez-vous continuer ?", vbYesNo, "Lancer le script ?")
If (rep = vbYes) then
dim DNS1, DNS2
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oTS = oFS.OpenTextFile("computers.txt")
arrServers = Split( strParamServers, " " )
DNS1 = DNSChoice("DNS n°1 : ")
DNS2 = DNSChoice("DNS n°2 : ")
WScript.echo vbCrlf 'Saut de ligne
Do Until oTS.AtEndOfStream
sComputer = oTS.ReadLine
Showdns sComputer
Do
choice = MakeChoice( "Voulez-vous modifier le DNS n°1 : " & DNS1 & " et le DNS n°2 : " & DNS2 & " (O/N)?" )
Loop until choice ="O" or choice = "N"
if choice = "O" then
WScript.echo vbCrlf 'Saut de ligne
Setdns sComputer,DNS1,DNS2
else
WScript.Quit
end if
WScript.echo( vbCrlf )
Loop
oTS.Close
else
Msgbox "Opération annulé !"
end If
' ############ Quitter le script une fois terminé ############
WScript.Quit
' ############ Sous fonction pour auditer la configuration DNS du poste distant ############
Sub Showdns(strServer)
strComputer = strServer
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set collection_Network = objWMI.ExecQuery("Select Caption from Win32_NetworkAdapter where AdapterType=""Ethernet 802.3"" and ServiceName<>""VMnetAdapter"" ")
strResult = ""
For each objNetworkCard In collection_Network
Set collection_Nics = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where Caption=""" & objNetworkCard.Caption & """")
For each objNIC In collection_Nics
If NOT (IsNull(objNIC.DNSServerSearchOrder) OR IsEmpty(objNIC.DNSServerSearchOrder)) Then
strResult = strResult & " - " & objNIC.Description & " : " & vbCrlf & " "
n = 1
For Each strDns In objNIC.DNSServerSearchOrder
strResult = strResult & vbTab & "DNS n°" & n & " : " & strDns & " "
n = n + 1
Next
strResult = strResult & vbCrlf
End If
Next
Next
WScript.echo strResult
WScript.echo vbCrlf
End Sub
' ############ Sous fonction pour modifier la configuration DNS du poste distant ############
Sub Setdns( strServer, DNS1, DNS2)
strWinMgmt = "winmgmts:{impersonationLevel=impersonate}!//"& strServer &""
Set objNICs = GetObject( strWinMgmt ).InstancesOf( "Win32_NetworkAdapterConfiguration" )
WScript.echo "Changement des DNS pour : "
For Each objNIC In objNICs
If objNIC.IPEnabled Then
objNIC.SetDNSServerSearchOrder Array(DNS1,DNS2)
WScript.echo " - " & objNIC.Description & vbCrlf
End If
Next
WScript.echo vbCrlf
End Sub
' ############ Fonction pour afficher du texte sur le promt : Yes/No ############
Function MakeChoice(strMsg)
WScript.StdOut.Write(strMsg)
WScript.StdIn.Read(0)
strChoice = WScript.StdIn.ReadLine()
MakeChoice = UCase( Left( strChoice, 1 ) )
End Function
' ############ Fonction pour afficher du texte sur le promt ############
Function DNSChoice(strMsg)
WScript.StdOut.Write(strMsg)
WScript.StdIn.Read(0)
DNSChoice = WScript.StdIn.ReadLine()
End Function |
Le seul soucie, est que ce script change le DNS sur TOUTES les cartes réseaux.
Il faudrait que je le face que sur les cartes filaires et qui sont actives.
Dans la fonction "Showdns", j'arrive à afficher que les cartes filaires (il faudrait voir pour sélectionné que celle qui sont active)
mais dans la fonction "Setdns", je n'arrive pas à faire cette restriction.
Auriez vous une idée car je ne suis pas très fort pour faire des requêtes WMI en VBS...
Merci d'avance.