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
208 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
ID: 22779216
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
ID: 22779474
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
ID: 22779499
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
ID: 22779728
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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.
How to set-up an On Demand, IPSec, Site to SIte, VPN from a Draytek Vigor Router to a Cyberoam UTM Appliance. A concise guide to the settings required on both devices
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 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