REM Récupérer les imprimantes utilisateur
REM Lancer ce fichier avec un fichier lancer_le_scan_des_PC.bat comme tel :
REM copy imprimantes_utilisateurs.log imprimantes_utilisateurs.bak
REM del imprimantes_utilisateurs.log
REM cscript go.vbs
REM notepad imprimantes_utilisateurs.log
REM del imprimantes_utilisateurs.log_2.txt
REM del pingOK.tmp
REM del sid_user.log
REM del regedit_query.log.tmp
REM ***************************************************
REM Fichier : imprimantes_utilisateur.vbs
REM Description : pour chaque ordinateur de liste_PC_imprimantes_utilisateurs.txt,
REM 1. chercher user logué
REM 2. chercher ses imprimantes (+défaut)
REM Fichiers nécessaires : dsquery.exe et dsget.exe
REM ***************************************************
'pour codes WshShell.Run : http://www.commentcamarche.net/contents/vbscript/vbs-wshshell.php3
Const LOG_FILE = "imprimantes_utilisateurs.log"
Const LOG_FILE_HTM = "imprimantes_utilisateurs.htm"
Const LOG_FILE_TMP = "regedit_query.log.tmp"
Set WshShell = WScript.CreateObject("WScript.Shell")
set oFSO = CreateObject("Scripting.FileSystemObject")
Dim toutHTML
Demarrer
Sub Demarrer
toutHTML = "<html> <table border=1> " & vbcrlf & _
"<tr> <td>Ordinateur</td>" & vbcrlf & _
" <td>User</td>" & vbcrlf & _
" <td>Imprimantes</td>" & vbcrlf & _
" <td>Impr. défaut</td>" & vbcrlf & _
"</tr>"
Traiter_Liste_TXT
toutHTML = toutHTML & vbcrlf & " </table> </html>"
toutHTML = Replace(toutHTML, "hp LaserJet 1160", "<font size=+2><b>hp LaserJet 1160</b></font>")
SaveToFile toutHTML, LOG_FILE_HTM
Msgbox "Fin." & vbcrlf & _
"Les fichiers " & LOG_FILE & " et " & LOG_FILE_HTM & " contiennent les résultats."
End Sub
Sub Traiter_Liste_TXT
Set f = oFSO.OpenTextFile("liste_PC_imprimantes_utilisateurs.txt")
rAll = f.ReadAll
t = Split(rAll, vbCrLf)
nbOrdi = UBound(t) + 1
Wscript.Echo nbOrdi & " ordinateurs vont être analysés."
'0. traiter les computers un par un depuis liste.txt
For i = 0 To nbOrdi-1
u = Split(t(i), ";")
strComputer = u(0)
toutHTML = toutHTML & vbcrlf & _
"<tr>"
'1. afficher ordinateur
Append_LOG_File strComputer
If PingOK(strComputer) = TRUE Then
Wscript.Echo "Ping OK"
'2. rechercher user logué
le_user_logue = ""
le_user_logue = UserLogue(strComputer)
If le_user_logue <> "" Then
Append_LOG_File le_user_logue
'3. afficher son SID
le_SID_user = SID_user(le_user_logue)
'Append_LOG_File le_SID_user
'4. rechercher imprimantes installées
'Append_LOG_File "4. Imprimantes installées : "
cle = "\\" & strComputer & "\HKEY_USERS\" & le_SID_user & "\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Devices"
cle = chr(34) & cle & chr(34)
If oFSO.FileExists(LOG_FILE_TMP) Then oFSO.DeleteFile LOG_FILE_TMP
cmdLine = "cmd /c reg query " & cle & " | findstr REG_SZ > " & LOG_FILE_TMP
Append_LOG_File_2 cmdLine
return = WshShell.Run (cmdLine, 0, TRUE)
'Wscript.Echo "cmdline =" & vbcrlf & cmdline
impr_inst = PrendreAvantREGSZ
Append_LOG_File impr_inst
'5. rechercher imprimante par défaut
'Append_LOG_File "5. Imprimante par défaut : "
cle = "\\" & strComputer & "\HKEY_USERS\" & le_SID_user & "\software\microsoft\windows nt\currentversion\windows"
cle = chr(34) & cle & chr(34)
If oFSO.FileExists(LOG_FILE_TMP) Then oFSO.DeleteFile LOG_FILE_TMP
cmdLine = "cmd /c reg query " & cle & " /v device | findstr /i device > " & LOG_FILE_TMP
Append_LOG_File_2 cmdLine
return = WshShell.Run (cmdLine, 0, TRUE)
impr_def = PrendreApresREGSZ
Append_LOG_File impr_def
End If
'6. séparateur
Else
Wscript.Echo "Ping NOK pour " & strComputer
End If
'Append_LOG_File "======================="
toutHTML = toutHTML & vbcrlf & "</tr>"
Next
End Sub
Sub Append_LOG_File(s)
Set f = oFSO.OpenTextFile(LOG_FILE, 8, True) '8 = For Appending
f.WriteLine(s)
Wscript.Echo s
f.Close
Set f = Nothing
toutHTML = toutHTML & vbcrlf & _
"<td>" & Replace(s, vbcrlf, "<BR>") & "</td>"
End Sub
Sub Append_LOG_File_2(s)
Set f = oFSO.OpenTextFile(LOG_FILE & "_2.txt", 8, True) '8 = For Appending
f.WriteLine(s)
Wscript.Echo s
f.Close
Set f = Nothing
End Sub
Function PrendreAvantREGSZ
on error resume next
REM PDFCreator REG_SZ winspool,Ne00:
REM Microsoft XPS Document Writer REG_SZ winspool,Ne01:
REM Microsoft Office Document Image Writer REG_SZ winspool,Ne02:
REM CutePDF Writer REG_SZ winspool,CPW2:
Set f = oFSO.OpenTextFile(LOG_FILE_TMP)
Do
a = f.ReadLine
PrendreAvantREGSZ = _
PrendreAvantREGSZ & vbCrLf & _
" - " & Trim(Left(a, Instr(a, "REG_SZ") - 1 ))
Loop Until f.AtEndOfStream
f.Close
Set f = Nothing
If Left(PrendreAvantREGSZ, 2) = vbcrlf Then PrendreAvantREGSZ = Right(PrendreAvantREGSZ, Len(PrendreAvantREGSZ) - 2)
End Function
Function PrendreApresREGSZ
on error resume next
'Device REG_SZ PDFCreator,winspool,Ne00:
Set f = oFSO.OpenTextFile(LOG_FILE_TMP)
a = f.ReadLine
a = Right(a, Len(a) - Instr(a, "REG_SZ") - 6) 'PDFCreator,winspool,Ne00:
a = Left(a, Instr(a, ",") - 1)
PrendreApresREGSZ = " - " & Trim(a)
f.Close
Set f = Nothing
End Function
Function UserLogue(sComputer)
'sComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
UserLogue = objComputer.UserName
REM Wscript.Echo "User Name = " & objComputer.UserName _
REM & VBNewLine & "Computer Name = " & objComputer.Name
REM WScript.Echo objComputer.UserName
Next
End Function
Function SID_User(sUser)
sSamid = Right(sUser, Len(sUser) - Instr(sUser, "\"))
cmdLine = "cmd /c dsquery user -samid " & sSamid & " | dsget user -sid | findstr S- > sid_user.log"
'Wscript.Echo "cmdline =" & vbcrlf & cmdline
return = WshShell.Run (cmdLine, 0, TRUE)
set f = oFSO.OpenTextFile("sid_user.log")
SID_User = f.ReadAll
SID_User = Replace(SID_User, chr(10), "")
SID_User = Replace(SID_User, chr(13), "")
SID_User = Trim(SID_User)
'Msgbox "SID_User = '" & SID_User & "'"
'Wscript.Echo "SID user " & sUser & " = " & SID_User
f.Close
'dsquery user -name *sSmaid* | dsget user -sid | findstr S- > sid_user.log
'dsquery user -name samid | dsget user -sid | findstr S- > sid_user.log
End Function
Function PingOK(sHost)
If oFSO.FileExists("pingOK.tmp") Then oFSO.DeleteFile "pingOK.tmp"
cmdLine = "cmd /c ping " & sHost & " -n 2 > pingOK.tmp"
return = WshShell.Run (cmdLine, 0, TRUE)
set f = oFSO.OpenTextFile("pingOK.tmp")
tout = f.ReadAll
If Instr(tout, "perdus = 0") Then
PingOK = TRUE
Else
PingOK = FALSE
End If
End Function
Function PingOK_old(sHost)
' Dim sHost 'name of Windows XP computer from which the PING command will be initiated
Dim sTarget 'name or IP address of remote computer to which connectivity will be tested
Dim cPingResults 'collection of instances of Win32_PingStatus class
Dim oPingResult 'single instance of Win32_PingStatus class
Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
"WHERE Address = '" + sTarget + "'")
For Each oPingResult In cPingResults
If oPingResult.StatusCode = 0 Then
PingOK = TRUE
If LCase(sTarget) = oPingResult.ProtocolAddress Then
'WScript.Echo sTarget & " is responding"
Else
'WScript.Echo sTarget & "(" & oPingResult.ProtocolAddress & ") is responding"
End If
'Wscript.Echo "Bytes = " & vbTab & oPingResult.BufferSize
'Wscript.Echo "Time (ms) = " & vbTab & oPingResult.ResponseTime
'Wscript.Echo "TTL (s) = " & vbTab & oPingResult.ResponseTimeToLive
Else
PingOK = FALSE
'WScript.Echo sTarget & " is not responding"
'WScript.Echo "Status code is " & oPingResult.StatusCode
End If
Next
End Function
Sub SaveToFile (sTexte, sFile)
REM ========================================
REM = Sauvegarde un texte dans un fichier
REM ========================================
Set f1 = oFSO.CreateTextFile(sFile)
f1.Write sTexte
f1.Close
Set f1 = Nothing
Set oFSO = Nothing
End Sub