VBscript: vbscript to enumerate the printers of Active Directory users and write the results to a file

Posted May 27th, 2011 in printers by dirk adamsky

Today’s script is made for Hassan.
He asked me to do a script that enumerates the printers of each user. Although that sounds easy it is a little complicated.

The printers of each user are saved in the user profile
(in the ntuser.dat file to be precisely).
The easiest way to get a list of printers per user is to add a small script to the logon script (can also be done with a group policy).
The script below is such a script.
It is very compact and can be run under the users credentials.
The script does a WMI query (the Win32_Printer Class) for the printers and writes the output to a file.
Unfortunately we cannot use the same file for all users because of potential “file locking” problems.
This can be adressed by using a database but that is beyond the scope of this article.

Follow the next steps to run the script (no admin rights needed):

* open your favorite text editor
* copy and paste the script into the editor
* change the UNC path (“\srvXXXlogfiles”) in the LogToFile function on line 19 to your UNC path
* save the script (for example c:tempprinters.vbs)
* open a command prompt
* go to “c:\temp”
* give “cscript printers.vbs” (without quotes) and enter

To run the script as a logon script you can copy the script to the netlogon share or another user accessible location.

The script:

' Name : printers.vbs
' Description : script to enumerate the printers of Active Directory users and write the results to a file
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 27-05-2011

Set objNetwork = CreateObject("Wscript.Network")
Set objWMIService = GetObject("winmgmts:\.rootcimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter in colPrinters
    LogToFile(objNetwork.UserDomain & "" & objNetwork.UserName & " ; " & objPrinter.Name)
Next
Set colPrinters = Nothing
Set objWMIService = Nothing
Set objNetwork = Nothing 

Function LogToFile(Message)
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = ObjFSO.OpenTextFile("\srvXXXlogfiles" & objNetwork.UserName & Date & ".txt",8,True)
objTextFile.WriteLine Message
objTextFile.Close
Set objTextFile = Nothing
Set ObjFSO = Nothing
End Function

When you have problems/questions with the script please post a reply.

Happy scripting.

Best regards,

Dirk Adamsky

VBscript and WMI: VBscript to find files with specific extensions and creation date on remote servers

Posted March 21st, 2011 in files by dirk adamsky

I recently got the question to find all Symantec BESR snaphot files older than 2 weeks (the retention period) on the company servers. This so that we could clear up some space on the snapshot volumes.
I decided to make a general purpose script of it.
You can use it to find old logfiles, etc.

What the script does:

  • create an array with all server you want to check (replace srv001, etc with your server names)
  • create a variable with a date of 2 weeks ago (Date -14)(can easily be changed in 30, 60 days ago)
  • change the variable date to the UTC date of 14 days ago (because WMI works with UTC dates)
  • for each server in the array
  • make a wmi connection
  • query for files with specific extensions and a creation date older than 14 days ago
  • in this example i took .txt and .csv extensions, change them in whatever filetype you want

The script is tested in an win2003 environment.

Follow the next steps to run the script (admin rights needed):

  • copy and paste the script in your favorite text editor
  • replace the strings ‘srv001′ and ‘srv002′ with the name of your exchange server
  • replace the extensions ‘txt’ and ‘csv with the extensions you need
  • save the script (for example c:tempfindoldfileswithspecificextensions.vbs)(or something shorter..)
  • open a command prompt
  • go to “c:temp”
  • give “cscript findoldfileswithspecificextensions.vbs” (without quotes) and enter

When you want the output in a file please give this command:

“cscript findoldfileswithspecificextensions.vbs > findoldfileswithspecificextensions.txt” (again without the quotes)

The script:

' Name : findoldfileswithspecificextensions.vbs
' Description : VBscript to find files with specific extensions and creation date on remote servers
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 21-03-2011
' Level: intermediate

arrServers = Array("srv001","srv002")
strDate = Date - 14
Set objDateToUtcDate = CreateObject("WbemScripting.SWbemDateTime")
objDateToUtcDate.SetVarDate(strDate)

For Each Server in arrServers
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & Server & "rootcimv2")
	Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where (Extension = 'txt' or Extension = 'csv') and CreationDate < '" & objDateToUtcDate & "'")
	For Each objFile in colFiles
		Wscript.Echo objFile.Name
	Next
	Set colFiles = Nothing
	Set objWMIService = Nothing
Next
Set objDateToUtcDate = Nothing

When you have problems/questions please post a reply or give a ‘star’ rating.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

VBscript and WMI: VBscript to enumerate all mailboxes on a given Exchange server

Posted February 23rd, 2011 in exchange by dirk adamsky

This script is made for Jeff Doty.

What the script does:

  • make a wmi connection to a given exchange server and create a list of the mailboxes and their size

The script is tested in an win2003/exchange2003 environment.

Follow the next steps to run the script (admin rights needed):

  • copy and paste the script in your favorite text editor
  • replace the string ‘srv001′ with the name of your exchange server
  • save the script (for example c:tempmailboxes.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript mailboxes.vbs” (without quotes) and enter

When you want the output in a file please give this command:

“cscript mailboxes.vbs > mailboxes.txt” (again without the quotes)

The script:

' Name : mailboxes.vbs
' Description : script to enumerate all mailboxes on a given Exchange server
' Author : dirk adamsky - deludi bv
' Version : 1.10 (changed/corrected based on input by Mike)
' Date : 23-03-2011
' Level: intermediate

strServer = "srv001"
Const MinimalSize = 2048 'size in MB
Set objWMIExchange = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strServer & "/root/MicrosoftExchangeV2")
Set colExchangeMailboxes = objWMIExchange.InstancesOf("Exchange_Mailbox")
For Each objExchangeMailbox in colExchangeMailboxes
    If (Left(objExchangeMailbox.StorageGroupName, 5) <> "Recov") And (Round(objExchangeMailbox.Size/1024,0) > MinimalSize) Then
		Wscript.Echo objExchangeMailbox.MailboxDisplayName & " ; " &_
			Round(objExchangeMailbox.Size/1024,0) & " MB"
	End If
Next
Set colExchange_Mailboxes = Nothing
Set objWMIExchange = Nothing

When you have problems/questions please post a reply or give a ‘star’ rating.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

Active Directory and WMI: VBscript to enumerate the ntfs rights of a given UNC path and a given level of subfolders

Posted October 22nd, 2010 in ntfsrights by dirk adamsky

Today I have extended the previous script:
it now also enumerates the NTFS rights of subfolders below the share.
As a bonus the level of subfolders can be set

Follow the next steps to run the script (admin rights needed for the WMI connection):

  • copy and paste the script in your favorite text editor
  • save the script (for example c:\temp\uncacl2.vbs)
  • open a command prompt
  • go to “c:\temp”
  • give “cscript uncacl2.vbs” (without quotes) and enter

The script:

' Name : uncacl2.vbs
' Description : script to enumerate the ntfs rights of a given UNC path and a given level of subfolders
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 22-10-2010

strUNCPathName = InputBox("please supply the UNC path to the shared folder")
strSubfolderLevel = InputBox("please supply the subfolder depth (1,2,etc.)")
arrUNC = split(strUNCPathName,"\")
If Ubound(arrUNC) > 3 Then
strRightPartOfPath = Mid(strUNCPathName,(Instr(strUNCPathName,arrUNC(4)) -1))
End If
Set objWMI = GetObject("winmgmts:\\" & arrUNC(2) & "\root\CIMV2")
Set objFileShare = objWMI.Get("Win32_Share.Name=""" & arrUNC(3) & """")
If Right(arrUNC(3),1) = "$" And Len(arrUNC(3)) = 2 Then
strPath = objFileShare.Path & Mid(strRightPartOfPath,2)
Else
strPath = objFileShare.Path & strRightPartOfPath
End If
ShowACL strPath
ViewSubFolders strPath, strSubfolderLevel
Set objFileShare = Nothing
Set objWMI = Nothing

Function ViewSubfolders(strFolder, strMaxlevel)
Set colSubfolders = objWMI.ExecQuery("Associators Of {Win32_Directory.Name='" & strFolder & "'} " &_
"Where AssocClass = Win32_Subdirectory ResultRole = PartComponent")
If strMaxlevel >= 1 Then
    For Each SubFolder in colSubfolders
        wscript.echo SubFolder.Name
        ShowACL SubFolder.Name
        ViewSubFolders SubFolder.Name, (strMaxlevel - 1)
    Next
End If
Set colSubfolders = Nothing
End Function

Function ShowACL(strDir)
Set objFolderSecuritySettings = objWMI.Get("Win32_LogicalFileSecuritySetting.Path='" & strDir & "'")
objFolderSecuritySettings.GetSecurityDescriptor objSD
For Each objAce in objSD.DACL
    Select Case objAce.AccessMask
        Case 1179817
            strRights = "read-only"
        Case 2032127
            strRights = "full-control"
        Case 1245631
            strRights = "change"
    End Select
Wscript.Echo strUNCPathName & " ; " & strDir & " ; " & objAce.Trustee.Domain & " ; " & objAce.Trustee.Name & " ; " & strRights
Next
Set objSD = Nothing
Set objFolderSecuritySettings = Nothing
End Function

When you have problems/questions please post a reply or give a ‘star’ rating.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV