Announcement

Collapse
No announcement yet.

Help with a export script "MODIFY.VBS" - Newbie

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with a export script "MODIFY.VBS" - Newbie

    I have no VB scripting experience, but I needed to create an export of my Exchange Distribution groups, I came across this KB on Microsoft and they provide the steps and script to export all DL to text files. The issue is that not all the groups are exporting. It is weird, but on certain groups I get an error "Script: Modify.vbs, Line: 19, Char: 9, Error: Invalid procedure call or argument: 'mid', Code: 800A0005, Source: Microsfot VBScript runtime error".

    Code:
    Code:
    Dim objFileSystem, objInputFile
    Dim strOutputFile, inputData, strData, strTemp
     
    Const OPEN_FILE_FOR_READING = 1
     
    ' generate a filename base on the script name
    strOutputFile = "groups.ldf"
    Set objFileSystem = CreateObject("Scripting.fileSystemObject")
    Set objInputFile = objFileSystem.OpenTextFile(strOutputFile, OPEN_FILE_FOR_READING)
     
    ' read everything in an array
    inputData = Split(objInputFile.ReadAll, vbNewline)
    For each strData In inputData
        if Mid(strData, 1,2)="me" and len(strdata)>10  then
            strTemp=Mid(strData, 12, InStr(1, strData,",",1)-12)
            WScript.Echo strTemp
        end if   
        if Mid(strData,1,2)=" C" then
            strTemp=Mid(strData, 5, InStr(1, strData,",",1)-5)
            WScript.Echo strTemp   
        end if
    Next
    objInputFile.Close
    Set objFileSystem = Nothing
    WScript.Quit(0)
    I cannot see any reason why some are able to be exported to a text file fine and others cannot.

    Any help would be greatly appreciated. If more information is needed, I'm happy to provide what ever additional information is needed.

    Thanks,

  • #2
    Re: Help with a export script "MODIFY.VBS" - Newbie

    The mid() function returns a substring. If arguments are out of range, you could get such an error. Substring's start nor range can be out of the string, so be sure to check range before the call.

    In addition, VBScript's boolean statements do not short-circuit. In most programming languages, it is fine to do like this:

    Code:
    if getStringLenghth(somestring) >= 10 and substring(somestring, 5, 2) == "of"
    This usually works, as if the length of somestring is less than 10 chars, the right hand side of and is not evaluated. Anding false and whatever is always false, so what's the use of checking the second term anyway? There is none, so the whole statement short-circuits. Which improves performance and makes programmer's life usually easier. There is a Wikipedia article about short circuit evaluation.

    This is not the case with VBScript. It always evaluates the whole if statement. So,

    Code:
    if len(somestring) >= 10 and mid(somestring, 5, 2) = "of" then
    will fail in error if somestring ever happens to be less than 10 chars.


    To see what data causes crash in your script, insert some debug statements. By adding a wscript.echo, you can print every strData onto the console to check if there is anything funny going on in the loop.

    Code:
    For each strData In inputData
      wscript.echo "strData :" & strData & ":"
      ' ... the rest of the foreach loop
    Next
    -vP

    Comment


    • #3
      Re: Help with a export script "MODIFY.VBS" - Newbie

      Thanks for the reply. As I mentioned, I have no VB scripting experience, I got the script from MicroSoft website to help me with a task. I can see basically what the script is doing, it creates a file called group.ldf and then takes the CN(Common Name) from the file and puts it in a text file. What I noticed is that when ever the DC(Domain Component) is on seperate lines, there is an error and the rest of the file is ignored.

      Example:

      dn: CN=Orders Sea,OU=EmailDistribution,OU=Groups,OU=Company Domain,DC=CompanyDomain,DC=local
      changetype: add
      cn: Orders Sea
      member:
      CN=Tara Doover,OU=Seattle,OU=Users,OU=Domain Company,DC=CompanyDomain,DC=loc
      al
      member:
      CN=Jennifer Ellende,OU=Seattle,OU=Users,OU=Company Domain,DC=CompanyDomain,D
      C=local
      member:
      CN=Carl Anderson,OU=Seattle,OU=Users,OU=Company Domain,DC=CompanyDomain,DC=
      local
      groupType: 8

      In the above file, Carl Anderson is not added to the text file. I'm not sure why in the Jennifer entry, the DC are on separate lines. I have about 68 DL and I don't really feel like going through all files to make sure they are correct. Any other information needed, please let me know.

      As for adding the debugging info, not sure where I'm suppose to add it to my script.

      Thanks,

      Comment


      • #4
        Re: Help with a export script "MODIFY.VBS" - Newbie

        OK, so I was able to get some assistance to help me solve this issue. Replace the contents of the modify.vbs script with the following code(Thanks to Richard Mueller)
        Code:
        Option Explicit
        
        Dim strFile, objFSO, objFile, strLine, strPrevious, strMember
        Dim intComma, intCommaEscaped, intStart
        
        Const ForReading = 1
        
        ' Specify the input file.
        strFile = "Groups.ldf"
        
        ' Open the file for reading.
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.OpenTextFile(strFile, ForReading)
        
        Do Until objFile.AtEndOfStream
            strLine = objFile.ReadLine
            ' Check if line begins with a blank space, which
            ' indicates it is a continuation of the previous line.
            If (Left(strLine, 1) = " ") Then
                ' Concatenate this line (without the leading space)
                ' to the previous.
                strLine = strPrevious & Mid(strLine, 2)
            Else
                ' Previous line was not continued. Process this line.
                If (Left(strPrevious, 8) = "member: ") Then
                    ' Parse for Common Name of group member.
                    strMember = Mid(strPrevious, 12)
                    ' Look for first comma that is not escaped.
                    intStart = 1
                    Do
                        intComma = InStr(intStart, strMember, ",")
                        intCommaEscaped = InStr(intStart, strMember, "\,")
                        If (intCommaEscaped = 0) Or (intComma < intCommaEscaped) Then
                            If (intComma > 0) Then
                                strMember = Left(strMember, intComma - 1)
                            End If
                            Exit Do
                        Else
                            intStart = intComma + 1
                        End If
                    Loop
                    Wscript.Echo strMember
                End If
            End If
            strPrevious = strLine
        Loop
        
        ' Process the last line.
        If (Left(strPrevious, 8) = "member: ") Then
            ' Parse for Common Name of group member.
            strMember = Mid(strPrevious, 12)
            ' Look for first comma that is not escaped.
            intStart = 1
            Do
                intComma = InStr(intStart, strMember, ",")
                intCommaEscaped = InStr(intStart, strMember, "\,")
                If (intCommaEscaped = 0) Or (intComma < intCommaEscaped) Then
                    If (intComma > 0) Then
                        strMember = Left(strMember, intComma - 1)
                    End If
                    Exit Do
                Else
                    intStart = intComma + 1
                End If
            Loop
            Wscript.Echo strMember
        End If
        
        ' Clean up.
        objFile.Close

        Comment

        Working...
        X