Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Log on scripts Windows 2003 Server

Posted on 2004-10-14
6
Medium Priority
?
273 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 71

Expert Comment

by:Chris Dent
ID: 12317079

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
ID: 12317327
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 71

Expert Comment

by:Chris Dent
ID: 12317583

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:nbcintern
ID: 12326599
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 71

Expert Comment

by:Chris Dent
ID: 12328210

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 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 12328232

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This may not be a text book method to resolve VSS backup issues but it seemed to have worked on few of the Windows 2003 servers we had issues while performing a Volume Shadow Copy backup. If you have issues while performing a shadow copy backup usin…
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

971 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