Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB logon script to sync domain client time

Posted on 2009-02-18
15
Medium Priority
?
1,581 Views
Last Modified: 2012-05-06
I would like to use a vb script as a logon script. I currently have a script to map printers and drives but also want it to sync time with  one of the servers. Previously I did this with a bat file and net time but I now need the equivalent for vbs.
0
Comment
Question by:duncaninnz
  • 8
  • 4
  • 3
15 Comments
 
LVL 14

Expert Comment

by:rejoinder
ID: 23674719
Try this.  Please edit the domain name to match your own.
Dim objSyncTime
Dim objShell
Dim strDomain
strDomain = "some.domain.com"
Set objShell = CreateObject("WScript.Shell")
objSyncTime = objShell.Run("net time /domain:" & strDomain & " /set /y",0 , False)

Open in new window

0
 

Author Comment

by:duncaninnz
ID: 23674978
yes that works great. I am a bit of a novice though and I want to add it in to my existing script which maps printers and drives. I have tried copying it in but it doesnt work. Also I need to disconnect the network drives before connecting them so that it doesnt display an error that the resource already exists.
 This is the script I have so far

 Windows Logon Script.

' ------------------------------------------------------------------------------'

Option Explicit
Dim objNetwork, strUNCPrinter

strUNCPrinter = "\\evserver\xerox c525"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strUNCPrinter
objNetwork.SetDefaultPrinter strUNCPrinter


Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "R:" , "\\evserver\resources"


WScript.Quit
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 23675210
Please try this...
In the code I have also provided a few samples for you so you may expand the login script to include more printers or drive mappings.
Option Explicit
 
Dim objSyncTime
Dim objShell : Set objShell = CreateObject("WScript.Shell")
Dim strDomain
Dim objNetwork
Dim strUNCPrinter
 
strDomain = "some.domain.com"
 
'Sync time
objSyncTime = objShell.Run("net time /domain:" & strDomain & " /set /y",0 , False)
 
'Map printer
'Usage: MapPrinter "\\<print-server>\<printer-name>", "True" or "False"
'To map the printer as the default, use "True"
 
MapPrinter "\\evserver\xerox c525", "True"
'MapPrinter "\\evserver\someotherprinter", "False"
 
'Map network drive
'Usage: MapDrive "<drive-letter>:", "\\<server>\<share>"
 
MapDrive "R:", "\\evserver\resources"
'MapDrive "S:", "\\evserver\anothershare"
'MapDrive "T:", "\\evserver\yetanothershare-etc"
 
Sub MapPrinter(strPrinterPath,strPrinterDefault)
    On Error Resume Next
    WSHNetwork.AddWindowsPrinterConnection strPrinterPath
    WScript.Sleep 300
    If strPrinterDefault = "1" Or strPrinterDefault = UCase("TRUE") Then
        WSHNetwork.SetDefaultPrinter strPrinterPath
    End If
End Sub
 
Sub MapDrive(strDrive,strShare)
    On Error Resume Next
    Dim objNet
    Set objNet = CreateObject("Wscript.Network")
    objNet.MapNetworkDrive strDrive, strShare
    If Err.Number Then
        objNet.RemoveNetworkDrive strDrive
        objNet.MapNetworkDrive strDrive, strShare
    End If
End Sub

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:duncaninnz
ID: 23675377
That works for the time and the mapped derives but the printer doesnt map. Share name is definitely correct and it maps using the other script.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 23675670
My fault - forgot to declair something.
Please try this...
Option Explicit
 
Dim objSyncTime
Dim objShell : Set objShell = CreateObject("WScript.Shell")
Dim strDomain
Dim objNetwork
Dim strUNCPrinter
 
strDomain = "some.domain.com"
 
'Sync time
objSyncTime = objShell.Run("net time /domain:" & strDomain & " /set /y",0 , False)
 
'Map printer
'Usage: MapPrinter "\\<print-server>\<printer-name>", "True" or "False"
'To map the printer as the default, use "True"
 
MapPrinter "\\evserver\xerox c525", "True"
'MapPrinter "\\evserver\someotherprinter", "False"
 
'Map network drive
'Usage: MapDrive "<drive-letter>:", "\\<server>\<share>"
 
MapDrive "R:", "\\evserver\resources"
'MapDrive "S:", "\\evserver\anothershare"
'MapDrive "T:", "\\evserver\yetanothershare-etc"
 
Sub MapPrinter(strPrinterPath,strPrinterDefault)
    On Error Resume Next
    Dim objNet
    Set objNet = CreateObject("Wscript.Network")
    objNet.AddWindowsPrinterConnection strPrinterPath
    WScript.Sleep 300
    If strPrinterDefault = "1" Or strPrinterDefault = UCase("TRUE") Then
        objNet.SetDefaultPrinter strPrinterPath
    End If
End Sub
 
Sub MapDrive(strDrive,strShare)
    On Error Resume Next
    Dim objNet
    Set objNet = CreateObject("Wscript.Network")
    objNet.MapNetworkDrive strDrive, strShare
    If Err.Number Then
        objNet.RemoveNetworkDrive strDrive
        objNet.MapNetworkDrive strDrive, strShare
    End If
End Sub

Open in new window

0
 

Author Comment

by:duncaninnz
ID: 23675818
Still doesnt seem to work.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 23675915
Comment out line 29 and lets see what error message comes up.
0
 
LVL 14

Accepted Solution

by:
rejoinder earned 500 total points
ID: 23676003
So when I use this in place of the existing code, my printer maps and is set to the default...

Sub MapPrinter(strPrinterPath, strPrinterDefault)
    On Error Resume Next
    Dim objNet
    Set objNet = CreateObject("Wscript.Network")
    objNet.AddWindowsPrinterConnection strPrinterPath
    WScript.Sleep 1000
    If UCase(strPrinterDefault) = UCase("TRUE") Then
        objNet.SetDefaultPrinter strPrinterPath
    End If
End Sub
0
 

Author Closing Comment

by:duncaninnz
ID: 31548450
Great it works. I had deleted the printer off the server during my testing by mistake. Put it back on and works a treat. Thanks for your help.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 23676075
Glad I could help.
0
 
LVL 3

Expert Comment

by:Undisputed
ID: 25211883
I know this is closed but is there a way to map the network drives to friendly names?
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 25211913
Check this article out to see if it might be of use...
http://www.computerperformance.co.uk/ezine/ezine60.htm
0
 
LVL 3

Expert Comment

by:Undisputed
ID: 25211997
I noticed that page but was wondering how that would work if i have multiple drives to rename?
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 25212386
It could probably be shortened to something like this if you want to just rename the existing drive mapping.
Dim arrDriveLetter(5)
Dim arrDriveName(5)
 
arrDriveLetter(0) = "H:"
arrDriveName(0)   = "Home Drive"
 
arrDriveLetter(1) = "M:"
arrDriveName(1)   = "Administration"
 
'keep adding to the array but remember to match the array integer in both arrays
'The next drive will be arrDriveLetter(2), arrDriveLetter(3), etc.
 
Set objShell = CreateObject("Shell.Application")
 
for sDrive = LBound(arrDriveLetter) to UBound(arrDriveLetter)
	if arrDriveLetter(sDrive) <> "" AND arrDriveName(sDrive) <> "" then
		objShell.NameSpace(arrDriveLetter(sDrive)).Self.Name = arrDriveName(sDrive)
	end if
next

Open in new window

0
 
LVL 3

Expert Comment

by:Undisputed
ID: 25212537
so remove the mapdrive subroutine?

thanks for your help!

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

810 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