Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Logon Script Problems with loops

Posted on 2007-03-21
10
Medium Priority
?
251 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 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 

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

Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

715 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