Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Lost in VBS can you help.?? (Ok now with correct code)

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,61711
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Lost in VBS can you help.?? (Ok now with correct code)
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Lost in VBS can you help.?? (Ok now with correct code) - 6/22/2008 8:36:07 PM   
  PK

 

Posts: 22
Score: 0
Joined: 10/10/2007
Status: offline
Hi all..

I can do basic VBS stuff But when things get a Little complicated i quickly find myself out of my depth..

To write my current project ive had a lot of help, but it fails since i made an additional function which was basically to perform the same "Overall" function as the Create Home Folder below.
There are so many IFs and END IFs that im totally confused..!

There is an error in my script that is "Loop Without Do" on line 316,
I can post the whole code if nessasary..

Any help really appreaciated..

Paul

'231---------------------------------------------------------------

          ' Create Profile Folder.
               If (strProfileFolder <> "") Then
                   If (objFSO.FolderExists(strProfileFolder) = False) Then
                       On Error Resume Next
                       objFSO.CreateFolder strProfileFolder
                       If (Err.Number <> 0) Then
                           On Error GoTo 0
                           Wscript.Echo "Unable to create Profile folder: " & strProfileFolder
                       End If
                       On Error GoTo 0
                   End If
                   If (objFSO.FolderExists(strProfileFolder) = True) Then
                       ' Assign user permission to home folder.
                       intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
                           & strProfileFolder & " /T /E /C /G " & strNetBIOSDomain _
                           & "\" & strNTName & ":F", 2, True)
                       If (intRunError <> 0) Then
                           Wscript.Echo "Error assigning permissions for user " _
                               & strNTName & " to Profile folder " & strProfileFolder
                       'End If
                   'End If
               'End If
             'End If

'260--------------------------------------------------------------------
' Group DN's start in column 12.
               intCol = 8
               Do While objSheet.Cells(intRow, intCol).Value <> ""
                   strGroupDN = Trim(objSheet.Cells(intRow, intCol).Value)
                   ' Attempt to bind to group object DN.
                   blnBound = False
                   On Error Resume Next
                   Set objGroup = GetObject("LDAP://" & strGroupDN)
                   If (Err.Number <> 0) Then
                       On Error GoTo 0
                       '365 Try  again converting NT Name to DN.
                       On Error Resume Next
                       objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain _
                           & "\" & strGroupDN
                       If (Err.Number <> 0) Then
                           On Error GoTo 0
                           Wscript.Echo "Unable to bind to group " & strGroupDN
                       Else
                           On Error GoTo 0
                           strGroupDN = objTrans.Get(ADS_NAME_TYPE_1779)
                           Set objGroup = GetObject("LDAP://" & strGroupDN)
                           blnBound = True
                       End If
                   Else
                       On Error GoTo 0
                       blnBound = True
                   End If
                   If (blnBound = True) Then
                       objGroup.Add objUser.AdsPath
                       If (Err.Number <> 0) Then
                           On Error GoTo 0
                           Wscript.Echo "Unable to add user " & strNTName _
                               & " to group " & strGroupDN
                       End If
                   End If
                   On Error GoTo 0
                   ' Increment to next group DN.
                   intCol = intCol + 1

               Loop
           End If
       End If
   End If

'308---------------------------------------------

    ' Increment to next user.
   intRow = intRow + 1

    ' ... In the loop.
    ' Add new user to the group.
objGroup.Add oNewUser.AdsPath
Loop

Wscript.Echo "Done"

' Clean up.

objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Set objUser = Nothing
Set objGroup = Nothing
Set objContainer = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
Set objFSO = Nothing
Set objShell = Nothing

< Message edited by PK -- 6/22/2008 9:55:46 PM >
 
 
Post #: 1
 
 RE: Lost in VBS can you help.?? (Ok now with correct code) - 6/22/2008 10:12:13 PM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Whenever I have something like this happen, and it does happen, I will basically go line by line looking for the opening and closing.  I.e. Find the Do and the corresponding Loop.  If/Then etc.  From looking at the code, it appears you have 2 loops but only 1 do.

               Loop
           End If
       End If
   End If

'308---------------------------------------------

    ' Increment to next user.
   intRow = intRow + 1

    ' ... In the loop.
    ' Add new user to the group.
objGroup.Add oNewUser.AdsPath
Loop

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to PK)
 
 
Post #: 2
 
 RE: Lost in VBS can you help.?? (Ok now with correct code) - 6/23/2008 1:42:52 AM   
  PK

 

Posts: 22
Score: 0
Joined: 10/10/2007
Status: offline
Many Thanks for that..

Found it it wasnt actually the loop statements after all..

I was missing an EndIF..

So thats cleared that bug seems i have one left which is a hardcoded add to group function. (highlighted in bold nr the top + bottom of the script)
This usto work ok but i cant make it work anymore...!

I get an "Object Required" error on line 311 (oNewUser.SetInfo)

Cheers
Paul

Option Explicit
Dim objExcel, strExcelPath, objSheet
Dim strLast, strFirst, strMiddle, strPW, intRow, intCol, strdispNM, strDesc, strSAMlogon, sDomain, oNewUser, orecordset, strProfileFolder
Dim strGroupDN, objUser, objGroup, objContainer, objAddGroup
Dim strCN, strNTName, strContainerDN
Dim strHomeFolder, strHomeDrive, objFSO, objShell
Dim intRunError, strNetBIOSDomain, strDNSDomain
Dim objRootDSE, objTrans, strLogonScript, strUPN
Dim strPreviousDN, blnBound
' Modify this to match your company's AD domain
sDomain="lcbt.co.uk"
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
' Specify spreadsheet.
strExcelPath = "c:\test\users.xls"

'Bind to the default group, using Distinguished Name.
' You only need to this once, before your loop.
Set objAddGroup = GetObject("
LDAP://cn=Students,ou=StudentsGP,dc=My-Domain,dc=co,dc=uk")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
' Use the NameTranslate object to find the NetBIOS domain name
' from the DNS domain name.
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
strNetBIOSdomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)
' Open spreadsheet.
Set objExcel = CreateObject("Excel.Application")
On Error Resume Next
objExcel.Workbooks.Open strExcelPath
If (Err.Number <> 0) Then
   On Error GoTo 0
   Wscript.Echo "Unable to open spreadsheet " & strExcelPath
   Wscript.Quit
End If
On Error GoTo 0
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
'-----------------------------------------------------------------------------------------------
' Start with row 2 of spreadsheet.
' Assume first row has column headings.
intRow = 2
' Read each row of spreadsheet until a blank value
' encountered in column 6 (the column for cn).
' For each row, create user and set attribute values.
strPreviousDN = ""
Do While objSheet.Cells(intRow, 6).Value <> ""
   ' Read values from spreadsheet for this user.
   strContainerDN = Trim(objSheet.Cells(intRow, 1).Value)
   strFirst = Trim(objSheet.Cells(intRow, 3).Value)
   strLast = Trim(objSheet.Cells(intRow, 4).Value)
   strCN = strFirst & " " & strLast
   strdispNM = strFirst & " " & strLast
   strPW = Trim(objSheet.Cells(intRow, 4).Value)
   strDesc = Trim(objSheet.Cells(intRow, 6).Value)
   strNTName = strFirst & "." & strLast
   strUPN = strFirst & "." & strLast & "@" & sDomain
   strProfileFolder = "\\Saturn\StudentProfiles$\" & strFirst & "." & strLast
   strHomeDrive = "S:"
   strHomeFolder = "\\Saturn\StudentArea\" & strFirst & "." & strLast
   strLogonScript = "Students.bat"
   '100 If this container is different from the previous, bind to
   ' the container the user object will be created in.
   If (strContainerDN <> strPreviousDN) Then
       'On Error Resume Next
       Set objContainer = GetObject("LDAP://" & strContainerDN)
       If (Err.Number <> 0) Then
           On Error GoTo 0
           Wscript.Echo "Unable to bind to container: " & strContainerDN
           Wscript.Echo "Unable to create user with NT name: " & strNTName
           '109 Flag that container not bound.
           strPreviousDN = ""
       Else
           On Error GoTo 0
           strPreviousDN = strContainerDN
       End If
   End If
   '117 Proceed if parent container bound.
   If (strPreviousDN <> "") Then
       ' Create user object.
       On Error Resume Next
       Set objUser = objContainer.Create("user", "cn=" & strCN)
       If (Err.Number <> 0) Then
           on Error GoTo 0
           Wscript.Echo "Unable to create user with cn: " & strCN
       Else
           On Error GoTo 0
           ' Assign mandatory attributes and save user object.
           If (strNTName = "") Then
               strNTName = strCN
           End If
           objUser.sAMAccountName = strNTName
           On Error Resume Next
           objUser.SetInfo
           If (Err.Number <> 0) Then
               On Error GoTo 0
               Wscript.Echo "Unable to create user with NT name: " & strNTName
           Else
               ' Set password for user.
               objUser.SetPassword strPW
               If (Err.Number <> 0) Then
                   On Error GoTo 0
                   Wscript.Echo "Unable to set password for user " & strNTName
               End If
               On Error GoTo 0
'151

               ' Enable the user account.
               objUser.AccountDisabled = False
               If (strFirst <> "") Then
                   objUser.givenName = strFirst
               End If
               ' Assign values to remaining attributes.
               'If (strMiddle <> "") Then
                   'objUser.initials = strMiddle
               'End If
               If (strLast <> "") Then
                   objUser.sn = strLast
               End If
               If (strdispNM <> "") Then
                   objUser.DisplayName = strdispNM
               End If
               If (strDesc <> "") Then
                   objUser.description = strDesc
               End If
               If (strUPN <> "") Then
                   objUser.userPrincipalName = strUPN
               End If
               If (strProfileFolder <> "") Then
                   objUser.profilePath = strProfileFolder
               End If
               If (strHomeDrive <> "") Then
                   objUser.homeDrive = strHomeDrive
               End If
               If (strHomeFolder <> "") Then
                   objUser.homeDirectory = strHomeFolder
               End If
               If (strLogonScript <> "") Then
                   objUser.scriptPath = strLogonScript
               End If
               ' Set password expired. Must be changed on next logon.
               objUser.pwdLastSet = 0
        '200 Save changes.
               On Error Resume Next
               objUser.SetInfo
            
               If (Err.Number <> 0) Then
                   On Error GoTo 0
                   Wscript.Echo "Unable to set attributes for user with NT name: " _
                       & strNTName
               End If
               On Error GoTo 0

               ' Create home folder.
               If (strHomeFolder <> "") Then
                   If (objFSO.FolderExists(strHomeFolder) = False) Then
                       On Error Resume Next
                       objFSO.CreateFolder strHomeFolder
                       If (Err.Number <> 0) Then
                           On Error GoTo 0
                           Wscript.Echo "Unable to create home folder: " & strHomeFolder
                       End If
                       On Error GoTo 0
                   End If
                   If (objFSO.FolderExists(strHomeFolder) = True) Then
                       ' Assign user permission to home folder.
                       intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
                           & strHomeFolder & " /T /E /C /G " & strNetBIOSDomain _
                           & "\" & strNTName & ":F", 2, True)
                       If (intRunError <> 0) Then
                           Wscript.Echo "Error assigning permissions for user " _
                               & strNTName & " to home folder " & strHomeFolder
                       End If
                   End If
               End If
'---------------------------------------------------------------
'237
          ' Create Profile Folder.
               If (strProfileFolder <> "") Then
                   If (objFSO.FolderExists(strProfileFolder) = False) Then
                       On Error Resume Next
                       objFSO.CreateFolder strProfileFolder
                       If (Err.Number <> 0) Then
                           On Error GoTo 0
                           Wscript.Echo "Unable to create Profile folder: " & strProfileFolder
                       End If
                       On Error GoTo 0
                   End If
                   If (objFSO.FolderExists(strProfileFolder) = True) Then
                       ' Assign user permission to home folder.
                       intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
                           & strProfileFolder & " /T /E /C /G " & strNetBIOSDomain _
                           & "\" & strNTName & ":F", 2, True)
                       If (intRunError <> 0) Then
                           Wscript.Echo "Error assigning permissions for user " _
                               & strNTName & " to Profile folder " & strProfileFolder
                     
                   End If
                End If
             End If
'--------------------------------------------------------------------
'266
' Group DN's start in column 12.
               intCol = 7
               Do While objSheet.Cells(intRow, intCol).Value <> ""
                   strGroupDN = Trim(objSheet.Cells(intRow, intCol).Value)
                   ' Attempt to bind to group object DN.
                   blnBound = False
                   On Error Resume Next
                   Set objGroup = GetObject("LDAP://" & strGroupDN)
                   If (Err.Number <> 0) Then
                       On Error GoTo 0
                       '365 Try  again converting NT Name to DN.
                       On Error Resume Next
                       objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain _
                           & "\" & strGroupDN
                       If (Err.Number <> 0) Then
                           On Error GoTo 0
                           Wscript.Echo "Unable to bind to group " & strGroupDN
                       Else
                           On Error GoTo 0
                           strGroupDN = objTrans.Get(ADS_NAME_TYPE_1779)
                           Set objGroup = GetObject("LDAP://" & strGroupDN)
                           blnBound = True
                       End If
                   Else
                       On Error GoTo 0
                       blnBound = True
                   End If
                   If (blnBound = True) Then
                       objGroup.Add objUser.AdsPath
                       If (Err.Number <> 0) Then
                           On Error GoTo 0
                           Wscript.Echo "Unable to add user " & strNTName _
                               & " to group " & strGroupDN
                       End If
                   End If
                   On Error GoTo 0
                   ' Increment to next group DN.
                   intCol = intCol + 1
               Loop
           End If
       End If
   End If
              oNewUser.SetInfo
            ' ... In the loop Add User to Group (Hardcoded). 
           'Add new user to the group.
                objgroup.Add oNewUser.AdsPath

'308---------------------------------------------
    ' Increment to next user.
   intRow = intRow + 1
Loop
Wscript.Echo "Done"
' Clean up.
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Set objUser = Nothing
Set objGroup = Nothing
Set objContainer = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
Set objFSO = Nothing
Set objShell = Nothing
Set objTrans = Nothing
Set objRootDSE = Nothing

(in reply to mbouchard)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Lost in VBS can you help.?? (Ok now with correct code) Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts