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
209 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Performance monitors 6 44
BATCH to EXE Converter 2 67
powershell switch statement 7 30
Move files based on file names? 8 16
This article will show, step by step, how to integrate R code into a R Sweave document
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to count occurrences of each item in an array.
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…

679 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