I am trying to modify all shortcuts on a computer. The script works fine but every now and then throws an error that the .Target property of an object is not available. Since my query only looks up files with a .lnk extension, this should never be the case. (For more details on this error you can see MS docs here: http://technet.microsoft.com/en-us/library/ff406382.aspx#H25)
The script in question:
strComputer = "."
Set wshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile WHERE Extension = 'lnk' AND Drive = 'C:'")
For Each objFile in colFiles
If InStr(1, ucase(objFile.Target), "METER.EXE") Then
Set objShortcut = wshShell.CreateShortcut(objFile.Name)
Wscript.Echo "FIXING: " & objShortcut.TargetPath
End If
Next
For the curious: The purpose of this script is to fix dozens of shortcuts on our lab machines which were previously modified to support a "home-grown" licensing/metering application. In all cases the original .EXE path was stripped from the target but can still be found from the shortcut's icon path.
Thanks
EDIT: The complete error message. It seems to appear more often after a restart, but not once I have run the script 2-3 times.
Microsoft VBScript runtime error: Object doesn't support this property or method: 'objFile.Target'