Solved

VBS Script Problems

Posted on 2009-07-07
6
371 Views
Last Modified: 2012-05-07
The following script has been created by using scripts developed by other people and then modified for my needs.  I am a scripting novice and was wondering if any could help with what is causing my error.  When run on my Windows XP workstation all works fine and produces the required output file.  However I was developing the script to run against a mixture of Win2K and Win2003 servers when run on these servers the VBScript returns a 80041017 error.
' All setup work is being done starting here

	Const adVarChar = 200

	Const MaxCharacters = 255

	Dim datein

	Dim datecre

	Dim Datemod

	Dim dateacc

	Dim filesize

	Dim fsizematch
 

	dim EMailTo		' for recipient address

	dim objMessage ' for the e-mail 

	dim sMailText  ' for the e-mail text message
 

	' E-mail address to receive script progress notifications
 

	eMailTo = "xxx.yyy@abc.com"
 
 

	Set oFSO = CreateObject("Scripting.FileSystemObject")

	Set fOut = oFSO.CreateTextFile("c:\listerms.txt", True)

	Set objEmail = CreateObject("CDO.Message")

	strComputer = "."

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 

	'Send a starting e-mail

	sMailText = "STARTING Lister Scan"

	SendMail

	'End of starting Email 
 

	strComputer = "."

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

	strFolderName = "c:\hrcontrol\test"
 

' All setup work is finished by here
 

' All hard work done here
 

Set colSubfolders = objWMIService.ExecQuery _

    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _

        & "Where AssocClass = Win32_Subdirectory " _

            & "ResultRole = PartComponent")
 
 

arrFolderPath = Split(strFolderName, "\")

strNewPath = ""

For i = 1 to Ubound(arrFolderPath)

    strNewPath = strNewPath & "\\" & arrFolderPath(i)

Next

strPath = strNewPath & "\\"

 

Set colFiles = objWMIService.ExecQuery _

    ("Select * from CIM_DataFile where Path = '" & strPath & "'")
 

For Each objFile in colFiles

			datein = mid(objfile.InstallDate,5,2) & "/" & mid(objfile.InstallDate,7,2) & "/" & mid(objfile.InstallDate,1,4)

			datecre = mid(objfile.CreationDate,5,2) & "/" & mid(objfile.CreationDate,7,2) & "/" & mid(objfile.CreationDate,1,4)

			datemod = mid(objfile.LastModified,5,2) & "/" & mid(objfile.LastModified,7,2) & "/" & mid(objfile.LastModified,1,4)

			dateacc = mid(objfile.LastAccessed,5,2) & "/" & mid(objfile.LastAccessed,7,2) & "/" & mid(objfile.LastAccessed,1,4)
 

If objfile.filesize > 524288000 Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

Else

		If lcase(Right(objFile.Name,3)) = "exe" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "mp3" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "wma" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "com" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "zip" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "msi" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "scr" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "avi" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,4)) = "mpeg" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		End if

End if

Next
 

For Each objFolder in colSubfolders

    GetSubFolders strFolderName

Next
 

Sub GetSubFolders(strFolderName)

    Set colSubfolders2 = objWMIService.ExecQuery _

        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _

            & "Where AssocClass = Win32_Subdirectory " _

                & "ResultRole = PartComponent")
 

    For Each objFolder2 in colSubfolders2

        strFolderName = objFolder2.Name

'        Wscript.Echo

'        Wscript.Echo objFolder2.Name

        arrFolderPath = Split(strFolderName, "\")

        strNewPath = ""

        For i = 1 to Ubound(arrFolderPath)

            strNewPath = strNewPath & "\\" & arrFolderPath(i)

        Next

        strPath = strNewPath & "\\"

 

        Set colFiles = objWMIService.ExecQuery _

            ("Select * from CIM_DataFile where Path = '" & strPath & "'")
 

        For Each objFile in colFiles

			datein = mid(objfile.InstallDate,5,2) & "/" & mid(objfile.InstallDate,7,2) & "/" & mid(objfile.InstallDate,1,4)

			datecre = mid(objfile.CreationDate,5,2) & "/" & mid(objfile.CreationDate,7,2) & "/" & mid(objfile.CreationDate,1,4)

			datemod = mid(objfile.LastModified,5,2) & "/" & mid(objfile.LastModified,7,2) & "/" & mid(objfile.LastModified,1,4)

			dateacc = mid(objfile.LastAccessed,5,2) & "/" & mid(objfile.LastAccessed,7,2) & "/" & mid(objfile.LastAccessed,1,4)
 

'If count = 0 Then

'    MsgBox "There are no items."

'ElseIf count = 1 Then

'    MsgBox "There is 1 item."

'Else

'    MsgBox "There are " & count & " items."

'End If
 
 
 

If objfile.filesize > 524288000 Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

Else

		If lcase(Right(objFile.Name,3)) = "exe" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "mp3" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "wma" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "com" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "zip" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "msi" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "scr" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,3)) = "avi" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		ElseIf	lcase(Right(objFile.Name,4)) = "mpeg" Then

			fOut.WriteLine objFile.Name & "," & objfile.filesize & "," & datecre & "," & datein & "," & datemod & "," & dateacc & ","

		End If

End If
 

		Next

        GetSubFolders strFolderName

    Next

End Sub
 

Sub SendMail

	Set objMessage = CreateObject("CDO.Message") 

	objMessage.Subject = sMailText
 

	objMessage.From = "lister@abc.com" 

	objMessage.To = eMailTo 

	objMessage.Configuration.Fields.Item _

	("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

	objMessage.Configuration.Fields.Item _

	("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp"

	objMessage.Configuration.Fields.Item _

	("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

	objMessage.Configuration.Fields.Update
 

	objMessage.Send

	Set objMessage = Nothing

End Sub
 

' All finishing work is being done starting here
 
 
 

	'Send a finishing e-mail

	sMailText = "FINISHED Lister Scan"

	SendMail

	'End of starting Email

Open in new window

0
Comment
Question by:HoricePlant
  • 3
  • 2
6 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
What line number is it failing on?  The error should give this to you...
0
 

Author Comment

by:HoricePlant
Comment Utility
The error is as per the attached
eexch.doc
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
Hi, this would indicate to me that no files have been returned from the search.  Above this line:
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from CIM_DataFile where Path = '" & strPath & "'")

if you put this

MsgBox "Getting file collection from " & strPath

You will then see the folder path that the script will read.  Verify that that path exists on your target system.

Regards,

Rob.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:HoricePlant
Comment Utility
RobSampson thanks for the comment and yes it appears partially to be correct that nothing is being returned.  

However it would appear that the issue is more based on the fact that some of the returned data contains special characters like apostrophes.  This therefore causes issues with the data retruned being incomplete.  Does anyone have any idea how to get round this.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hmmm, but you've posted that the error is on this line
        For Each objFile in colFiles

Which suggests that no files are returned by the query
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from CIM_DataFile where Path = '" & strPath & "'")

If that *is* because the folder path contains apostrophes, then you can try the following:
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from CIM_DataFile where Path = '" & Replace(strPath, "'", "\'") & "'")

which replaces any apostrophes in strPath with \' to try to escape that character.

Regards,

Rob.
0
 

Author Closing Comment

by:HoricePlant
Comment Utility
I had further work to do to complete the answer.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now