[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Shared Directories Deleted but Share Remains in Computer Management

Posted on 2008-10-08
3
Medium Priority
?
325 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 750 total points
ID: 22667453
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 750 total points
ID: 22670546
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
ID: 31504627
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline

834 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