Script VBS pour récupérer les info sur les HDD d'une active directory

helios49 Messages postés 6 Date d'inscription mardi 3 février 2009 Statut Membre Dernière intervention 7 août 2016 - 4 févr. 2009 à 10:00
zangiefgold Messages postés 2 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 7 février 2009 - 7 févr. 2009 à 14:33
Salut ^^

J'ai ici un script qui me permet de récupérer les info des disques
durs des postes d'une active directory... J'exécute donc ce script sur
mon serveur de domaine afin de récupérer ces info dans un fichier
excel.

Mais j'ai quelques problèmes!

Mon scripts ne gère pas du tout les postes avec plusieurs disques
durs ou partitions... Je veux dire qu'il ne prend en compte qu'un seul
disque dur! Problème ^^

Et lorsque l'un de mes PC de l'active directory est déconnecté mon script plante...

Voici mon script, si quelqu'un pouvait y jeter un ti coup de n'oeil ça serait super ^^

Merci!




'----------------------------------------------------------------------------------------c
onnexion active directory

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection

Set objRootDSE = GetObject("LDAP://rootDSE")

objCommand.CommandText = _

"Select Name from 'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' " & "Where objectClass='computer'"

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

'---------------------------------------------------------------------------------------connexion au fichier excel

set oexapp = wscript.createobject("Excel.Application")

set owshshell = wscript.createobject("wscript.shell")

oexapp.visible = false

set oexwk = oexapp.workbooks.open("C:infopc.xls")

'---------------------------------------------------------------------------------------effacement des données

oexapp.worksheets(1).cells(2,2).value = "NOM DU PC"

oexapp.worksheets(1).cells(2,2).Interior.ColorIndex = 27

oexapp.worksheets(1).cells(2,3).value = "ADRESSE IP"

oexapp.worksheets(1).cells(2,3).Interior.ColorIndex = 27

oexapp.worksheets(1).cells(2,4).value = "MAC"

oexapp.worksheets(1).cells(2,4).Interior.ColorIndex = 27

oexapp.worksheets(1).cells(2,5).value = "Lettre du Disque"

oexapp.worksheets(1).cells(2,5).Interior.ColorIndex = 27

oexapp.worksheets(1).cells(2,6).value = "Espace TOTAL"

oexapp.worksheets(1).cells(2,6).Interior.ColorIndex = 27

oexapp.worksheets(1).cells(2,7).value = "Espace Libre"

oexapp.worksheets(1).cells(2,7).Interior.ColorIndex = 27

oexapp.worksheets(1).cells(2,8).value = "Espace Libre %"

oexapp.worksheets(1).cells(2,8).Interior.ColorIndex = 27

x=3

nbligne=1

j=3

p=2

while oexapp.worksheets(1).cells(j,2).value <> ""

oexapp.worksheets(1).cells(j,2).Interior.ColorIndex = 2

for m=2 to 7

oexapp.worksheets(1).cells(j,m).value = ""

next

j=j+1

wend

while oexapp.worksheets(2).cells(p,1).value <> ""

for m=1 to 2

oexapp.worksheets(2).cells(p,m).value = ""

next

p=p+1

wend

'--------------------------------------------------------------------------------------- commencement de la boucle

Do Until objRecordSet.EOF

'---------------------------------------------------------------------------------------récupération du nom du pc

pcname = objRecordSet.Fields("Name").Value

oexapp.worksheets(1).cells(x,2).value = pcname

'---------------------------------------------------------------------------------------connexion au pc à distance

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & pcname & "rootcimv2")

'---------------------------------------------------------------------------------------si connexion échoué

if err.number <> 0 then

oexapp.worksheets(1).cells(x,2).Interior.ColorIndex = 46

oexapp.worksheets(1).cells(x,3).value ="Impossible de joindre l hote"

oexapp.worksheets(1).cells(x,4).value ="Impossible de joindre l hote"

oexapp.worksheets(1).cells(x,5).value ="Impossible de joindre l hote"

oexapp.worksheets(1).cells(x,6).value ="Impossible de joindre l hote"

oexapp.worksheets(1).cells(x,7).value ="Impossible de joindre l hote"

x=x+1

'--------------------------------------------------------------------------------------si connexion réussi

else

oexapp.worksheets(1).cells(x,2).Interior.ColorIndex = 37

'--------------------------------------------------------------------------------------récupération de l'IP et du mac

Set IPConfigSet = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

For Each IPConfig in IPConfigSet

oexapp.worksheets(1).cells(x,3).value =IPConfig.IPAddress

oexapp.worksheets(1).cells(x,4).value =IPConfig.MACAddress

Next

'--------------------------------------------------------------------------------------récupération des données système

Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

Set colDisks = objWMIService.ExecQuery _

("Select * from Win32_LogicalDisk Where DriveType = 3")

For Each objDisk in colDisks

intFreeSpace = objDisk.FreeSpace/1024^3

intTotalSpace = objDisk.Size/1024^3

pctFreeSpace = intFreeSpace / intTotalSpace

oexapp.worksheets(1).cells(x,5).value =objDisk.DeviceID

oexapp.worksheets(1).cells(x,6).value =intTotalSpace

oexapp.worksheets(1).cells(x,7).value =intFreeSpace

oexapp.worksheets(1).cells(x,8).value =pctFreeSpace

Next

trouve=faux

k=1

while k<=nbligne and trouve=faux

if oexapp.worksheets(2).cells(k,1).value = objOperatingSystem.Caption then

oexapp.worksheets(2).cells(k,2).value = oexapp.worksheets(2).cells(k,2).value+1

trouve=vrai

end if

k=k+1

wend

if trouve = faux then

nbligne=nbligne+1

oexapp.worksheets(2).cells(nbligne,1).value = objOperatingSystem.Caption

oexapp.worksheets(2).cells(nbligne,2).value=1

end if

x=x+1

Next

end if

'--------------------------------------------------------------------------------------bouge au suivant

err.clear

objRecordSet.MoveNext

Loop

'--------------------------------------------------------------------------------------sauvegarde et fermeture du fichier

oexwk.save

oexwk.close

Wscript.Echo "FIN DU SCRIPT! Vos resultats ont ete place dans votre fichier Excel"

6 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
4 févr. 2009 à 10:43
tes infos viennent de WMI, pas AD.
bref.

gaffe au theme, tu es en VBS, pas en .NET

concernant les postes possédant plusieurs disques, ton erreur est de réécrire toujours dans la même ligne (tu ecrases les infos des premiers disques)

Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk in colDisks
intFreeSpace = objDisk.FreeSpace/1024^3
intTotalSpace = objDisk.Size/1024^3
pctFreeSpace = intFreeSpace / intTotalSpace

oexapp.worksheets(1).cells(x,5).value =objDisk.DeviceID
oexapp.worksheets(1).cells(x,6).value =intTotalSpace
oexapp.worksheets(1).cells(x,7).value =intFreeSpace
oexapp.worksheets(1).cells(x,8).value =pctFreeSpace
Next

incrémente x ^^
0
helios49 Messages postés 6 Date d'inscription mardi 3 février 2009 Statut Membre Dernière intervention 7 août 2016
5 févr. 2009 à 16:23
arf désolé pour le thème je savais pas trop où poster tellement ya de thèmes ^^

merçi beaucoup pour ta solution!

J'ai trouvé une autre erreur, lorsque l'un de mes postes n'ai pas connecté, mon script me renvoit une erreur et n'arrive pas à poursuivre....

Je pense que l'erreur doit être en rapport avec ces lignes là....

'---------------------------------------------------------------------------------------si connexion échoué
if err.number <> 0 then
oexapp.worksheets(1).cells(x,2).Interior.ColorIndex = 46
oexapp.worksheets(1).cells(x,3).value ="Impossible de joindre l hote"
oexapp.worksheets(1).cells(x,4).value ="Impossible de joindre l hote"
oexapp.worksheets(1).cells(x,5).value ="Impossible de joindre l hote"
oexapp.worksheets(1).cells(x,6).value ="Impossible de joindre l hote"
oexapp.worksheets(1).cells(x,7).value ="Impossible de joindre l hote"
x=x+1

Quelqu'un voit il le truc???

Merci
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
5 févr. 2009 à 16:26
ajoutes un On Error Resume Next, pour relever et tester l'erreur, sans que ca ne crashe
0
zangiefgold Messages postés 2 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 7 février 2009
7 févr. 2009 à 10:34
bonjours a tous

mon tuteur de stage ma demander de crée plusieurs script dont un comme celui la. comme je débute dans les script je me comprend pas  " On Error Resume Next " comment sa marche.
serai t-il possible que l'un puis m'aider a corriger ce script.

merci  
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
7 févr. 2009 à 13:49
tu fais:

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & pcname & "rootcimv2")

'--------------------------si connexion échoué
if err.number <> 0 then
....

en fait, tu tentes de te connecter
mais si une erreur surviens, l'execution s'arrete.

ajoutes un 'On Error Resume Next' avant, pour demander a VB
de continuer l'execution, même si une erreur surviens.
0
zangiefgold Messages postés 2 Date d'inscription samedi 7 février 2009 Statut Membre Dernière intervention 7 février 2009
7 févr. 2009 à 14:33
merci pour tout

connais tout une fonction pour remettre a zéro la fichier xls
si j'abuse pas tops, serai cool

merci comme même 
0
Rejoignez-nous