Sponsored Links

Managing a network is not always easy. Sometimes you need to know who has what printers.
Here is a script to enumerate a users printers on a network.

1. Add this script to a users login script or via Group Ploicy.
2. The script will create a text file with the users name.
3. It will write the users name and computer name to the file.
4. It will then enumerate the users printers and write them to a text file.

 

----------COPY EVERYTHING BELOW THIS LINE----------


Const HKCR   = &H80000000
Const HKCU   = &H80000001
Const HKLM   = &H80000002
Const HKU    = &H80000003
Const HKCC   = &H80000005
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
Const OPEN_FILE_FOR_APPENDING = 8

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
 For Each objComputer in colComputer
 
 user = objComputer.UserName
 MyString = Replace(user, "MYDOMAIN\", "") 'PLACE DOMAIN HERE with \
 

Next


On Error Resume Next

strWritePath = "\\SERVERNAME\files\Printers\" & MyString & ".txt" 'PLACE UNC PATH TO FILES HERE
'THIS IS WHERE THE SCRIPT WILL CREATE THE TEXT FILES
strPath = "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"
strValueName = "ComputerName"


Dim fso, textFile, objFSsystemObject
Dim Text, Title, i, userName
Dim WshNetwork, oDevices


ParentFolder = "\\SERVERNAME\files\" 'PLACE PARENT PATH HERE
strDirectory = "\\SERVERNAME\files\Printers\" 'PLACE FULL PATH HERE
strFile = MyString &".txt"        

' Set File objects...
Set objFSsystemObject = CreateObject("Scripting.FileSystemObject")

'@@@@@@@@@@@@
If objFSsystemObject.FolderExists("\\SERVERNAME\files\Printers" ) Then
 Set objFolder = objFSsystemObject.GetFolder("\\SERVERNAME\files\Printers")
 
Else
 set objShell = CreateObject("Shell.Application")
 set objFolder = objShell.NameSpace(ParentFolder)
 objFolder.NewFolder "Printers"

End If


Set objFSO1 = CreateObject("Scripting.FileSystemObject")

If objFSO1.FileExists("\\SERVERNAME\files\Printers\" & strFile) Then
 Set objFolder = objFSO1.GetFile("\\SERVERNAME\files\Printers\" & strFile)

Else
 Set objFile = objFSO1.CreateTextFile(strDirectory & strFile)
 'Wscript.Echo "Just created " & objFolder & "\" & strFile
 objFile = ""

End If

'@@@@@@@@@@@@@


Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
Set fso = CreateObject("Scripting.FileSystemObject")
Set textFile = fso.OpenTextFile(strWritePath, OPEN_FILE_FOR_APPENDING)

'***********


Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
 For Each objComputer in colComputer
 'Wscript.Echo objComputer.UserName
 user = objComputer.UserName & vbCr
 textFile.WriteLine(user )

Next
textFile.Close

'***********


Set fso = CreateObject("Scripting.FileSystemObject")
Set textFile = fso.OpenTextFile(strWritePath, OPEN_FILE_FOR_APPENDING)
oReg.GetStringValue HKLM,strPath,strValueName,strValue
'Wscript.Echo "Value: " & strValue
Value = "Computer:" & strValue & vbCrLf
textFile.WriteLine(Value )

 


strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Devices"
 
oReg.EnumValues HKCU, strKeyPath, _
    arrValueNames, arrValueTypes
 
For i=0 To UBound(arrValueNames)
    'Wscript.Echo "Value Name: " & arrValueNames(i)
    Text = arrValueNames(i) & vbCr
    textFile.WriteLine(Text)
    
Next

textFile.Close
Text = ""


----------COPY EVERYTHING ABOVE THIS LINE----------


Just place this file into the logon script for the action to begin, or
make it apart of a group policy!

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR NOTEPAD!!!


*Make sure that all users have write access to the path you have chosen!


This information is provided "AS IS" with no warranties expressed or implied.