bsharath
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
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
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
-------------------------- -
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("Scrip ting.FileS ystemObjec t")
Dim objFile:Set objFile=objFSO.OpenTextFil e("C:\Comp uters.txt" )
Dim objOutput:Set objOutput=objFSO.CreateTex tFile("C:\ SharesRemo ved.log")
Do While Not objFile.AtEndOfStream
strPC=objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" & 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
Dim objFile:Set objFile=objFSO.OpenTextFil
Dim objOutput:Set objOutput=objFSO.CreateTex
Do While Not objFile.AtEndOfStream
strPC=objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper
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
ASKER
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
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.Fi leSystemOb ject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Co mputers.tx t")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\ SharesRemo ved.log")
Do While Not objFile.AtEndOfStream
strPC = objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" & 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
This should do it...
Dim objFSO: Set objFSO = CreateObject("Scripting.Fi
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Co
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\
Do While Not objFile.AtEndOfStream
strPC = objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper
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
ASKER
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\ CurrentCon trolSet\Se rvices\Lan manServer\ 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.Fi leSystemOb ject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Co mputers.tx t")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\ SharesRemo ved.log")
Do While Not objFile.AtEndOfStream
strPC = objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" & 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
Dim objFSO: Set objFSO = CreateObject("Scripting.Fi
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Co
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\
Do While Not objFile.AtEndOfStream
strPC = objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper
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
ASKER
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
-------------------------- -
--------------------------
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.Fi leSystemOb ject")
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Co mputers.tx t")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\ SharesRemo ved.log")
Do While Not objFile.AtEndOfStream
strPC = objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" & 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
Dim objFSO: Set objFSO = CreateObject("Scripting.Fi
Dim objFile: Set objFile = objFSO.OpenTextFile("C:\Co
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\
Do While Not objFile.AtEndOfStream
strPC = objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper
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
ASKER
I get this
-------------------------- -
Windows Script Host
-------------------------- -
removing E$
-------------------------- -
OK
-------------------------- -
Can you exclude even E$
--------------------------
Windows Script Host
--------------------------
removing E$
--------------------------
OK
--------------------------
Can you exclude even E$
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Dim objFile:Set objFile=objFSO.OpenTextFil
Dim objOutput:Set objOutput=objFSO.CreateTex
Do While Not objFile.AtEndOfStream
strPC=objFile.ReadLine
On Error Resume Next
Set objWMI = GetObject("winmgmts:{imper
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