Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

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

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
ugm-it
Asked:
ugm-it
  • 2
  • 2
1 Solution
 
exx1976Commented:
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
 
ugm-itAuthor Commented:
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
 
ugm-itAuthor Commented:
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
 
exx1976Commented:
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!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now