Showing posts with label VB script. Show all posts
Showing posts with label VB script. Show all posts

Thursday 27 September 2012

Mapping Network Drive with Visual Basic Script

Mapping Network Drive with Visual Basic Script Following on from the Visual basic script for adding printers, this time i will give you an example of how to map network shares as drives.
This is pretty similar to the Print Script and uses the same formatting, the following example maps two drives R and S drives if you want to add more then you will need to change the script accordingly.
The script starts out with the (On Error Resume Next) again like the printer script so that it will move onto the next line of code if there is any errors
On Error Resume Next 
Set objSysInfo = CreateObject("ADSystemInfo") 
Set objNetwork = CreateObject("Wscript.Network") 
'removes mapped drive R 
objNetwork.RemoveNetworkDrive "R:", True, True 
'removes mapped drive S 
objNetwork.RemoveNetworkDrive "S:", True, True 
'creates maped drive R 
objNetwork.MapNetworkDrive "R:", "\\Server\Share1" 
'creates maped drive S 
objNetwork.MapNetworkDrive "S:", "\\Server\Share2" 
This script will remove the two drives before it adds them as with the printer script, un like the printer script it still works even if the drives are not already mapped, if they are already there it will remove and re-add them.
You can simply copy and past this script and change it to suit your needs this also needs to be saved with the file extension of *.vbs (mapdrives.vbs) and can be used with a Group Policy or can be double clicked and run.

Adding Printers with a Visual Basic Script

Adding Printers with a Visual Basic Script If you are a Network Administrator or work in a IT Role and it is your responsibility to install printers and maintain the network then this script will save you lots of time, if you take care of more than 10 computers you will know that having to install the same thing on each one can be pretty laborious, if you are running a Windows Server environment with a domain, you can use group policies to deploy your printers, Windows Server 2008 has a Deployed Printers option built in but can be sometimes unreliable, if you find that this is the case then you can use my script to deploy your printers.
We start out by opening notepad or any other txt file editor, on the first line we want to put in the following without the brackets,
(on error resume next) 
this tells the program when you encounter an error just continue at the next line. The next line we use the
Set objNetwork = CreateObject("WScript.Network") 

WScript.Network is a scripting object that provides methods and properties that allow for manipulation of network objects.
The next line you add in the printer
objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name"
after it has added the printer you will need to set it as the Default printer if you have more than one printer installed
objNetwork.SetDefaultPrinter "\\Server\Printer Shared Name"

So your finished script will look like this.
on error resume next
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name"
objNetwork.SetDefaultPrinter "\\Server\Printer Shared Name"
Save this with the file extension of *.vbs for example addprinter.vbs and then you can use it as a script in the logon process with a group policy, or if you simply double click on the file it will install the printer for you, if you are deploying this with group policy you will want to remove it after successful installation as it will not work the second time around, unless you change some of the parameters, as it will already be installed it will give you an error, the change to the script that is required is below
'objNetwork.RemovePrinterConnection "\\Server\Printer Shared Name"
This goes above the objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name" line in the script so your script will now look like this below.

on error resume next
Set objNetwork = CreateObject("WScript.Network")
'objNetwork.RemovePrinterConnection "\\Server\Printer Shared Name"
objNetwork.AddWindowsPrinterConnection "\\Server\Printer Shared Name"
objNetwork.SetDefaultPrinter "\\Server\Printer Shared Name"