Solved

Would like to convert this remove printer [part of script] to do the same thing, but for network drives.

Posted on 2008-10-22
4
206 Views
Last Modified: 2010-04-24
I have a sub that removes all the network printers connected to our file and print server.  I have another section in the login script that removes network drives.  However, the network drives part isn't a sub, and its 'clunky'.  I am going thru the script and trying to clean things up, and what I'm looking for, is some assitance in converting the removeal of the network drives, and adding it in similar to the removeal of the network printers.  Any help in this matter is greatly appreciated, as I've been recently tasked with making the scripting changes, so I'm trying to learn as much as I can as quickly as I can, who loves a crash course!?!?!  Thanks again!
Set objNetwork = CreateObject("WScript.Network") 

On Error Resume Next

	objNetwork.RemoveNetworkDrive "H:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "J:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "K:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "M:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "P:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "Q:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "S:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "T:", _

	bforce, bUpdateProfile
 

RemovePrintersFromServer "ugmpdc"
 

sub RemovePrintersFromServer(sServerName)

    dim oPrinters, aPrinter

    if bolWriteLog then objLogFile.WriteLine(Now() & ": Removing all printers from: " & ucase(sServerName))

    Set oPrinters = objNetwork.EnumPrinterConnections

    For iLoop = 0 to oPrinters.Count - 1 Step 2

        on error resume next

            aPrinter = split(uCase(oPrinters.Item(iLoop+1)),"\",-1, 1)  '"

            if not UBound(aPrinter) = 0 then 'this is not a local printer, process code

                if aPrinter(2) = ucase(sServerName) then

                    objNetwork.RemovePrinterConnection oPrinters.Item(iLoop+1), true, true

                    If Err.Number = 0 Then

                        if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & oPrinters.Item(iLoop+1))

                    else

                        if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Remove printer: " & oPrinters.Item(iLoop+1))

                    end if

                end if

            end if

        aPrinter = ""

        on error goto 0

    next

    if bolWriteLog then objLogFile.WriteLine(Now() & ": Removed  all printers from: " & ucase(sServerName))

end sub

Open in new window

0
Comment
Question by:ugm-it
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:exx1976
Comment Utility
Like this?


removeNetworkDrives

RemovePrintersFromServer "ugmpdc"

 

sub RemovePrintersFromServer(sServerName)

    dim oPrinters, aPrinter

    if bolWriteLog then objLogFile.WriteLine(Now() & ": Removing all printers from: " & ucase(sServerName))

    Set oPrinters = objNetwork.EnumPrinterConnections

    For iLoop = 0 to oPrinters.Count - 1 Step 2

        on error resume next

            aPrinter = split(uCase(oPrinters.Item(iLoop+1)),"\",-1, 1)  '"

            if not UBound(aPrinter) = 0 then 'this is not a local printer, process code

                if aPrinter(2) = ucase(sServerName) then

                    objNetwork.RemovePrinterConnection oPrinters.Item(iLoop+1), true, true

                    If Err.Number = 0 Then

                        if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Remove printer: " & oPrinters.Item(iLoop+1))

                    else

                        if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Remove printer: " & oPrinters.Item(iLoop+1))

                    end if

                end if

            end if

        aPrinter = ""

        on error goto 0

    next

    if bolWriteLog then objLogFile.WriteLine(Now() & ": Removed  all printers from: " & ucase(sServerName))

end Sub
 
 

Sub removeNetworkDrives

	Set objNetwork = CreateObject("WScript.Network") 

	On Error Resume Next

	objNetwork.RemoveNetworkDrive "H:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "J:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "K:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "M:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "P:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "Q:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "S:", _

	bforce, bUpdateProfile

	objNetwork.RemoveNetworkDrive "T:", _

	bforce, bUpdateProfile

End Sub

Open in new window

0
 

Author Comment

by:ugm-it
Comment Utility
Well...  Kinda...  More like attached code.  Granted, this prolly wouldn't work.  Because I don't know how to specify individual drive letters, however...  I wouldn't mind if it just looped thru the alphabet and removed any network drives it came across.  If that is possible.
sub RemoveNetworkDrives

    dim oNetworkDrive, aNetworkDrive

    if bolWriteLog then objLogFile.WriteLine(Now() & ": Removing all Network Drives")

    Set oNetworkDrive = objNetwork.EnumNetworkDriveConnections

    For iLoop = 0 to oNetworkDrive.Count - 1 Step 2

        on error resume next

            aNetworkDrive = split(uCase(oNetworkDrive.Item(iLoop+1)),"\",-1, 1)  '"

            if not UBound(aNetworkDrive) = 0 then

                if aNetworkDrive(2) = ucase(??WHATSHOULDGOHERE??) then

                    objNetwork.RemoveNetworkDrives oNetworkDrive.Item(iLoop+1), true, true

                    If Err.Number = 0 Then

                        if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Removed network drive: " & oNetworkDrive.Item(iLoop+1))

                    else

                        if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Removeing network drive: " & oNetworkDrive.Item(iLoop+1))

                    end if

                end if

            end if

        aNetworkDrive = ""

        on error goto 0

    next

    if bolWriteLog then objLogFile.WriteLine(Now() & ": Removed  all network drives")

end Sub

Open in new window

0
 

Accepted Solution

by:
ugm-it earned 0 total points
Comment Utility
Ok...  I feel bad now, haven't paid too much attention to my subs till now (like I said, found most of this stuff online, and still learning all this)

But it looks like it already removes the network drive, before attempting to connect the new (if applicable) network drive.  Am I correct in this?  If so, how would I go about changing it so that it just removed all the network drives, then omitting that part from the mapping a network drive (since they have all been removed already) from the following snippet.

Thanks again for your help...  AGAIN!  =]
Sub DriveMapper(sDrive, sShare)

    For iLoop = 0 to objDrives.Count -1 Step 2

    if LCase(sDrive) = LCase(objDrives.Item(iLoop)) then

        if not LCase(sShare) = LCase(objDrives.Item(iLoop+1)) then

            objNetwork.RemoveNetworkDrive sDrive, true, true

        Else

            if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect  """ & sDrive & """ to share """ & _

                sShare & """ (previously connected)")

            Exit Sub

        End if

    End if

    Next

    on error resume next

    objNetwork.MapNetworkDrive sDrive, sShare

    If Err.Number = 0 Then

        if bolWriteLog then objLogFile.WriteLine(Now() & ": Success: Connect  """ & sDrive & """ to share """ & sShare & """")

    else

        if bolWriteLog then objLogFile.WriteLine(Now() & ": Failed:  Connect """ & sDrive & """ to share """ & sShare & """")

    end if

    on error goto 0

End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:exx1976
Comment Utility
Indeed, it looks like line 5 of the code you just posted removes mappings before line 14 puts them back..

It's hard to be sure without the rest of the code, but that looks like it does what you want.

Glad to be of service...   Again!  :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Quality of Service (QoS) options are nearly endless when it comes to networks today. This article is merely one example of how it can be handled in a hub-n-spoke design using a 3-tier configuration.
Is your computer hacked? learn how to detect and delete malware in your PC
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now