Link to home
Start Free TrialLog in
Avatar of lacroix_al
lacroix_al

asked on

Help with VBS script

I have this VBS script and can not figure out why it doesn't copy the data from the old folder to the new folder. can someone take a look and see what the issue is?
Sub onPostMove(Request)

If Request.class <> "user" Then Exit Sub
On Error Resume Next

Dim filesys, searchstr, newfolderpath, oldfolderpath, strname, DescName, oldscriptpath, olddescription, newcompany, newdescription


DirObj.Getinfo
objName=DirObj.Get("sAMAccountName")
cnpath=DirObj.Get("distinguishedName")
oldfolderpath=Dirobj.Get("division")
oldscriptpath=dirObj.Get("scriptpath")
olddescription=DirObj.Get("description")


DirObj.put "company", oldscriptpath
DirObj.put "department", olddescription

'DirObj.put "scriptpath", "RHS_Student.bat"
DirObj.put "scriptpath", "RHS_Stu_login.vbs"
DirObj.put "description", "RHS Student"
DirObj.setinfo



strName = objName

'Move from SPHS to RHS

searchstr = "RHS"

If Not InStr(cnpath, searchstr) = 0 Then

newfolderpath = "\\04infdata01\RHS_STU_HOME\" & strname

'DirObj.put "scriptpath", "RHS_Student.bat"
DirObj.put "scriptpath", "RHS_Stu_login.vbs"
DirObj.put "title", newfolderpath
DirObj.setinfo

'Add Users to Group
 
 
Set objUser = GetObject("LDAP://" & cnpath)
Set objGroup = GetObject("LDAP://CN=8e6Student,CN=Builtin,DC=sphs,DC=spuhsd,DC=k12,DC=ca,DC=us") 
objGroup.add(objUser.ADsPath)

Set objUser = GetObject("LDAP://" & cnpath)
Set objGroup = GetObject("LDAP://CN=RHSStudent,CN=Builtin,DC=sphs,DC=spuhsd,DC=k12,DC=ca,DC=us")
objGroup.add(objUser.ADsPath)


'Remove Users from Group

Set objUser = GetObject("LDAP://" & cnpath)
Set objGroup = GetObject("LDAP://CN=library,CN=Builtin,DC=sphs,DC=spuhsd,DC=k12,DC=ca,DC=us") 
objGroup.remove(objUser.ADsPath)

Set objUser = GetObject("LDAP://" & cnpath)
Set objGroup = GetObject("LDAP://CN=Students,CN=Builtin,DC=sphs,DC=spuhsd,DC=k12,DC=ca,DC=us")
objGroup.remove(objUser.ADsPath)



            
                
            Set filesys=CreateObject("Scripting.FileSystemObject") 
                              
                               ' creates folder if it doesn't exist 
                                If  Not filesys.FolderExists(newfolderpath) Then 
                                    Set newfolder = filesys.CreateFolder(newfolderpath)
                                End If
                                
                Set objShell = CreateObject("Wscript.Shell")
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                If objFSO.FolderExists(newfolderpath) Then
                
                ' Assign user permission to home folder.
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /t /c /g sphs.spuhsd.k12.ca.us\" & strname & ":C ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g sphs.spuhsd.k12.ca.us\administrator:F ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g sphs.spuhsd.k12.ca.us\Domain_admins:F ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g 04infdata01\administrator:F ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g sphs.spuhsd.k12.ca.us\rhsteacher:C ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g System:F ", 2, True)
                      
                      End If
                      
                      
                      
                      
                      
                 
                Set filesys=CreateObject("Scripting.FileSystemObject") 
                                If filesys.FolderExists(oldfolderpath) Then 
                                filesys.copyFolder oldfolderpath, newfolderpath
                                filesys.deleteFolder oldfolderpath
                                End If
      
      

End If

'Move from RHS To SPHS

searchstr = "SPHS"

If Not InStr(cnpath, searchstr) = 0 Then


DirObj.Getinfo
newfolderpath=Dirobj.Get("division")
oldfolderpath=DirObj.get("title")
newdescription=DirObj.get("department")
newscriptpath=DirObj.get("company")

DirObj.put "scriptpath", newscriptpath
DirObj.put "description", newdescription
DirObj.setinfo



'Remove Users to Group
 
 

Set objUser = GetObject("LDAP://" & cnpath)
Set objGroup = GetObject("LDAP://CN=RHSStudent,CN=Builtin,DC=sphs,DC=spuhsd,DC=k12,DC=ca,DC=us")
objGroup.remove(objUser.ADsPath)



'Add Users from Group

Set objUser = GetObject("LDAP://" & cnpath)
Set objGroup = GetObject("LDAP://CN=library,CN=Builtin,DC=sphs,DC=spuhsd,DC=k12,DC=ca,DC=us") 
objGroup.add(objUser.ADsPath)

Set objUser = GetObject("LDAP://" & cnpath)
Set objGroup = GetObject("LDAP://CN=Students,CN=Builtin,DC=sphs,DC=spuhsd,DC=k12,DC=ca,DC=us")
objGroup.add(objUser.ADsPath)



                    
            Set filesys=CreateObject("Scripting.FileSystemObject") 
                              
                               ' creates folder if it doesn't exist 
                                If  Not filesys.FolderExists(newfolderpath) Then 
                                    Set newfolder = filesys.CreateFolder(newfolderpath)
                                End If
                                
                Set objShell = CreateObject("Wscript.Shell")
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                If objFSO.FolderExists(newfolderpath) Then
                
                ' Assign user permission to home folder.
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /t /c /g sphs.spuhsd.k12.ca.us\" & strname & ":C ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g sphs.spuhsd.k12.ca.us\administrator:F ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g sphs.spuhsd.k12.ca.us\SPhsTeacher:C ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g 04infdata01\administrator:F ", 2, True)
                                intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & newfolderpath & " /e /c /g System:F ", 2, True)
                      
                      End If
                      
                      
                      
                      
                      
                 
                Set filesys=CreateObject("Scripting.FileSystemObject") 
                                If filesys.FolderExists(oldfolderpath) Then 
                                filesys.copyFolder oldfolderpath, newfolderpath
                                'filesys.deleteFolder oldfolderpath
                                End If
      
      

End If

Wscript.quit

    

End Sub

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

>If Request.class <> "user" Then Exit Sub

Are you sure the code is executing?
Hi, you have two of these sections:
		If filesys.FolderExists(oldfolderpath) Then 
			filesys.copyFolder oldfolderpath, newfolderpath
			filesys.deleteFolder oldfolderpath
		End If

Open in new window


Change those to this:
		Err.Clear
		If filesys.FolderExists(oldfolderpath) Then 
			filesys.copyFolder oldfolderpath, newfolderpath
			'filesys.deleteFolder oldfolderpath
			If Err.Number <> 0 Then
				WScript.Echo "Error copying " & oldfolderpath & " to " & newfolderpath & ". Error " & Err.Number & ": " & Err.Description
				Err.Clear
			Else
				WScript.Echo oldfolderpath & " was successfully copied to " & newfolderpath
			End If
		Else
			WScript.Echo "Old folder [" & oldfolderpath & "] was not found. Unable to copy data to " & newfolderpath
		End If

Open in new window


and you will see some output at that point to see why it might be failing.

Regards,

Rob.
And take out the WScript.Quit just before the End Sub.
Avatar of lacroix_al
lacroix_al

ASKER

Thank you for the replies

CodeCruiser:
This is running in the background of an active roles server. I know it runs because it does everything except copy the data from the old folder to the new folder.

RobSampson:
Yes, I have two setions with the same caode because the script is written the move data in two directions
'Move from SPHS to RHS
'Move from RHS to SPHS

why do I need to take out the WScript.Quit ?

I don't know if the error will report as the script is called by a program and not by a user in a window.
what are your thoughts?




I think I know what the problem is.
these are new accounts created in Active Directory. The do not have anything written in the oldfolderpath=Dirobj.Get("division"). when the script tries to copy the OldFolderPath to the NewFolderPath it fails because OldFolderPath is blank.

how do I write the script so if something is in the "division" placehold it doesn't get overwritten?
maybe something like this
if Dirobj.Get("division") <> " " then
Dirobj.put "division",
         For iYear = 2009 to 2015
If oFSO.FolderExists("\\04infdata01\sphs_stu_home\Grad_" & iYear & "\" & oNetwork.UserName)

I'm not sure if that makes since as I mixed to different scripts together, but you get the picture.
I have a folder for each grad year and need the script to figure out where the user is coming from (oldfolderpath) so it can set the variable before trying to copy it.

what are your thoughts on this?

Thank you for your help
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RobSampson:
Thank you for the reply.
I think this code will work for my needs.
if I want the script to set the variable if blank, do I just add DirObj.put "division", oldfolderpath

exp.
oldfolderpath = Trim(Dirobj.Get("division"))
If oldfolderpath = "" Then
      For iYear = 2015 To 2009 Step -1
            If oFSO.FolderExists("\\04infdata01\sphs_stu_home\Grad_" & iYear & "\" & strname) = True Then
                  oldfolderpath = "\\04infdata01\sphs_stu_home\Grad_" & iYear & "\" & strname
                         DirObj.put "division", oldfolderpath
                  Exit For
            End If
      Next
End If


As for the year of the user folder, it doesn't change. I setup the folders as the graduation year not the users grade. This way it won't change. the script you wrote will work just fine.

the WScript.Quit could be a mistake.
This is the entire script. I move users for one school to another and their server drive needs to move with them. the issue is when I move them the data doesn't copy because the variable is empty.
>> if I want the script to set the variable if blank, do I just add DirObj.put "division", oldfolderpath

Correct.  The way you have it should work fine.

Hopefully it works out for you.

Regards,

Rob.
Rob
one last thing
as I only want this part of the script to run when moving the account from SPHS to RHS, do I put this new code under this part odf the code?
'Move from SPHS to RHS

searchstr = "RHS"

If Not InStr(cnpath, searchstr) = 0 Then

<enter new script here>>?

Thank you for your help. the script is now working as it should
Hi, glad you got it sorted. Thanks for the grade.

Rob.