Solved

Shared Directories Deleted but Share Remains in Computer Management

Posted on 2008-10-08
3
308 Views
Last Modified: 2012-05-05
I recently wrote a vbscript to delete a large number of user home directories that were inactive.  The script used the FileSystemObject.DeleteFile method.  All of the directories were deleted successfully but the share information for each of the directories still shows in Computer Manager.  What did I miss?  How can I remove the shares?  There are almost a thousand of them.  Thanks for your assistance.

RJT
0
Comment
Question by:robtroller
3 Comments
 
LVL 2

Accepted Solution

by:
jmoisy earned 250 total points
Comment Utility
Hello,

Before delete a share folder you must remove the share. Now that all the folder are deleting you must go to the registry and delete manually all the entries for the share.

HKLM\System\CurrentControlSet\Services\lanmanserver\Shares

After you must reboot the server.

To delete a share in VBS here is the script :

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = 'share'")
For Each objShare in colShares
    objShare.Delete
Next

Regards,
0
 
LVL 14

Assisted Solution

by:igor-1965
igor-1965 earned 250 total points
Comment Utility
If you have a list of the folders in the format drive:path you could remove shares by running windows command "NET SHARE <drive:path> /DELETE" for each folder.

This command works even if the folder has been deleted on the server so just amend your script to launch Windows shell with the command instead of calling for FileSystemObject.DeleteFile method.
 
0
 

Author Closing Comment

by:robtroller
Comment Utility
Thanks Guys!  Igor, your solution would definately have worked.  Moisy, I did not have to go into the registry.  This scirpt did the trick without a reboot even though the origional directories had been deleted.  Again, thanks to both, you got me on the right track!

On Error Resume next
ServerFile = "\\nenas02\adscripts$\Nas02Shares.txt"
'Create FileSystem Object
Set objfso = CreateObject("Scripting.FileSystemObject")
'Open Target And Log Files      
Set objserverFile = objFSO.OpenTextFile(serverFile, 1)
Do While objserverFile.AtEndOfStream <> True
      strName = objserverFile.ReadLine
      strComputer = "NENas02"
      set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      Set objShare = objWMI.Get("Win32_Share.Name='" & strName & "'")
      If Err.Number =  0 Then
            intRC = objShare.Delete
            If intRC <> 0 then
               WScript.Echo "Error deleted share: " & intRC
      Else
               WScript.Echo "Successfully deleted share " & strName
      end If
Else
      Err.Number = 0
      WScript.Echo "Skipped " & strName
End If
Loop
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

6 Experts available now in Live!

Get 1:1 Help Now