?
Solved

Logon Script Problems with loops

Posted on 2007-03-21
10
Medium Priority
?
261 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:kwitcom
  • 5
  • 5
10 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 2000 total points
ID: 18763475
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
 

Author Comment

by:kwitcom
ID: 18763698
I am getting the following error in a Popup:
Error assigning permissions for user jtaylor

So it is erroring out.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18763753
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Comment

by:kwitcom
ID: 18763870
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
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 2000 total points
ID: 18763892
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
 

Author Comment

by:kwitcom
ID: 18763930
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18763960
I don't think I understand...
why not use objFSO.FolderExists(folderName) to see if the folder is there?
0
 

Author Comment

by:kwitcom
ID: 18764045
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 18770663
Appears to me that

If Not objFSO.FolderExists(strHomeFolder) Then

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

Author Comment

by:kwitcom
ID: 18788816
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In an Exchange Crossforest migration, the distribution groups can be a very complex operation that would cause loss of time, lots of issues and continued headaches if not solved in a timely manner. I had to do a similar project so I created a sc…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

585 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question