Solved

Log on scripts Windows 2003 Server

Posted on 2004-10-14
6
248 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
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 70

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 70

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 70

Accepted Solution

by:
Chris Dent earned 125 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Organizations create, modify, and maintain huge amounts of data to help their businesses earn money and generally function.  Typically every network user within an organization has a bit of disk space to store in process items and personal files.   …
Numerous times I have been asked this questions that what is it that makes my machine log on so slow, there have been cases where computers took 23 minute exactly after taking password and getting to the desktop. Interesting thing was the fact th…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

786 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