Logon Script Problems with loops

Login Script small problems.
1. Keeps reseting rights on User Folder every time script is ran. (need only done when folder is first created)

Also anything to clean this script up?

' Drive Mapping
' 3/9/07 Joshw
' -----------------------------------------------------------------'
Option Explicit
Dim CheckDrive, AlreadyConnected, intDrive, strDrive, strDriveLetter
Dim strHomeFolder, strHome, strUser, strDriveLetter1
Dim objFSO, objShell, intRunError, objNetwork
Dim strDrive2, strDriveLetter2, strDrive3, strDriveLetter3

' Create a shell for cmd and CACLS
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' Variables
strHome = "\\tmi-dc1\userdata\"
strUser = objNetwork.UserName
strDriveLetter1 = "w:"
strDrive = "\\tmi-dc1\Departments"
strDriveLetter = "i:"
strDrive2 = "\\tmi-dc1\Public"
strDriveLetter2 = "y:"
strDrive3 = "\\192.168.30.20\Public"
strDriveLetter3 = "z:"

' Personal Drive Mapping
strHomeFolder = strHome & strUser
If strHomeFolder <> "" Then
If Not objFSO.FolderExists(strHomeFolder) Then
On Error Resume Next
objFSO.CreateFolder strHomeFolder
     If Err.Number <> 0 Then
     On Error GoTo 0
     Wscript.Echo "Cannot create: " & strHomeFolder
     End If
On Error GoTo 0
End If
     If objFSO.FolderExists(strHomeFolder) Then
     ' Assign user permission to home folder.
     intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls "_
     & strHomeFolder & " /t /c /g Administrators:f "_
     & strUser & ":F", 2, True)
        If intRunError <> 0 Then
        Wscript.Echo "Error assigning permissions for user " _
        & strUser & " to home folder " & strHomeFolder
        End If
    End If
End If

On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter1 _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter1
objNetwork.MapNetworkDrive strDriveLetter1, strHomeFolder

Else
objNetwork.MapNetworkDrive strDriveLetter1, strHomeFolder
End if

' Department Drive
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strDrive

Else
objNetwork.MapNetworkDrive strDriveLetter, strDrive
End if

' Public Drive 1
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter2 _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter2
objNetwork.MapNetworkDrive strDriveLetter2, strDrive2

Else
objNetwork.MapNetworkDrive strDriveLetter2, strDrive2
End if

' Public Drive 2
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter3 _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter3
objNetwork.MapNetworkDrive strDriveLetter3, strDrive3

Else
objNetwork.MapNetworkDrive strDriveLetter3, strDrive3
End if

WScript.Quit
kwitcomAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Your best option...add the /e (edit parameter)

  intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls "_
     & strHomeFolder & " /e /t /c /g Administrators:f "_

Otherwise, you'll have to use the objshell.exec method to retrieve the output of
cacls strHomeFolder and enumerate the results to determine what it's currently set to...
0
kwitcomAuthor Commented:
I am getting the following error in a Popup:
Error assigning permissions for user jtaylor

So it is erroring out.
0
sirbountyCommented:
Is that because the script is running under credentials that don't have access to modify the permissions?

What's the actual error message? This is your version of it...change the code above to

If intRunError <> 0 Then
        wscript.echo Err.Description
'        Wscript.Echo "Error assigning permissions for user " _
 '       & strUser & " to home folder " & strHomeFolder
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

kwitcomAuthor Commented:
Ok, now it is not looping but gives me a blank message box to click ok on.  And will finish adding the other drives.  What if we remove the message box and just make it keep going?
0
sirbountyCommented:
Your use of the /c states that it'll skip any access denied errors.
Not sure why err.description is blank, unless there's no error being returned (intRunError would hold the error number, if there was one).

You can leave that in, assuming that it's skipping errors...if you're okay with that.
Realize though that some processing may not be occurring and you'll not get notification of that fact...
0
kwitcomAuthor Commented:
Well see it was to create the folder if it was not there then set the rights, and once the folder is there I don't want it to re-run the rights setup.  That was the problem, it was trying to change the rights on all the files.
0
sirbountyCommented:
I don't think I understand...
why not use objFSO.FolderExists(folderName) to see if the folder is there?
0
kwitcomAuthor Commented:
Ok, for users that have not connected with the script it will look for folder and if not there it will create.

This is a script that I have not rolled out to my whole domain so some user's don't have folders on that server yet, plus when i add users I don't want to take more time to create folder and set rights on folders.
0
sirbountyCommented:
Appears to me that

If Not objFSO.FolderExists(strHomeFolder) Then

resolves your last concern...?  It shouldn't process if the folder is there...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kwitcomAuthor Commented:
This is the last script that works:

' Drive Mapping
' 3/9/07 Joshw
' -----------------------------------------------------------------'
Option Explicit
Dim CheckDrive, AlreadyConnected, intDrive, strDrive, strDriveLetter
Dim strHomeFolder, strHome, strUser, strDriveLetter1
Dim objFSO, objShell, intRunError, objNetwork
Dim strDrive2, strDriveLetter2, strDrive3, strDriveLetter3

' Create a shell for cmd and CACLS
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set CheckDrive = objNetwork.EnumNetworkDrives()

' Variables
strHome = "\\tmi-dc1\userdata\"
strUser = objNetwork.UserName
strDriveLetter1 = "w:"
strDrive = "\\tmi-dc1\Departments"
strDriveLetter = "i:"
strDrive2 = "\\tmi-dc1\Public"
strDriveLetter2 = "y:"
strDrive3 = "\\192.168.30.20\Public"
strDriveLetter3 = "z:"


' Personal Drive Mapping
strHomeFolder = strHome & strUser
If strHomeFolder <> "" Then
If Not objFSO.FolderExists(strHomeFolder) Then
On Error Resume Next
objFSO.CreateFolder strHomeFolder
     ' Assign user permission to home folder.
     intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls "_
     & strHomeFolder & " /e /t /c /g Administrators:f "_
     & strUser & ":F", 2, True)
        If intRunError <> 0 Then
     '   wscript.echo Err.Description
        Wscript.Echo "Error assigning permissions for user " _
        & strUser & " to home folder " & strHomeFolder
        End If
     If Err.Number <> 0 Then
     On Error GoTo 0
     Wscript.Echo "Cannot create: " & strHomeFolder
     End If
On Error GoTo 0
End If
     If objFSO.FolderExists(strHomeFolder) Then
    End If
End If

On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter1 _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter1
objNetwork.MapNetworkDrive strDriveLetter1, strHomeFolder

Else
objNetwork.MapNetworkDrive strDriveLetter1, strHomeFolder
End if

' Department Drive
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strDrive

Else
objNetwork.MapNetworkDrive strDriveLetter, strDrive
End if

' Public Drive 1
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter2 _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter2
objNetwork.MapNetworkDrive strDriveLetter2, strDrive2

Else
objNetwork.MapNetworkDrive strDriveLetter2, strDrive2
End if

' Public Drive 2
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) =strDriveLetter3 _
Then AlreadyConnected =True
Next

If AlreadyConnected = True then
objNetwork.RemoveNetworkDrive strDriveLetter3
objNetwork.MapNetworkDrive strDriveLetter3, strDrive3

Else
objNetwork.MapNetworkDrive strDriveLetter3, strDrive3
End if

WScript.Quit
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.