
不久前,我發布了一個問題,要求使用 VB 腳本從任何人和每個人的桌面上刪除捷徑「TeamViewer 12 Host」(如果存在)。本地而非遠端。我在我的 Windows 10 工作站上運行它並且它有效。它甚至補償了我的桌面被重新導向到伺服器的情況。問題是我無法讓它在其他人的計算機上運行。在其他人身上,即使他們是本地管理員,當嘗試刪除它時,他們也會收到“訪問被拒絕”錯誤。他們也是當地的管理員。我什至嘗試從提升的命令提示字元運行它。沒有喜悅。奇怪的是,我可以透過檔案總管導航到它並毫無問題地刪除它。我唯一能想到的是,這是劇本裡面的東西。知道發生了什麼事嗎?
' 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
如果您想跳過重定向桌面的問題以及「所有使用者」連結建立的權限問題,您可以對搜尋目錄進行硬編碼,並跳過任何包含「所有使用者」的搜尋路徑。範例(兩個變更已標記為註解):
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
原答案:
您提到您的桌面已重新導向至網路位置。如果您的使用者擁有對您的網路設定檔路徑的遍歷權限(strUsers
在此上下文中),他們應該For Each
當循環嘗試從另一個使用者的重新導向桌面刪除檔案時,收到「存取被拒絕」錯誤。
範例:刪除\\fileserver\profiles\YourUser\Desktop\TeamViewer 12 Host.lnk
沒問題,但刪除後\\fileserver\profiles\SomeOtherUser\Desktop\TeamViewer 12 Host.lnk
會出現「存取被拒絕」的情況。