Solved

Logon Script Problems with loops

Posted on 2007-03-21
10
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 500 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 500 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

749 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