?
Solved

VB Logon Script to delete current network drives an add new ones

Posted on 2007-10-20
8
Medium Priority
?
1,201 Views
Last Modified: 2012-06-21
We have currently upgraded servers which means all new network drives...etc. So Here is my NEW logon script to map network drives. What I want is for it to delete all network drives and map these ones. If the new ones are already there, then not to error out but to finish..Is this possible, since I know almost nothing of vb scripting! Thanks!

Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "Z:" , "\\dataserver\data"
objNetwork.MapNetworkDrive "Y:" , "\\dataserver\files"
objNetwork.MapNetworkDrive "X:" , "\\server01\scan"
0
Comment
Question by:LeviDaily
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
dlangr earned 1000 total points
ID: 20115188
Did not test it, but should work:

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set oPrinters = WshNetwork.EnumPrinterConnections
For i = 0 to oDrives.Count - 1 Step 2
WshNetwork.RemoveNetworkDrive oDrives.Item(i)
oDrives.Item(i+1)
Next
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20115193
0
 
LVL 2

Author Comment

by:LeviDaily
ID: 20115236
dlangr.....

That worked great Thanks....should it also delete network printers??
0
Industry Leaders: 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!

 
LVL 12

Assisted Solution

by:chandru_sol
chandru_sol earned 800 total points
ID: 20115280
This code should help you in removing printers....

Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
      WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
'To remove all printers incuding LOCAL printers use this statement and comment out the If Statement above
WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
Next

'Remove a specific printer
WSHNetwork.RemovePrinterConnection "\\ServerOld\HP5si",True,True
                               
'Install A Printer
WSHNetwork.AddWindowsPrinterConnection "\\Server\HP5si"
0
 
LVL 2

Author Comment

by:LeviDaily
ID: 20115538
Sorry...but I dont understand it so much.....do I have to enter in the OLD network printers, or can I just delete ALL network printers, but keep local printers?

Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
      WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
0
 
LVL 7

Expert Comment

by:dlangr
ID: 20115736
no, it should not, you can leave this line out:

Set oPrinters = WshNetwork.EnumPrinterConnections

I included it accidentally and only fills and it does not do anything in this case.
0
 
LVL 7

Expert Comment

by:dlangr
ID: 20115745
to clarify: You should only delete printers when you see a reason to do so, and since you did not ask for it.... i guess you don't want to.
0
 
LVL 12

Assisted Solution

by:weareit
weareit earned 200 total points
ID: 20115810
A method to remove network printers and leave local printers alone:

      Option Explicit ' Force Explicit Declarations

      'Declare constants
      const kErrorSuccess = 0
      const kNameSpace = "root\cimv2"

      ' Declare variables
      ' Object variables
      Dim objWMISvc ' WMI Service Object

      ' Miscellaneous variables
      Dim varIdx ' Index Counting
      Dim strComputer, colItems, Printers
      Dim oPrinter, oService, iResult, strPrinterName
      Dim bDelete, bConnection, strTemp, remPrn
      Dim strPrinter ' Printer string
      Dim strUser ' User string
      Dim strPassword ' Password string

      ' Set computer to local computer
      strComputer ="."

      ' Call WMIConn function
      If WMIConn(strComputer, kNameSpace, strUser, strPassword, oService) Then

            ' Set Printers to an instance of the oServer based on the printer
            set Printers = oService.InstancesOf("Win32_Printer")

            ' Set the WMI Service object to get objects from the local computer
            Set objWMISvc = GetObject("winmgmts:\\" & strComputer & "\" & kNameSpace)

            ' Set the Collected Items to be installed printers
            Set colItems = objWMISvc.ExecQuery ("SELECT * FROM Win32_Printer WHERE Local = False")

            ' For loop to work with enumerated printer connections
            For Each oPrinter In Printers
                  ' Remove network printer connections
                  oPrinter.Delete_
            ' Next iteration in for-loop
            Next
      End If

Function WMIConn(strComputer, strNameSpace, strUser, strPassword, oService)
      On Error Resume Next
      Dim oLocator
      Dim bResult
      oService = null
      bResult = FALSE
      Set oLocator = CreateObject("WbemScripting.SWbemLocator")
      If Err = kErrorSuccess Then
            Set oService = oLocator.ConnectServer(strComputer, strNameSpace, strUser, strPassword)
            If Err = kErrorSuccess then
                  bResult = true
                  oService.Security_.impersonationlevel = 3
                  '
                  ' Required to perform administrative tasks on the spooler service
                  '
                  oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
                  Err.Clear
            End if
      End if
      WMIConn = bResult
End Function

-saige-
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

569 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