Pages

Monday, February 13, 2012

List Printers Installed in the User Profile on a Remote Machine

I came across the vbscript below when I was working on an issue with a print server. Came in handy when it came time to reinstall missing printers on the server. The script works for Windows XP, I have not tested it with Windows Vista or 7.


'Script By Tommriddle 2010 - List All Network printers installed in the profile of a user on a remote machine.
forceUseCScript
Sub forceUseCScript()
 
Set oShell = CreateObject("Wscript.Shell")
   If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
      oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34),1,False
      WScript.Quit 0
   End If
End Sub
strComputer=inputbox("Enter PC Name")
CU=GetCurrentUser(strComputer)
CUSID = GetSIDFromUser(CU)
strKeyPath = CUSID & "\Printers\settings"
 
'Enumerate Registry Values
'http://www.activexperts.c...istry/#EnumRegVals.htm
Const HKEY_USERS = &H80000003
const REG_SZ = 1
const REG_EXPAND_SZ = 2
const REG_BINARY = 3
const REG_DWORD = 4
const REG_MULTI_SZ = 7
 
Set StdOut = WScript.StdOut
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
oReg.EnumValues HKEY_USERS, strKeyPath, arrValueNames, arrValueTypes
 
For i=0 To UBound(arrValueNames)
    StdOut.WriteLine "Printer: " & arrValueNames(i)
    StdOut.Writeline "User: " & CU
    StdOut.WriteBlankLines(1)
Next
 
'-----------------------------------------------------------------------
 
Function GetCurrentUser(strComputer)
'Input: strComputer = machine to query
'Output: Current User as domain\logon
'Only works on XP/W2003
   on error resume next
   Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'explorer.exe'")
   For Each objProcess in colProcessList
      objProcess.GetOwner strUserName, strUserDomain
   Next
   GetCurrentUser = strUserDomain & "\" & strUserName
   if err<> 0 then
      Msgbox " Error accessing remote machine"
      wscript.quit
   end if
   on error goto 0
End Function
 
'-----------------------------------------------------------------------
 
Function GetSIDFromUser(UserName)
'Input: UserName as domain\logon
'Output: SID
'http://groups.google.com/...t/msg/1bd0d208ef41dda7
   Dim DomainName, Result, WMIUser
   If InStr(UserName, "\") > 0 Then
      DomainName = Mid(UserName, 1, InStr(UserName, "\") - 1)
      UserName = Mid(UserName, InStr(UserName, "\") + 1)
   Else
      DomainName = CreateObject("WScript.Network").UserDomain
   End If
   On Error Resume Next
   Set WMIUser = GetObject("winmgmts:{impersonationlevel=impersonate}!" _
      & "/root/cimv2:Win32_UserAccount.Domain='" & DomainName & "'" _
         & ",Name='" & UserName & "'")
   If Err = 0 Then Result = WMIUser.SID Else Result = ""
   On Error GoTo 0
   GetSIDFromUser = Result
End Function
 
'-----------------------------------------------------------------------

1 comment:

  1. Hi Micheal,

    Such a great effort for a good printer script, however its doesn’t work on win7 , I hope if you can still do some changes in order to have the same outcome on win 7

    Rgrads, Ahmed

    ReplyDelete