• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Explain the script

can someone comments this script for me. line by line.?
I appreciate it.

On Error Resume Next

Dim GroupList
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")

GetGroupInfo()

LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)
'**************************************Group Mappings Based on Grouplist.csv*********************************
If fso.FileExists(logonpath&"\Grouplist.csv") Then
   Set grplist = Fso.OpenTextFile(logonpath&"\Grouplist.csv")
   ' make File into an Array
   aGroup = Split(grplist.Readall,vbcrlf)
   For I = 0 to UBound(GroupList) ' Check Every Group Membership the user is in (populated into Grouplist)
      grpname = Grouplist(i)
      For x = 0 to UBound(aGroup) ' Read the entire CSV to make sure all drives are mapped for each Group
         mapline = agroup(x)
         If InStr(LCase(mapline),LCase(grpname)) Then ' If you're in the group
            mapline = Mid(mapline,InStr(mapline,",")+1) ' Remove the GroupName from the line
            Drive = Left(mapline,InStr(mapline,",")-1) ' Extract Drive Letter
            Path = Mid(mapline,InStr(mapline,",")+1) ' Extract the path

            If (fso.DriveExists(drive) <> True) and (Drive<>"!!") Then ' If The Drive is not already mapped
               WshNetwork.MapNetworkDrive drive,path,true ' Map The Drive
               wscript.sleep 1000
            End If

        If Drive = "!!" then
               WSHNetwork.AddWindowsPrinterConnection Path
               wscript.sleep 1000
            end if

         End If
      Next
   Next
End If


Sub GetGroupInfo
Set UserObj = GetObject("WinNT://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
Set Groups = UserObj.groups

For Each Group In Groups
GroupCount = GroupCount + 1
Next

ReDim GroupList(GroupCount -1)
i = 0
For Each Group In Groups
GroupList(i) = Group.Name
i = i + 1
Next
End Sub
0
jskfan
Asked:
jskfan
1 Solution
 
RobSampsonCommented:
Hi, I hope this clarifies it enough for you:

'====================
' Turn error checking off. This means that the script will Not
' stop when it encounters an error. It will continue with the Next
' of code.  This can cause problems if you don't know why a script
' is not working.
On Error Resume Next

' Declare a variable for use later
Dim GroupList
' Create an object that allows us to work with the file system
Set fso = CreateObject("Scripting.FileSystemObject")
' Create an object that allows us to use Shell comamnds
Set WshShell = CreateObject("WScript.Shell")
' Create an object that allows us to use networking comamnds
Set WshNetwork = WScript.CreateObject("WScript.Network")

' Call a Sub Procedure called GetGroupInfo(), which is written below
' The procedure populates the GroupList array, which will hold the
' current group memberships of the user
GetGroupInfo()

' Retrieve the Parent folder name of the location where the script
' file is running from.
LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)

'**************************************Group Mappings Based on Grouplist.csv*********************************
' Check if the file "Grouplist.csv" exists in the parent folder
If fso.FileExists(logonpath&"\Grouplist.csv") Then
      ' If the file exists, open it. By using Set grplist, then grplist becomes a pointer
      ' to the open file, meaning we can read from it.
   Set grplist = Fso.OpenTextFile(logonpath&"\Grouplist.csv")
   ' make File into an Array, using the Split function. This makes each element
   ' of the array become one line from the file, because it has been split by vbCrLf (newline character).
   ' aGroup is the array of groups that a user *should* be a member of
   ' GroupList is the array of groups that a user is *currently* a member of
   aGroup = Split(grplist.Readall,vbcrlf)
   For I = 0 to UBound(GroupList) ' Check Every Group Membership the user is in (populated into Grouplist)
      grpname = Grouplist(i)
      For x = 0 to UBound(aGroup) ' Read the entire CSV to make sure all drives are mapped for each Group
         mapline = agroup(x)
         ' Check if the user is already a member of the groups from the CSV
         If InStr(LCase(mapline),LCase(grpname)) Then ' If you're in the group
            mapline = Mid(mapline,InStr(mapline,",")+1) ' Remove the GroupName from the line
            Drive = Left(mapline,InStr(mapline,",")-1) ' Extract Drive Letter
            Path = Mid(mapline,InStr(mapline,",")+1) ' Extract the path

            If (fso.DriveExists(drive) <> True) and (Drive<>"!!") Then ' If The Drive is not already mapped
               WshNetwork.MapNetworkDrive drive,path,true ' Map The Drive
               wscript.sleep 1000
            End If

            ' In the CSV file, the path that needs to be mapped can also be identified
            ' as !! for the drive letter, which means it's not a drive, it is a printer to be mapped
        If Drive = "!!" then
               WSHNetwork.AddWindowsPrinterConnection Path
               wscript.sleep 1000
            end if

         End If
      Next
   Next
End If


' This sub procedure populates the GroupList array, which will hold the
' current group memberships of the user
Sub GetGroupInfo
' Bind to the current user object on the domain
Set UserObj = GetObject("WinNT://" & wshNetwork.UserDomain & "/" & WshNetwork.UserName)
' Return a collection of the groups the user is a member of
Set Groups = UserObj.groups

' Go through each group
For Each Group In Groups
GroupCount = GroupCount + 1
Next

' Resize the array to fit each group name
ReDim GroupList(GroupCount -1)
i = 0
For Each Group In Groups
GroupList(i) = Group.Name
i = i + 1
Next
End Sub
'===================

Regards,

Rob.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now