Announcement

Collapse
No announcement yet.

Desktop Icon Scripts for objShortcut.TargetPath

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Desktop Icon Scripts for objShortcut.TargetPath

    Thanks in advance and please dont mind the noobness

    I need to search many PCs (With VBS if possible) but I will run this locally at login script or GPO

    * This PC can be Win7x32, Win7x64, XPx32, and XPx64

    I need to look at any profile on the box and look at the objShortcut.TargetPath for each icon.

    If the objShortcut.TargetPath = "some url or exe" delete it.

    Then copy the new Shortcut from a UNC location to the "DEFAULT USER" or "PUBLIC" desktop based on if it was W7 or XP.

    My assumption is that (And this is just the start....

    Basically if we deploy and icon for internal mail or whatever URL. Users may have renamed it to my mail or 123abc. I want to specifically attack the target either the URL or the *.exe and delete the icon and replace it. But then put that in the ALLUSERS or PUBLIC desktop so everyone gets the icon. Yes, multiple profiles on the PC. So there can be many unique names for them.

    Code:
    '********** START OF SCRIPT *******************
     
    'Create Objects
    Set objShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Set to local computer name
    strComputer = "."
    'Connect to computer WMI Service
    Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    'Get Operating System Information
    Set colItems = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem",, 48)
    ***But what about X64?? ****
    For Each objItem In colItems
    strOSversion = objItem.Version
    Next
    strOSversion = Left(strOSversion, 3)
    'Check Version and run correct sub
    Select Case strOSversion
    Case "5.0"
    WinXP
    Case "5.1"
    WinXP
    Case "6.0"
    Win7
    Case "6.1"
    Win7
    End Select
     
    Sub WinXP
    For each profile if objShortcut.TargetPath = xyz 
    then delete object
    then for %DefaultUser copy new icon from UNC
     
    Sub Win7
    For each profile if objShortcut.TargetPath = xyz 
    then delete object
    then for %Public Desktop copy new icon from UNC
     
    '********** ENDOF SCRIPT *******************
    Last edited by Rems; 24th October 2012, 10:16.

  • #2
    Re: Desktop Icon Scripts for objShortcut.TargetPath

    Originally posted by bobsha View Post
    [...]

    I will run this locally at login script or GPO

    I need to look at any profile on the box and look at the objShortcut.TargetPath for each icon.

    If the objShortcut.TargetPath = "some url or exe" delete it.
    If you run the script as user logon script it can only delete files from the user's own profile folder as the user has no permissions to read or change the profile folders of others. The script will also not be able to add a new shortcut file to the "All Users" desktop folder, this can only be programatically done with a computer startup script or GPP.

    To find all short cuts in the user's profile directory , search for files (use recursion)with the extension .LNK and .URL


    To get the user's profile folder (Logon script) use this script part:
    Code:
    Const USER_PROFILE = &H28&        'use this for the logon script to locate the current users's profile directory
    Const ALL_USERS_DESKTOP = &H19&   'use this for the startup script that creates the new shortcut
    
    strMyProfileFolder = GetSpecialFolder(USER_PROFILE)
    strDefaultUserDesktop = GetSpecialFolder(ALL_USERS_DESKTOP)
    
    
    Function GetSpecialFolder(sfolder)
       With CreateObject("Shell.Application")
          With .Namespace(sfolder)
             GetSpecialFolder = .Self.Path
          End With
       End With
    End Function
    To read the current "Tartget path" of each found short cut, you can use this script part:
    Code:
    strLink = "c:\users\UserAccountName\Desktop\shortcut.url"   'example
    
    
    set WshShell = WScript.CreateObject("WScript.Shell")
    
    set oShellLink = WshShell.CreateShortcut(strLink)
    
    scTargetPath = oShellLink.TargetPath)
    
    If Ucase(scTargetPath) = "SOME PATH" Then  wscript.echo scTargetPath
    
    'note that string comparison is mostly case sensitive. Therefore convert the string here to all caps like: Ucase(scTargetPath)

    Originally posted by bobsha View Post
    Then copy the new Shortcut from a UNC location to the "DEFAULT USER" or "PUBLIC" desktop based on if it was W7 or XP.
    Do not copy a prefixed shortcut file, it is more reliable to use a startup script or GPP to create the shortcut in the desired folder.


    /Rems
    Last edited by Rems; 24th October 2012, 15:03.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Desktop Icon Scripts for objShortcut.TargetPath

      Originally posted by bobsha View Post
      Code:
      [...]
      Set colItems = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem",, 48)
      ***But what about X64?? ****
      [...]
      The phrase "Win32" in the name Win32_OperatingSystem was just meant to indicate that it was not available in 16 bit operating systems. The name is unchanged in 64 bit versions of Windows.

      _
      Originally posted by bobsha View Post
      I need to look at any profile on the box and look at the objShortcut.TargetPath for each icon
      and that will be a lot of shortcuts to check. I think you should make a selection of folders instead of seaching all subfolders.

      Here a sample script that will search for all shortcut files (lnk and url) in all profile folders and subfolder on the computer.

      !! Run the script from a CSCRIPT.exe command line. And run it as an administrator.

      command line:
      cmd /c cscript /nologo "c:\path and\scriptname.vbs" >"c:\path and\outputfile.txt" 2>&1
      Code:
      ' -  scriptname.vbs  -
      
      Const USER_PROFILE = &H28&
      
      strComputer = "."
      
      Set objWMIService = GetObject("winmgmts:\\" _
         & strComputer & "\root\cimv2")
      
      Set WshShell = WScript.CreateObject("WScript.Shell")
      
      d = GetSpecialFolder(USER_PROFILE)
      ProfilesFldr = Left(d,inStrRev(d, "\")-1)
      
      wscript.echo ProfilesFldr & vbNewLine
      
      MySub ProfilesFldr
      
      wscript.quit
      
      
      Sub MySub(strFolderName)
         arrFolderPath = Split(strFolderName, "\")
         strNewPath = ""
      
         For i = 1 to Ubound(arrFolderPath)
           strNewPath = strNewPath & "\\" & arrFolderPath(i)
         Next
         strPath = strNewPath & "\\"
      
         Set colFiles = objWMIService.ExecQuery _
           ("Select * from CIM_DataFile where Path = '" & strPath _
             & "' AND (Extension = 'lnk' OR Extension = 'url')")
      
         For Each objFile in colFiles
      
           set oShellLink = WshShell.CreateShortcut(objFile.Name)
           jobTargetPath = oShellLink.TargetPath
      
           Wscript.echo "shortcut file  : " & objFile.Name & vbnewline _
                      & "shortcut target: " & jobTargetPath & vbnewline
         Next
      
         'Recursion
         Set colSubfolders = objWMIService.ExecQuery _
           ("Associators of {Win32_Directory.Name='" & strFolderName _
             & "'} Where AssocClass = Win32_Subdirectory ResultRole =" _
             & " PartComponent")
      
         For Each objFolder in colSubfolders : On Error Resume Next
           MySub objFolder.name
         Next
      End Sub
      
      Function GetSpecialFolder(sfolder)
         With CreateObject("Shell.Application")
            With .Namespace(sfolder)
               GetSpecialFolder = .Self.Path
            End With
         End With
      End Function
      /Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment

      Working...
      X