Desktop Icon Scripts for objShortcut.TargetPath

Home Forums Scripting Windows Script Host Desktop Icon Scripts for objShortcut.TargetPath

This topic contains 2 replies, has 2 voices, and was last updated by Rems Rems 6 years, 11 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar
    bobsha
    Member
    #160161

    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 & “rootCIMV2”)
    ‘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 *******************

    Rems
    Rems
    Moderator
    #228239

    Re: Desktop Icon Scripts for objShortcut.TargetPath

    bobsha;265976 wrote:
    […]

    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:

    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

    [B][COLOR=”Red”]strMyProfileFolder[/COLOR][/B] = GetSpecialFolder(USER_PROFILE)
    [B][COLOR=”Red”]strDefaultUserDesktop[/COLOR][/B] = GetSpecialFolder(ALL_USERS_DESKTOP)

    Function GetSpecialFolder(sfolder)
    With CreateObject(“Shell.Application”)
    With .Namespace(sfolder)
    GetSpecialFolder = .Self.Path
    End With
    End With
    End Function[/CODE]

    To read the current “Tartget path” of each found short cut, you can use this script part:
    [CODE]

    strLink = “[I]c:usersUserAccountNameDesktopshortcut.url[/I]” ‘example

    set WshShell = WScript.CreateObject(“WScript.Shell”)

    set oShellLink = WshShell.CreateShortcut([B]strLink[/B])

    [B][COLOR=”red”]scTargetPath[/COLOR][/B] = oShellLink.TargetPath)

    If Ucase(scTargetPath) = “SOME PATH” Then wscript.echo scTargetPath

    ‘note that string comparison is mostly [U]case sensitive[/U]. Therefore convert the string here to all caps like: Ucase(scTargetPath)
    [/CODE]

    [QUOTE=bobsha;265976]
    Then copy the new Shortcut from a UNC location to the “DEFAULT USER” or “PUBLIC” desktop based on if it was W7 or XP.[/QUOTE]
    Do not copy a prefixed shortcut file, it is more reliable to use a startup script or GPP to [U]create[/U] the shortcut in the desired folder.

    /Rems[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[/CODE]

    To read the current “Tartget path” of each found short cut, you can use this script part:

    strLink = “[I]c:usersUserAccountNameDesktopshortcut.url[/I]” ‘example

    set WshShell = WScript.CreateObject(“WScript.Shell”)

    set oShellLink = WshShell.CreateShortcut([B]strLink[/B])

    [B][COLOR=”red”]scTargetPath[/COLOR][/B] = oShellLink.TargetPath)

    If Ucase(scTargetPath) = “SOME PATH” Then wscript.echo scTargetPath

    ‘note that string comparison is mostly [U]case sensitive[/U]. Therefore convert the string here to all caps like: Ucase(scTargetPath)
    [/CODE]

    [QUOTE=bobsha;265976]
    Then copy the new Shortcut from a UNC location to the “DEFAULT USER” or “PUBLIC” desktop based on if it was W7 or XP.[/QUOTE]
    Do not copy a prefixed shortcut file, it is more reliable to use a startup script or GPP to [U]create[/U] the shortcut in the desired folder.

    /Rems[CODE]

    strLink = “c:usersUserAccountNameDesktopshortcut.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)
    [/CODE]

    bobsha;265976 wrote:
    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

    Rems
    Rems
    Moderator
    #228241

    Re: Desktop Icon Scripts for objShortcut.TargetPath

    bobsha;265976 wrote:
    Code:
    […]
    Set colItems = objWMI.ExecQuery(“SELECT * FROM Win32_OperatingSystem”,, 48)
    [B]***But what about X64?? ****[/B]
    […]

    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.

    _

    bobsha;265976 wrote:
    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 andscriptname.vbs” >”c:path andoutputfile.txt” 2>&1

    Code:
    ‘ – scriptname.vbs –

    Const USER_PROFILE = &H28&

    strComputer = “.”

    Set objWMIService = GetObject(“winmgmts:\” _
    & strComputer & “rootcimv2”)

    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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.