Wednesday, October 13, 2010

Enumerate Printer Shares for User on PC vbs

This source code does as the title suggests. It takes an input for PC and then it finds the user and then enumerates the printer share for that user.


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" 
  


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 
  
'----------------------------------------------------------------------- 

No comments:

Post a Comment