Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Remove all shares in a machine

Hi,

I need to remove all shares in a machine.Is it possible doing this remotely.I have a list of machines from which i need to remove all shares.

And the results have to be shared to a file.

Regards
Sharath
Avatar of sirbounty
sirbounty
Flag of United States of America image

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFile:Set objFile=objFSO.OpenTextFile("C:\Computers.txt")
Dim objOutput:Set objOutput=objFSO.CreateTextFile("C:\SharesRemoved.log")

Do While Not objFile.AtEndOfStream
  strPC=objFile.ReadLine
  On Error Resume Next  
  Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPC & "\root\cimv2")
  If Err.Number=0 Then
    Set colShares = objWMI.ExecQuery ("Select * from Win32_Share)
    For Each objShare in colShares
      objShare.Delete
      objOutput.WriteLine strPC & vbTab & objShare.Name & " removed."
    Next
  Else
    objOutput.WriteLine strPC & " could not be reached."
    On Error Goto 0
  End If
Loop

objOutput.Close
objFile.Close
Set objWMI=Nothing
Set objFSO=Nothing
Avatar of bsharath

ASKER

Hi,

I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Remove all shares in a machine.vbs
Line:      10
Char:      66
Error:      Unterminated string constant
Code:      800A0409
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFile:Set objFile=objFSO.OpenTextFile("C:\Computers.txt")
Dim objOutput:Set objOutput=objFSO.CreateTextFile("C:\SharesRemoved.log")

Do While Not objFile.AtEndOfStream
  strPC=objFile.ReadLine
  On Error Resume Next  
  Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPC & "\root\cimv2")
  If Err.Number=0 Then
    Set colShares = objWMI.ExecQuery ("Select * from Win32_Share")
    For Each objShare in colShares
      objShare.Delete
      objOutput.WriteLine strPC & vbTab & objShare.Name & " removed."
    Next
  Else
    objOutput.WriteLine strPC & " could not be reached."
    On Error Goto 0
  End If
Loop

objOutput.Close
objFile.Close
Set objWMI=Nothing
Set objFSO=Nothing
Sirbounty this worked but this is removing all shares even the default shares

dev-chen-pc1029      IPC$ removed.
dev-chen-pc1029      D$ removed.
dev-chen-pc1029      ADMIN$ removed.
dev-chen-pc1029      C$ removed.


Can this script omit these default shares
That's what you asked for..."I have a list of machines from which i need to remove all shares" :^)

This should do it...


Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Computers.txt")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\SharesRemoved.log")

Do While Not objFile.AtEndOfStream
  strPC = objFile.ReadLine
  On Error Resume Next
  Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPC & "\root\cimv2")
  If Err.Number = 0 Then
    Set colShares = objWMI.ExecQuery("Select * from Win32_Share")
    For Each objShare In colShares
      Select Case UCase(objShare.Name)
        Case "IPC$", "C$", "ADMIN$", "D$"  'add to this format if you want more ignored
          'Do nothing
          'Uncomment the next line if you want reporting on this...
          'objOutput.WriteLine strPC & vbTab & objShare.Name & " not removed."
        Case Else
            objShare.Delete
      End Select
      objOutput.WriteLine strPC & vbTab & objShare.Name & " removed."
    Next
  Else
    objOutput.WriteLine strPC & " could not be reached."
    On Error GoTo 0
  End If
Loop

objOutput.Close
objFile.Close
Set objWMI = Nothing
Set objFSO = Nothing
Sorry my mistake.I want to remove only user given shares.I tried it but still removes all shares
If you need to be selective, I think an easier way is to remove the shares via the registry.  All local shares on a PC can be found at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares.  Here you can delete any shares you want.  I recommend backing up this reg key in case you ever need to restore them.
Try this...


Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Computers.txt")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\SharesRemoved.log")
Do While Not objFile.AtEndOfStream
  strPC = objFile.ReadLine
  On Error Resume Next
  Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPC & "\root\cimv2")
  If Err.Number = 0 Then
    Set colShares = objWMI.ExecQuery("Select * from Win32_Share")
    For Each objShare In colShares
      Select Case UCase(objShare.Name)
wscript.echo "Identified protected share...not removing"
        Case "IPC$", "C$", "ADMIN$", "D$"  'add to this format if you want more ignored
          'Do nothing
          'Uncomment the next line if you want reporting on this...
          'objOutput.WriteLine strPC & vbTab & objShare.Name & " not removed."
        Case Else
wscript.echo "removing " & objShare.Name
            objShare.Delete
      End Select
      objOutput.WriteLine strPC & vbTab & objShare.Name & " removed."
    Next
  Else
    objOutput.WriteLine strPC & " could not be reached."
    On Error GoTo 0
  End If
Loop

objOutput.Close
objFile.Close
Set objWMI = Nothing
Set objFSO = Nothing
I get this error.

---------------------------
Windows Script Host
---------------------------
Script:      C:\Remove all shares given by users.vbs
Line:      13
Char:      9
Error:      Syntax error
Code:      800A03EA
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
Dropped that in the wrong spot...
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Computers.txt")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\SharesRemoved.log")
Do While Not objFile.AtEndOfStream
  strPC = objFile.ReadLine
  On Error Resume Next
  Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strPC & "\root\cimv2")
  If Err.Number = 0 Then
    Set colShares = objWMI.ExecQuery("Select * from Win32_Share")
    For Each objShare In colShares
      Select Case UCase(objShare.Name)
        Case "IPC$", "C$", "ADMIN$", "D$"  'add to this format if you want more ignored
wscript.echo "Identified protected share...not removing"
          'Do nothing
          'Uncomment the next line if you want reporting on this...
          'objOutput.WriteLine strPC & vbTab & objShare.Name & " not removed."
        Case Else
wscript.echo "removing " & objShare.Name
            objShare.Delete
      End Select
      objOutput.WriteLine strPC & vbTab & objShare.Name & " removed."
    Next
  Else
    objOutput.WriteLine strPC & " could not be reached."
    On Error GoTo 0
  End If
Loop

objOutput.Close
objFile.Close
Set objWMI = Nothing
Set objFSO = Nothing
I get this

---------------------------
Windows Script Host
---------------------------
removing E$
---------------------------
OK  
---------------------------
Can you exclude even E$
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How many PC's are you looking to do.  I think the registry changes may be easier.  The exported reg keys can be exported to a txt file.