0

A while back I posted a question asking for a VB script to delete a shortcut "TeamViewer 12 Host" from anyone's and everyone's desktop if it existed. Local not remote. I ran it on my Windows 10 workstation and it worked. It even compensated for my desktop being redirected to a server. The problem is I can't get it to work on anyone else's computer. On everyone else's, even though they are local administrators, they get an Access Denied error when it tries to delete it. They too are local administrators. I have even tried running it from an elevated command prompt. No joy. The odd thing is that I can navigate to it via File Explorer and delete it no problem. Only think I can think is that it is something in the script. Any idea what is going on?

    ' Specify filename to remove from user desktops
strShortcut = "TeamViewer 12 Host.lnk"

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

' Find root of user data folder (C:\USERS on recent versions of Windows)
strUsers = objFSO.GetParentFolderName(objFSO.GetParentFolderName(objShell.SpecialFolders("Desktop")))
Set objUsers = objFSO.GetFolder(strUsers)

' Check each user folder, and look for our file in the DESKTOP subfolder
For Each objFolder In objUsers.SubFolders
    strCheck = objFolder & "\Desktop\" & strShortcut
    Wscript.Echo "Checking:" & strCheck
    ' If shortcut file exists remove it
    If objFSO.FileExists(strCheck) Then
        Wscript.Echo "Deleting:" & strCheck
        objFSO.DeleteFile(strCheck)
    End If
Next
1
  • does it help to add on error resume next to the start of the script? Commented Mar 16, 2017 at 21:06

1 Answer 1

0

If you want to skip the problem of the redirected desktop and the permissions problem created by the "All Users" junction, you can hard-code the search directory and skip any search paths that include "All Users." Example (the two changes are marked with comments):

strShortcut = "TeamViewer 12 Host.lnk"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

strUsers = "C:\Users" ' <-- or "C:\Documents and Settings" for XP, etc.
Set objUsers = objFSO.GetFolder(strUsers)

For Each objFolder In objUsers.SubFolders
    strCheck = objFolder & "\Desktop\" & strShortcut
    If InStr(strCheck, "All Users") = 0 Then ' <-- SKIP "All Users" to avoid permission problems
        Wscript.Echo "Checking:" & strCheck
        If objFSO.FileExists(strCheck) Then
            Wscript.Echo "Deleting:" & strCheck
            objFSO.DeleteFile(strCheck)
        End If
    End If
Next

Original answer:

You mentioned that your desktop is redirected to a network location. If your users have traversal rights to your network profile path (strUsers in this context), they should receive an "Access Denied" error as the For Each loop tries to delete a file from another user's redirected desktop.

Example: Deleting \\fileserver\profiles\YourUser\Desktop\TeamViewer 12 Host.lnk will be fine, but deleting \\fileserver\profiles\SomeOtherUser\Desktop\TeamViewer 12 Host.lnk will give "Access Denied."

16
  • Does not compute. My desktop is redirected. Their's isn't. It works on mine. Doesn't work on theirs. In conjunction with that even if it were the case the error would be File Not Found and not Access Denied. Try it on your local computer. Put a "TeamViewer 12 Host" shortcut in the User\Public\Public Desktop folder and try the script. Commented Mar 16, 2017 at 18:59
  • All our domain accounts have redirected desktops, so that was my context. Running it from a local administrator account works for me as long as the shortcut isn't on the Public desktop, in which case it still gives "Permission Denied." For your users, is the shortcut on their desktop or the public desktop? Commented Mar 16, 2017 at 21:51
  • The shortcut could be anywhere but as a rule it is in the Public\Desktop. You now see the problem. I can navigate to that folder and delete the icon manually but the script gives "Access Denied". That would lead me to believe that the problem is in the script. Even running the script at an elevated command prompt yield "Access Denied". Commented Mar 17, 2017 at 13:03
  • I think the problem is the Public desktop. Because it checks folders alphabetically, it checks "All Users" before "Public," and "All Users" is a junction to "Public" with different permissions. Try putting something like this before your "Checking:" echo: If InStr(strCheck, "All Users") <> 0 Then Wscript.Echo "Skipping All Users" Else Commented Mar 17, 2017 at 13:35
  • That worked but actually brought up more questions. The first being why it couldn't delete from "All Users"? Local admin and ran at an elevated prompt no less but the real question.... I know why it worked on mine now. It never looked on the C: drive. It determined that my desktop was on the server and never looked at C: so it never found an "All Users" folder. I don't know enough about VB but will this script even work if a user is not logged in? Commented Mar 17, 2017 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .