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

Active Directory: VBscript to enumerate the roaming profile size of all users in Active Directory

Posted March 15th, 2010 in roamingprofile by dirk adamsky

For a sysadmin roaming profiles are both a blessing and a curse.
In time they tend to grow and grow…
The roaming profile size problem results in: long logon and logoff times, corrupted profiles, etc.
This script enumerates the roaming profile size of all users in your Active Directory domain.
By adding extra attributes to the arrAttributes array you can change the output.

Follow the next steps to run the script (admin rights needed for access to the roaming profiles directory):

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

The script:

' Name : roamingprofilesize.vbs
' Description : script to enumerate the roaming profile size of all users in Active Directory
' Author : dirk adamsky - deludi bv
' Version : 1.10 (changed script error based on input from Jim)
' Date : 06-05-2011
' Level : intermediate

arrAttributes = Array("profilePath","displayname","mail") 

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">"
Set objRootDSE = Nothing

strFilter = "(&(objectCategory=person)(objectClass=user)(profilePath=*))"
strAttributes = Join(arrAttributes,",")
Wscript.Echo Join(arrAttributes,";") & " ; roaming profile size in MB"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
	On Error Resume Next
	strTempOutput = ""
	For i = 0 To Ubound(arrAttributes)
		strTempOutput =  strTempOutput & " ; " & adoRecordset.Fields(arrAttributes(i)).Value
		strOutput = Mid(Ltrim(strTempOutput),3)
	Next
	Wscript.Echo strOutput & " ; " & Foldersize (adoRecordset.Fields(arrAttributes(0)).Value) & " MB"
	adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing

Function Foldersize(strPath)
	On Error Resume Next
	Set objFSO = CreateObject("scripting.filesystemobject")
	Set objFld = objFSO.GetFolder(strPath)
	Foldersize = Round(objFld.Size/1048576,2)
	Set objFld = Nothing
	Set objFSO = Nothing
End Function

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

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

Filesystem: VBscript to enumerate the subfolders (recursive) with depth limit

Posted March 5th, 2010 in folders by dirk adamsky

Here’s the second script of today.
It is a mutation of the previous script.
Added is a depth limit, this is done by a second argument of the subroutine.
The subroutine needs two arguments: the path of the folder and the depth level.
The path can be local (“c:temp”) or an UNC path like “\servertest”.

Follow the next steps to run the script (admin rights needed on the folder you want to enumerate):

  • open your favorite text editor
  • copy and paste the script into the editor
  • change the path of the folder to your folder path
  • change the depth value to your depth value (current value = 2)
  • save the script (for example c:tempenumeratesubfolderslevel.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript enumeratesubfolderslevel.vbs” (without quotes) and enter

The script:

' Name : enumeratesubfolderslevel.vbs
' Description : script to enumerate the subfolders (recursive) with depth limit
' Author : dirk adamsky - deludi bv
' Version : 1.01 corrected small counter error
' Date : 05-03-2010
' Level : Intermediate

ViewSubFolders "c:temp", 2
Sub ViewSubFolders(strFolder, strMaxLevel)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder(strFolder)
	If strMaxlevel >= 1 Then
		For Each SubFolder in objFolder.SubFolders
			Wscript.Echo SubFolder.Path
			ViewSubFolders SubFolder, (strMaxlevel - 1)
		Next
	End If
	Set objFolder = Nothing
	Set objFSO = Nothing
End Sub

When you have problems/questions please make a reply, when you like the script please rate it with the star rating below.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

[adrotate group="2" banner="3"]

Filesystem: VBscript to enumerate all subfolders (recursive) of a given folder

Posted March 5th, 2010 in folders by dirk adamsky

The motto of the day is: Recursion rules.
A couple of years ago I have done quite a few Filesystem Object scripts.
To refresh my mind I have made some new filesystem scripts.
This is the first one: a simple subroutine that uses recursion to enumrate all subfolders of a folder.
The subroutine needs one argument: the path of the folder.
The path can be local (“c:temp”) or an UNC path like “\servertest”.

Follow the next steps to run the script (admin rights needed on the folder you want to enumerate):

  • open your favorite text editor
  • copy and paste the script into the editor
  • change the path of the folder to your folder path
  • save the script (for example c:tempenumeratesubfolders.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript enumeratesubfolders.vbs” (without quotes) and enter

The script:

' Name : enumeratesubfolders.vbs
' Description : script to enumerate all subfolders (recursive) of a given folder
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 05-03-2010
' Level : Intermediate

ViewSubFolders "c:temp"
Sub ViewSubFolders(strFolder)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolder = objFSO.GetFolder(strFolder)
	For Each SubFolder in objFolder.SubFolders
		Wscript.Echo SubFolder.Path
		ViewSubFolders SubFolder
	Next
	Set objFolder = Nothing
	Set objFSO = Nothing
End Sub

When you have problems/questions please make a reply, when you like the script please rate it with the star rating below.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

[adrotate group="3"]