Solved

Logon Script Problems with loops

Posted on 2007-03-21
10
241 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 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now