Hi,

I''d like to create a vbs script that runs when a user logs in.

Basically, the scripts should query the users group membership and then run
a batch file stored on a server share.

If the user is not a member of the group then the batch file should not be
run

Any help will be appreciated.

TIA

LM

Re: Help on script by Jon

Jon
Mon Sep 15 18:46:43 CDT 2003


"Lee Messenger" <lmessenger@westcon.co.uk> wrote in message
news:u1tQT$8eDHA.1648@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> I''d like to create a vbs script that runs when a user logs in.
>
> Basically, the scripts should query the users group membership and then run
> a batch file stored on a server share.
>
> If the user is not a member of the group then the batch file should not be
> run
>
> Any help will be appreciated.
>


Here is one way:


Set WSHShell = WScript.CreateObject("WScript.Shell")
If IsMember("administrators") Then WSHShell.Run "notepad.exe"

Function IsMember(strGroupName)
Set WSHNetwork = CreateObject("WScript.Network")
Set usr = GetObject("WinNT://./" & WSHNetwork.UserName & ",user")
For Each grp In usr.Groups
If UCase(grp.Name) = UCase(strGroupName) Then
IsMember = True : Exit For
End If
Next
End Function




Re: Help on script by Wayne

Wayne
Mon Sep 29 22:07:06 CDT 2003

Try this. I had problems myself if finding a code that would work. This is for a NT domain with clients running Windows NT4, 2000 and XP I gave you two of the scripts. One to the question you ask. The other script is my completed script that looks at computer names and add the right printer to it

'Script one

call main()

Public Sub Main()

'Main loop to detect group that user belongs to
adsPath = "WinNT://chgirls.school.nz/" & strUser
Set dso = GetObject("WinNT:")
Set objUser = dso.OpenDSObject(adsPath,"","", ADS_READONLY_SERVER)

For Each Prop In objUser.groups
Select Case Prop.Name

'NOTE: the group name is case Sensitive

Case "Girls"
Call Division1()

Case "STAFF"
Call Division2()

'etc NOTE: if a user is a member of many case then they will be called in turn

End Select
Next 'Prop

End Sub

Sub Division1()
'students group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "q:", "\\chgirls\student"
strHomeDir = "\\chgirls\" & strUser & "$"
oNet.MapNetworkDrive "w:", strHomeDir
End Sub

Sub Division2()
' staff group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "q:", "\\chgirls\student"
oNet.MapNetworkDrive "n:", "\\chgirls\staff_files"
strHomeDir = "\\chgirls\" & strUser & "$"
oNet.MapNetworkDrive "w:", strHomeDir
End Sub

' END OF SCRIPT
'-------------------------------------------------

And Here is number two
' Start of script two
On Error Resume Next
Dim WshNetwork, asdPath, User
Dim strMappedDrives, strStatus
Dim IE
dim oNet

Const ADS_READONLY_SERVER = 4
'----------------------------------------------------------------------------------
' Display IE status window

Call CreateIE()
strStatus = "CGHS WSH Logon Script v1.0 " & Date()
ie.document.all.wstatus.InnerText = strMsg3
'------------------------------------------------------------------------------------
'Get and Display Username

Set WSHNetwork = WScript.CreateObject("WScript.Network")
strUser = ""
While strUser = ""
strUser = WSHNetwork.UserName
Wend

ie.document.all.Msg1.InnerText = strUser


call main()
'--------------------------------------------------------------------------------------
'Get and Display Workstation Name

Set WSHNetwork = WScript.CreateObject("WScript.Network")
strComputername = ""
While strComputername = ""
strComputername = WSHNetwork.ComputerName
Wend


ie.document.all.Msg3.InnerText = strUser
'------------------------------------------------------------------------------------
'Now setup the printer for the room that they have log into


'First remove any printer that are part of the profile that should not be there

PrinterPath = "\\matrix\adminstaff"
WshNetwork.RemovePrinterConnection PrinterPath
'------------------------------------------------------------------------------------
'Staff Admin Staff Room
if Left(strComputerName, 4) = "admi" Then
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\matrix\adminstaff"
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Gym
if Left(strComputerName, 4) = "GYM-" Then
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\gym-02\printer"
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'T205
if Left(strComputerName, 4) = "T205" Then
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\T205-2000\T205"
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Suppor Rooms R2
if Left(strComputerName, 4) = "supp" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\support\printer"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Lyn's Computer
if Left(strComputerName, 2) = "rb" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\matrix\admin"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Maths Computer
if Left(strComputerName, 4) = "math" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\2math\maths"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Maths Computer number 2
if Left(strComputerName, 4) = "2mat" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
end if
'------------------------------------------------------------------------------------
'TG Computer
if Left(strComputerName, 4) = "tg-0" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\matrix\admin"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'EG's Computer
if Left(strComputerName, 2) = "eg" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\\matrix\admin"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------- MAIN COMPUTER LABS SETUP --------------
'------------------------------------------------------------------------------------
'Lab T104
if Left(strComputerName, 4) = "T104" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\matrix\t104"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t104-colour"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t104"
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Lab T115
if Left(strComputerName, 4) = "T115" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\matrix\t104"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t104-colour"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t104"
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Lab T119
if Left(strComputerName, 4) = "T119" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\matrix\t119"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t119-colour"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t119"
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Lab T313
if Left(strComputerName, 4) = "T313" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\matrix\t313"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t313"
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Lab T203
if Left(strComputerName, 4) = "T203" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\t203-2000\printer"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\t203-2000\printer"
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Lab CN-2
if Left(strComputerName, 4) = "CN-2" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\matrix\admin"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\admin"
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Lab 4th Floor
if Left(strComputerName, 4) = "4THF" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\4thfloor\printer"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\4thfloor-02-2000\printer"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\4thfloor\printer"
WshNetwork.SetDefaultPrinter PrinterPath
end if
'------------------------------------------------------------------------------------
'Libaray Computers
if Left(strComputerName, 4) = "LIB-" Then
on Error Resume Next
Set WshNetwork = CreateObject("WScript.Network")
'-------------------------------------------------------------
PrinterPath = "\\LIB-02-2000\printer"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t119"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t119-colour"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\LIB-02-2000\printer"
WshNetwork.SetDefaultPrinter PrinterPath
end if



'-------------------------------------------------------------------------------------------
'Finish this program

strStatus = strStatus & vbCRLF & "Network Logon Complete..." & vbCRLF
ie.document.all.wstatus.InnerText = strStatus

'-------------------------------------------------------------------------------------------
' Close IE status window

If not ie.document.all.holdit.checked then
ie.quit()
End if

' End of logon script
'-----------------------------------------------------------------------------------















Public Sub Main()

'Main loop to detect group that user belongs to
adsPath = "WinNT://chgirls.school.nz/" & strUser
Set dso = GetObject("WinNT:")
Set objUser = dso.OpenDSObject(adsPath,"","", ADS_READONLY_SERVER)

For Each Prop In objUser.groups
Select Case Prop.Name

'NOTE: the group name is case Sensitive

Case "Girls"
Call Division1()

Case "STAFF"
Call Division2()

Case "MATHS"
Call Division3()

Case "esol"
Call Division4()

Case "REFiles"
Call Division5()

Case "INTERNATIONAL"
Call Division6()

Case "ONSTREAM"
Call Division7()

Case "Enterprise"
Call Division8()

Case "Netguide"
Call Division9()

Case "admin_mainprinter"
Call Division10()


'etc NOTE: if a user is a member of many case then they will be called in turn

End Select
Next 'Prop

End Sub

'--------------------------------------------------------------------------------------
Sub CreateIE()

On Error Resume Next
Set IE = CreateObject("InternetExplorer.Application")
With IE
.navigate "http://matrix/logon.htm"
.resizable=0
.height=190
.width=500
.menubar=0
.toolbar=0
.statusBar=0
.visible=1
End With
Do while ie.Busy
' wait for page to load
Wscript.Sleep 300
Loop

End Sub

Sub Division1()
'students group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "q:", "\\chgirls\student"
strHomeDir = "\\chgirls\" & strUser & "$"
oNet.MapNetworkDrive "w:", strHomeDir
End Sub

Sub Division2()
' staff group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "q:", "\\chgirls\student"
oNet.MapNetworkDrive "n:", "\\chgirls\staff_files"
strHomeDir = "\\chgirls\" & strUser & "$"
oNet.MapNetworkDrive "w:", strHomeDir
End Sub

Sub Division3()
' maths
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "i:", "\\chgirls\maths"
End Sub

Sub Division4()
' esol grouop
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "i:", "\\chgirls\esol_students"
End Sub

Sub Division5()
'refiles group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "k:", "\\chgirls\re$" 'NOTE: this may need to be set to just re
End Sub

Sub Division6()
'International
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "h:", "\\chgirls\international"
End Sub

Sub Division7()
'Onstream group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "h:", "\\chgirls\onstream"
End Sub

Sub Division8()
'Enterprise group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "q:", "\\chgirls\enterprise"
End Sub

Sub Division9()
'Net Guide group
Set oNet = CreateObject("Wscript.Network")
oNet.MapNetworkDrive "z:", "\\chgirls\netguide"
End Sub

Sub Division10()
'Administrator Main Printer group
PrinterPath = "\\matrix\admin"
WshNetwork.RemovePrinterConnection "LPT1:" ' Remove any LPT mapping before hand
WshNetwork.AddWindowsPrinterConnection PrinterPath
WshNetwork.AddPrinterConnection "LPT1:", PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t119"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\matrix\t119-colour"
WshNetwork.AddWindowsPrinterConnection PrinterPath
'-------------------------------------------------------------
PrinterPath = "\\LIB-02-2000\printer"
WshNetwork.SetDefaultPrinter PrinterPath
End Sub

' end of script two


**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...