Solved

Log on scripts Windows 2003 Server

Posted on 2004-10-14
6
245 Views
Last Modified: 2010-04-19
Hello Experts,

We are running windows 2003 server and I want to create a default log in script the script that I have been working on is giving me trouble. The things that I want to do is when a user logs on I want some of the drives to be mapped to all the users and some drives to be mapped if they belong to a specific group. Before mapping the drive I want to check if that drive is already mapped if mapped I want to remove that mapping and create a new mapping. For example “Contact list” should be mapped to all the users in the network and IT drive should be mapped to only users who belong to IT group. Then it should map User drives some of our users have things saved in the server not all users have so those drives should be mapped according to the user name E.g. If user name is “jsmith” then map \\servername\User\jsmith. Then what I want to do is map printers this should be done according to the which group they belong E.g if user belongs to IT group then map \\servername\Printer1,

I appreciate your response.
Thank you
Dheenu
0
Comment
Question by:nbcintern
  • 4
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

What scripting language are you using?

Both VBScript and Kixtart Script will work, just depends on which you prefer.

%UserName% is a Windows Environmental Variable, that should work quite nicely for mapping the users folders.

Anyway, have a pseudo-code version

' Global Drive Mappings

Map P: \\Server\Contact List
Map H: \\Server\Share\%UserName%

' Group Based Drive Mappings

If User In Group IT
    Map R: \\Server\IT Share
    Install Printer to \\Share\IT Printer
End If Statement

0
 

Author Comment

by:nbcintern
Comment Utility
I am familiar with VB and Java script Either VB or Java script is fine with me. “pseudo-code” version that you have provide can that be used same as VB or java script what’s the extension that I should save this “pseudo-code” file,
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Pseudo-code is just fake code, a sort of design process so you know what you want to do before obscuring it with lots of real code.

If you prefer VB then the script can be written in VB... Hopefully the following will provide you with enough of a beginning to continue.

To map a drive is easy:

' Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")

' Global Drive Mappings

objNetwork.MapNetworkDrive "N:", "\\server\share"

But with that script we have no error control and it'll drop out of the script if the drive is already mapped.

So now a version with a bit of error control:

' Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Global Drive Mappings

strDrive = "N:"
strSharePath = "\\server\share"

bolDrive = objFileSystem.DriveExists(strDrive)

If (bolDrive = FALSE) Then
    objNetwork.MapNetworkDrive strDrive, strSharePath
Else
    objNetwork.RemoveNetworkDrive strDrive
    objNetwork.MapNetworkDrive strDrive, strSharePath
End If

For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

  Set objNetwork = CreateObject("WScript.Network")
  strDomain = objNetwork.UserDomain
  strUser = objNetwork.UserName
  bolIsMember = FALSE
  Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
  For Each objGroup In objUser.Groups
    If objGroup.Name = strGroupName Then
      bolIsMember = TRUE
      Exit For
    End If
  Next
  IsMember = bolIsMember
  Set objUser = nothing
  Set objNetwork = nothing

End Function

Finally... to map Printers you can use:

Dim objNetwork
Dim strNetworkPath

Set objNetwork = CreateObject("WScript.Network")

strNetworkPath = "\\Server\Printer"
objNetwork.AddWindowsPrinterConnection strNetworkPath

All that remains is to glue it all together.

Hope that lot helps.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:nbcintern
Comment Utility
Ok let see for example if I use the a script like the following one

‘Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Global Drive Mappings

strDrive = "T:"
strSharePath = "\\server01\Telephone List"

bolDrive = objFileSystem.DriveExists(strDrive)

If (bolDrive = FALSE) Then
    objNetwork.MapNetworkDrive strDrive, strSharePath
Else
    objNetwork.RemoveNetworkDrive strDrive
    objNetwork.MapNetworkDrive strDrive, strSharePath
End If

'For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

  Set objNetwork = CreateObject("WScript.Network")
  strDomain = objNetwork.UserDomain
  strUser = objNetwork.UserName
  bolIsMember = FALSE
  Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
  For Each objGroup In objUser.Groups
    If objGroup.Name = strGroupName Then
      bolIsMember = TRUE
      Exit For
    End If
  Next
  IsMember = bolIsMember
  Set objUser = nothing
  Set objNetwork = nothing

End Function


So how will make this map the users personal drive I think the above script is mapping all the common drives. How can I expand it to map specific user drives and I want to printers to be mapped according to user groups too for Example IT group should get some printers and some other group should get some other set of printers

Thank you
Dheenu
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

All the function does it return True or False depending on whether the user is in a Group or not. We can use that in an If Statement to perform a drive mapping for a particular group.

‘Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Map if user is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
End If

' Map a Printer if the User is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' Or all as one statement:

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

  Set objNetwork = CreateObject("WScript.Network")
  strDomain = objNetwork.UserDomain
  strUser = objNetwork.UserName
  bolIsMember = FALSE
  Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
  For Each objGroup In objUser.Groups
    If objGroup.Name = strGroupName Then
      bolIsMember = TRUE
      Exit For
    End If
  Next
  IsMember = bolIsMember
  Set objUser = nothing
  Set objNetwork = nothing

End Function
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 125 total points
Comment Utility

One edit for the above (typos...)...

‘Logon Script

Option Explicit

' Variable Declaration

Dim objNetwork, objFileSystem
Dim bolDrive
Dim strDrive, strSharePath, strNetworkPath

' Initialize Objects

Set objNetwork = CreateObject("WScript.Network")
Set objFileSystem = CreateObject("Scripting.FilesystemObject")

' Map if user is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
End If

' Map a Printer if the User is in a Group - Uses the Private Function IsMember

If (IsMember("Domain Admins") = TRUE) Then
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' Or all as one statement:

If (IsMember("Domain Admins") = TRUE) Then
    strDrive = "K:"
    strSharePath = "\\Server\Share"
    bolDrive = objFileSystem.DriveExists(strDrive)
    If (bolDrive = FALSE) Then
        objNetwork.MapNetworkDrive strDrive, strSharePath
    Else
        objNetwork.RemoveNetworkDrive strDrive
        objNetwork.MapNetworkDrive strDrive, strSharePath
    End If
    strNetworkPath = "\\Server\Printer"
    objNetwork.AddWindowsPrinterConnection strNetworkPath
End If

' Close everything down

Set objNetwork = nothing
Set objFileSystem = nothing

' For checking against Group Membership we can use this function:

Private Function IsMember(groupName)

    Dim strDomain, strUser, strGroupName
    Dim objUser, objGroup
    Dim bolIsMember

    strDomain = objNetwork.UserDomain
    strUser = objNetwork.UserName
    bolIsMember = FALSE
    Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
    For Each objGroup In objUser.Groups
        If objGroup.Name = strGroupName Then
            bolIsMember = TRUE
            Exit For
        End If
    Next
    IsMember = bolIsMember

    Set objUser = nothing

End Function
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Preface Having the need * to contact many different companies with different infrastructures * do remote maintenance in their network required us to implement a more flexible routing solution. As RAS, PPTP, L2TP and VPN Client connections are no…
I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

8 Experts available now in Live!

Get 1:1 Help Now