Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Network Share

Status
Not open for further replies.

w33mhz

MIS
May 22, 2007
531
US
OK this was working and now I can't seem to get it to work from any remote machine or locally. I am trying to delete a network share and I have tried a couple different techniques. I must not have something correct. here are the techniques I am using:
Code:
DeleteUsrFolder(DeleteME)

WScript.Echo "Done"


Sub DeleteUsrFolder(UserString)
strComputer = "machine-name"
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = '" & UserString & "'")
For Each objShare in colShares
WScript.Echo objShare.Name
    objShare.Delete
Next
end Sub
and
Code:
DeleteUsrFolder(DeleteME)

WScript.Echo "Done"

sub DeleteUsrFolder(UserString)
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oDelFolder
Dim oFileCollection
Dim oFile
Dim oFolderCollection
strComputer = "machine-name"
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "\\" & strComputer & "\" & UserString
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFolderCollection = oFolder.SubFolders
set oFileCollection = oFolder.Files

For each oFile in oFileCollection
                oFile.Delete(True)
Next
For each oDelFolder in oFolderCollection
                oDelFolder.Delete(True)
Next

'Clean up

Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing         
end Sub
 
I do not recieve any errors, but I have noticed that in the first one it never executes the "WScript.Echo objShare.Name" in my for each loop and I have verified the folder name I am using is correct.
 
I just tried this and it works. I removed the sub for testing.
Code:
Set WshNetwork = CreateObject("WScript.Network")
UserString = WshNetwork.UserName


Wscript.echo UserString
WScript.Echo "Done"

strComputer = "PC Name"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery ("Select * from Win32_Share Where Name = '" & UserString & "'")
	
For Each objShare in colShares
WScript.Echo objShare.Name
    objShare.Delete
Next
 
with the SUB
Code:
Set WshNetwork = CreateObject("WScript.Network")
UserString = WshNetwork.UserName

Wscript.echo UserString
DeleteMe = UserString
DeleteUsrFolder(DeleteME)

Sub DeleteUsrFolder(UserString)
strComputer = "PC name"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery ("Select * from Win32_Share Where Name = '" & UserString & "'")
	
For Each objShare in colShares
    objShare.Delete
Next
end sub
 
Hmmmm, well defining "UserString" as a network object really should have nothing to do with the "UserString" that is in the Sub. Makes me wonder if there is something wrong with the WMI on the remote machine(s)?
 
The account under which script is executed should be in the local admin group (modulo minute relaxation depending on what to delete) of the remote machine.
 
Well ok, I feel very dumb now. I see my problem. I actually fixed the problem when I posted the code. I was missing a space between a quote and an ampersand. I didn't caught that in my code. LoL Thanks anyways
 
>I was missing a space between a quote and an ampersand.
A space and an ampersand?! what is the problem.

>I actually fixed the problem when I posted the code.
And idea did not come to you naturally to notify the forum.
 
tsuji
I did notify when I figured it out. I appologize that I didn't do it sooner but my PC crashed and I had to reinstall. Here was the problem

Code:
Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = '"& UserString & "'")
note the double quotation and the ampersand in front of UserString. There needs to be a space there.
should be
Code:
Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = '" & UserString & "'")
I didn't notice it in my code when I posted, and when I copied it up I fixed the code to be genaric and not have my specific information in it. While doing that I put a space in where I needed it. I just didn't realize it until it was too late. :)
 
>note the double quotation and the ampersand in front of UserString. There needs to be a space there.
Never heard of it being a problem in the kind of line.
 
well "white space" is not suppose to matter in vbscript, you are suppose to be able to add as much "white space" as you want and not affect performance. But with no space it probably didn't know where one string started and the other ended. I would think it would generate an error, but it didn't. I guess I could use a stringbuilder to keep me from making silly mistakes like that, but I doubt it.

 
You can keep your imagination as wide open as you like and use as many tools as you like, but that is not the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top