Vbsctips trouver l'utilisateur connecté sur un poste

Résolu
cs_Patrin Messages postés 16 Date d'inscription jeudi 14 septembre 2006 Statut Membre Dernière intervention 8 décembre 2006 - 1 déc. 2006 à 09:31
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 8 déc. 2006 à 18:18
Bonjour, j'ai fait un petit VBSCRIPT pour extraire l'utilisateur connecté sur une liste de postes dans excel. Le script lit le poste dans la première colonne d'excel et écrit dans la deuxième l'utilisateur. Techniquement ça fonctionne pour la première ligne mais pas pour les autres. C'est surement une connerie mais comme je suis débutant je n'arrive pas à trouver la solution...
<colgroup><col style=\"WIDTH: 76pt; mso-width-source: userset; mso-width-alt: 3693\" width=\"101\" /><col style=\"WIDTH: 116pt; mso-width-source: userset; mso-width-alt: 5632\" width=\"154\" /></colgroup>----
Computer, Users, ----
POSTE01, DOMAINE\USER
POSTE02         RIEN
POSTE03         RIEN

Voici mon script:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
    ("C:\Scripts\extraction.xls")
objExcel.Visible = True

intRow = 2

strComputer = objExcel.Cells(introw,1).Value
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer In colComputer
objExcel.Cells(introw,2).Value =  objComputer.UserName
Do Until objExcel.Cells(introw,1).Value = ""
intRow = intRow + 1
Loop
objExcel.Quit
next

9 réponses

cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
1 déc. 2006 à 10:49
Alors effectivement, j'avais mal lu ton script et ton besoin. Excuse-moi

Tu as bien besoin de la boucle Do...Loop. Ceci dit elle était mal placée.

Voila le script corrigé, mais pas testé, j'ai qu'un PC, pas de réseau

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\extraction.xls")
objExcel.Visible = True

intRow = 2
Do Until objExcel.Cells(introw,1).Value = ""
    strComputer = objExcel.Cells(intRow,1).Value
    Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
   
    Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
    For Each objComputer In colComputer
        objExcel.Cells(intRow,2).Value =  objComputer.UserName
        intRow = intRow + 1
    Next
Loop
objExcel.Quit

---- Sevyc64  (alias Casy) ----<hr size="2" width="100%" /># LE PARTAGE EST NOTRE FORCE #
3
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
8 déc. 2006 à 17:58
 Bonjour à tous,
bonjour Patrin,

J'ai repris le code ci-dessus en y rajoutant des objets.
Je ne savais même qu'il y avait des books sur wmi.
J'apprends le wmi sur:
http://www.secretswindows.com/index.php?rubrique=scripts&ssrubrique=WMI&page=./scripts/wmi/win32ntlogevent.htm
et supinfo, genre
http://www.supinfo-projects.com/fr/2004/wmi/4/

Const fichier = "d:\test_computer_excel.xls"
'
Dim objExcel    : Set objExcel = CreateObject("Excel.Application")
Dim objWorkbook : Set objWorkbook = objExcel.Workbooks.Open(Fichier)
objExcel.DisplayAlerts = False
objExcel.Visible = True
'
intRow = 2
Do Until objExcel.Cells(introw,1).Value = ""
    
    strComputer = objExcel.Cells(intRow,1).Value
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
    Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")    Set colAdapters objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled True")
   
   
    For Each objComputer In colComputer
    Set objWSHNetwork = CreateObject("WScript.NetWork")
        If Err.Number = 0 Then
           objExcel.Cells(intRow,2).Value =  objWSHNetWork.UserName
           objExcel.Cells(intRow,3).Value =  Ping(objComputer)
           WScript.Sleep 1400
           objExcel.Columns(2).AutoFit
           objExcel.Columns(3).AutoFit
           intRow = intRow + 1
           Else
           objExcel.Cells(intRow,2).Value =  objWSHNetWork.UserName
           objExcel.Cells(intRow,3).Value =  "NO PING"
           objExcel.Columns(2).AutoFit
           objExcel.Columns(3).AutoFit
           'WScript.Echo "server " & strComputer &vbCrLf& "code erreur: " &_
           '        CStr(Err.Number) &vbCrLf& " " & Err.Description
           Err.Clear
           intRow = intRow + 1
           Exit For
      End If
      Set objWSHNetwork = Nothing
    Next
Loop
WScript.Sleep 1400
objExcel.ActiveWorkbook.SaveAs Fichier,True
objExcel.Application.Visible = True
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = True



Set objWorkbook = Nothing
Set objExcel = Nothing
Set colComputer = Nothing
Set colAdapters = Nothing
Set objWMIService = Nothing



Function Ping(adresse)
Dim msg : msg =""
For Each objAdapter in colAdapters
    If Not IsNull(objAdapter.DefaultIPGateway) Then
       For i = LBound(objAdapter.DefaultIPGateway) To UBound(objAdapter.DefaultIPGateway)
           msg = objAdapter.DefaultIPGateway(i)
       Next
    End If
Next
Ping = msg
End Function





jean-marc
3
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
1 déc. 2006 à 09:48
Ton Do...Loop n'a aucune utilité ici

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
    ("C:\Scripts\extraction.xls")objExcel.Visible TrueintRow 2

strComputer = objExcel.Cells(introw,1).Value
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer In colComputer
    objExcel.Cells(introw,2).Value =  objComputer.UserName
<strike>    Do Until objExcel.Cells(introw,1).Value = ""</strike>
    intRow = intRow + 1
<strike>    Loop</strike>
<strike>    objExcel.Quit</strike>
next
objExcel.Quit

PS : Tu ne sauvegarde pas ton fichier Excel ???

---- Sevyc64  (alias Casy) ----<hr size="2" width="100%" /># LE PARTAGE EST NOTRE FORCE #
0
cs_Patrin Messages postés 16 Date d'inscription jeudi 14 septembre 2006 Statut Membre Dernière intervention 8 décembre 2006
1 déc. 2006 à 09:56
J'ai apporté les corrections mais le script ne va toujours pas lire la deuxième ligne d'excel... sans message d'erreur.

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
    ("C:\Scripts\extraction.xls")
objExcel.Visible = True



intRow = 2



strComputer = objExcel.Cells(introw,1).Value
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")



Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer In colComputer
objExcel.Cells(introw,2).Value =  objComputer.UserName
intRow = intRow + 1
Next
objExcel.Quit
0

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

Posez votre question
cs_Patrin Messages postés 16 Date d'inscription jeudi 14 septembre 2006 Statut Membre Dernière intervention 8 décembre 2006
1 déc. 2006 à 11:12
ça fonctionne très bien merci. La seule chose c'est que lorsqu'il n'arrive pas à trouver le poste il stop alors que j'aimerais qu'il continue... Je vais continuer à l'améliorer. Merci encore
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
1 déc. 2006 à 18:19
 Bonsoir à tous

Bonsoir Casy,

Pour tester du wmi (sans le wbem) sur poste local:
strComputer = "." qui figure dans presque tous les exemples.
Je viens d'adapter vos 2 posts.

'Le fichier excel contient:
'
' L2C1 = un point
' L3C1 = un point
'
' Pour utiliser du wmi sur poste local:
' strComputer = "." correspond au localeSetting, et qui est "." par defaut
' locale=LocaleID en wbem est une valeur de la forme "ms_xxxx" où xxxx est une valeur
' hexadécimale LCID (exemple : " ms_409 ").
'
' Array=("strComputer_1","strComputer_3","strComputer_X") sera utilisé par la méthode SWbemLocator
'
' WMI Scripting API :
' SWbemLocator : Obtient un objet SWbemServices pour obtenir l'accès à WMI sur un hôte local ou distant.
' SWbemServices: Crée, met à jour, et recherche les instances ou les classes d'un namespace sur un hôte local ou distant.
' SWbemObject  : Contient et manipule une classe d'objets simple ou instance WMI.
'
'
Const fichier = "d:\test_computer_excel.xls"
'
Dim objExcel    : Set objExcel = CreateObject("Excel.Application")
Dim objWorkbook : Set objWorkbook = objExcel.Workbooks.Open(Fichier)
objExcel.DisplayAlerts = False
objExcel.Visible = True
'
intRow = 2
Do Until objExcel.Cells(introw,1).Value = ""
    Dim strComputer, objWMIService, colComputer
    strComputer = objExcel.Cells(intRow,1).Value
    Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
    Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
    For Each objComputer In colComputer
        objExcel.Cells(intRow,2).Value =  Now & "     " & objComputer.UserName
        WScript.Sleep 1400
        objExcel.Columns(2).AutoFit
        intRow = intRow + 1
    Next
Loop
WScript.Sleep 1000
objExcel.ActiveWorkbook.SaveAs Fichier,True
objExcel.Application.Visible = True
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = True
Set objWorkbook = Nothing
Set objExcel = Nothing
Set colComputer = Nothing
Set objWMIService = Nothing

Quant à ta question, Patrin, je regarde car j'ai un exemple de script de prod sur x servers et domain.

jean-marc
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
1 déc. 2006 à 19:38
 Re,

Si server inaccessible (ou inconnu), il faut gérer les erreurs.
Pour tester, dans mon fichier, j'ai mis:
L2C1 = un point
L3C1 = toto
L4C1 = un point

' Pour connaitre le strComputer:
' Set WshShell = CreateObject("WScript.Shell")
' WScript.Echo "strComputer = " & WshShell.ExpandEnvironmentStrings("%ComputerName%")
' Set WshShell = Nothing



Const fichier = "d:\test_computer_excel.xls"
'
Dim objExcel    : Set objExcel = CreateObject("Excel.Application")
Dim objWorkbook : Set objWorkbook = objExcel.Workbooks.Open(Fichier)
objExcel.DisplayAlerts = False
objExcel.Visible = True
'
intRow = 2
Do Until objExcel.Cells(introw,1).Value = ""
    
    strComputer = objExcel.Cells(intRow,1).Value
    On Error Resume Next
    Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
    Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
 
     For Each objComputer In colComputer
         If Err.Number = 0 Then ' réponse pour Patrin
            WScript.Echo "server connecté"  &vbCrLf& strComputer
            objExcel.Cells(intRow,2).Value =  Now & "     " & objComputer.UserName
            WScript.Sleep 1400
            objExcel.Columns(2).AutoFit
            intRow = intRow + 1
            Else
            objExcel.Cells(intRow,2).Value =  Now & "     " & strComputer & " inaccessible"
            WScript.Echo "server " & strComputer &vbCrLf& "code erreur: " &_
                    CStr(Err.Number) &vbCrLf& " " & Err.Description,,"Server inaccessible"
            Err.Clear
            intRow = intRow + 1
            Exit For
        End If
    Next
Loop
WScript.Sleep 1000
objExcel.ActiveWorkbook.SaveAs Fichier,True
objExcel.Application.Visible = True
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = True
Set objWorkbook = Nothing
Set objExcel = Nothing
Set colComputer = Nothing
Set objWMIService = Nothing


jean-marc
0
cs_Patrin Messages postés 16 Date d'inscription jeudi 14 septembre 2006 Statut Membre Dernière intervention 8 décembre 2006
8 déc. 2006 à 15:10
Salut Jean-Marc, yé pas mal hot ton script. Une autre petite chose. J'aimerais avoir seulement l'utilisateur connecté et pas la date ni domain\ dans la colone 2. Aussi par contrôle d'erreur c'est bien mais j'aimerais aussi que le script écrive dans la colone 3 l'adresse IP et si il ne ping pas NO PING.

Je suis débutant en WMI mais je me suis acheté deux livres pour ne plus embêter les gens avec mes problèmes et être autonome en script...

Merci
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
8 déc. 2006 à 18:18
 Re,

il faut enlever le "Exit For" qui ne sert à rien.

jean-marc
0
Rejoignez-nous