Thursday, April 17, 2008

Create a new link in Favorites

Well, it's been a while since I've been able to post a new script.  Guess I've been too busy using my skillz to pay the billz as they say...  So today I'm posting a new script or rather, a snippet you can use in your own scripts.  This comes directly from my library of functions and is used frequently in enterprise logon scripts.


As a system administrator, you may find it necessary to create a script to add a link to a user's favorites.  There are several ways to do this including Group Policy, copying the .url or .lnk to the local computer, or vbscript.  I could put my smartz hat on and show you how it's done via group policies but this is a scripting blog.  I could also show you how to copy a shortcut file via copy, xcopy, or robocopy but that would be no fun.


So let's look at a script all ready, begeezus!  Fine.  There are really two things you can do.  1.  Copy a premade link using the filesystemobject.  Or, and better yet.. 2. Create a shortcut file on the fly using the Wscript.Shell object.



Sub CreateFavorite(sstrURL, sstrFileName, sstrFolder)
'Notes: Creates a favorite for the current logged on user.
'Input: strURL = URL to website
'Input: strFilename = file name to save (ex. technet.url)  
'Input: strFolder = folder to put the favorite in.  (pass null and it will default to the root)
    sstrURL = Trim(sstrURL)
    sstrFileName = Trim(sstrFileName)
    Set sobjFSO = CreateObject("Scripting.FileSystemObject")
    If IsNull(sstrFolder) Then
        sstrFolder = ""
    End If 
    If Not UCase(Right(sstrFileName,3)) = "URL" Then
        sstrFileName = sstrFileName & ".url"
    End If 
    Set sWshShell = CreateObject("WScript.Shell")
    sstrPath = sWshShell.SpecialFolders("Favorites")
    If Not sobjFSO.FolderExists(sstrPath & "\" & sstrFolder) Then
        sobjFSO.CreateFolder(sstrPath & "\" & sstrFolder)
    End If 
    Set sobjShortcutUrl = sWshShell.CreateShortcut(sstrPath & "\" & sstrFolder & "\" & sstrFileName)
    sobjShortcutUrl.TargetPath = sstrURL
End Sub 


To use:  Simply call CreateFavorite and pass the information.

Example:  CreateFavorite "", "VBScript Blog", "VBScript Stuff"


This will create a link called "VBScript Blog" to this website in a folder called VBScript Stuff inside Favorites.  Nifty. 

Take note that this is ran locally so you'll need to execute it via logon script or some other method like remote scripting, etc.  Enjoy!


Until next time (whenever that may be at this rate)!


1 comment:

Anonymous said...

awesome dude! it interests me a lot!! thanks for the share!!