Deleting an expired shortcut from several remote workstations

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
operez
Posts: 14
Joined: Tue Aug 09, 2011 12:04 am

Deleting an expired shortcut from several remote workstations

Post by operez » Fri Jan 11, 2013 1:21 am

I need to delete an expired shortcut icon from several remote computers. I will be reading a text file with each workstation’s IP address and performing the following function. To test my logic I was running this script with just one IP address, but it doesn’t seem to find anything, even though if I do a search of this workstation's profile folder it finds three shortcuts, one in the administrator’s folder and the other two in two user folders.

Could someone please look at this and tell me where I went wrong?


DeleteExpiredShortcuts "10.227.201.261","Lotus Notes 6.4"

Function DeleteExpiredShortcuts(strComputer,strShortcut)
  Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")

  Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_ShortcutFile Where FileName = '" & strShortcut & "'")

  For Each objItem in colItems
    If Instr(LCase(objItem.Name), "desktop") Then
      strPath = objItem.Name
      strPath = Replace(strPath, "", "")
      Set colFiles = objWMIService.ExecQuery _
        ("Select * From CIM_Datafile Where Name = '" & strpath & "'")
      For Each objFile in colFiles
        WScript.Echo "Deleting " & objFile.Name
        'objFile.Delete
      Next
    End If
  Next
  strComputer = ""
  strShortcut = ""
End Function

Thank you in advance.

User avatar
jvierra
Posts: 13301
Joined: Tue May 22, 2007 9:57 am
Contact:

Deleting an expired shortcut from several remote workstations

Post by jvierra » Fri Jan 11, 2013 11:03 am

Some profiles are in All Users Profile and some are in the registry.

User avatar
jvierra
Posts: 13301
Joined: Tue May 22, 2007 9:57 am
Contact:

Deleting an expired shortcut from several remote workstations

Post by jvierra » Fri Jan 11, 2013 11:17 am

Start here:

DeleteExpiredShortcuts "10.227.201.261","Lotus Notes 6.4"

Function DeleteExpiredShortcuts(strComputer,strShortcut)

Set wmi = GetObject("winmgmts:" & strComputer & "rootcimv2")

Set colItems=wmi.ExecQuery("Select * From Win32_ShortcutFile Where FileName = '" & strShortcut & "'")

For Each objItem in colItems
If Instr(LCase(objItem.Name), "desktop") Then
WScript.Echo "Deleting " & objItem.Name
'objItem.Delete
End If
Next

End Function

User avatar
operez
Posts: 14
Joined: Tue Aug 09, 2011 12:04 am

Deleting an expired shortcut from several remote workstations

Post by operez » Mon Jan 14, 2013 4:53 am

Thank you. That seems to have worked.

Locked