Sunday, November 11, 2012

VBScript for URL link creation


We had a need to create URL link of our project on users's desktop using a script. First stage the users had enough rights on their PC's so we included the required icon file also with the script and passed the following VBScript to users to click on it to create the desktop icon link.

' Definie shell object
Set objWSHShell = WScript.CreateObject("Wscript.Shell")
strDesktop = objWSHShell.SpecialFolders("Desktop")
' Copy the icon file to windows
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileCopy = objFSO.GetFile( objWSHShell.CurrentDirectory&"\icon.ico")
objFileCopy.Copy ("C:\WINDOWS\system32\")
' Define link properties and create link
strShortcutName = "Log In" ' Shortcut Name - Edit for the wanted name
strShortcutPath = "http://thesystem.com/Login.aspx" ' Link URL - Edit to the wanted URL
strIconPath = "C:\WINDOWS\system32\icon.ico"
Set objShortcut = objWSHShell.CreateShortcut(strDesktop & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath=strShortcutPath
objShortcut.IconLocation=strIconPath
objShortcut.Save

' Quit Script
WScript.Quit

But later the user access rights has been limited and were blocked accessing system folder, the link creator didn't created the link with supplied icon file. As the icon file is copied to the system folder. So the updated script would not use supplied icon file, but make use of existing system icon.

' Definie shell object
Set objWSHShell = WScript.CreateObject("Wscript.Shell")
strDesktop = objWSHShell.SpecialFolders("Desktop")

' Define link properties and create link
strShortcutName = "LogIn" ' Shortcut Name - Edit for the wanted name
strShortcutPath = "http://thesystem.com/Login.aspx" ' Link URL - Edit to the wanted URL
strIconPath = "c:\windows\system32\shell32.dll,44"
Set objShortcut = objWSHShell.CreateShortcut(strDesktop & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath=strShortcutPath
objShortcut.IconLocation=strIconPath
objShortcut.Save

' Quit Script
WScript.Quit

Now the icon shown from system.

No comments:

Post a Comment