Solved

Script to Create shortcut on Users Desktop

Posted on 2014-01-06
25
710 Views
Last Modified: 2014-01-10
I'd like to create a script that I can send out to 100 different users to create a shortcut on their desktop.  This will be file they download from for instance an email and manually run from the desktop or wherever.

Is there 1 script I can apply for 100 different user profiles?

The structure would look like this...

User A from Property A
User B from Property B
User C from Property C
etc...

create shortcut to:

"c:\Local Cloud\Shared\Sites\Property-A\PM-Master-ALL"
"c:\Local Cloud\Shared\Sites\Property B\PM-Master-ALL"
"c:\Local Cloud\Shared\Sites\Property-C\PM-Master-ALL"
"c:\Local Cloud\Shared\Sites\Property D\PM-Master-ALL"
etc, etc.....

**note: The path will have spaces on some not on others.

Windows Domains are not in play here, so no GPO, no domain.
0
Comment
Question by:Vontech615
  • 11
  • 7
  • 4
  • +1
25 Comments
 
LVL 10

Expert Comment

by:Korbus
ID: 39759749
I dont understand the first part of your script where you say

User A from Property A
User B from Property B

What does this mean?
What information exists on each user's system that differentiates them from other users?  You mention no domain, so should the script run based on username?
0
 
LVL 12

Accepted Solution

by:
piattnd earned 250 total points
ID: 39759753
Copying or creating the shortcuts isn't too bad, but your filter will be a bit difficult.

This link describes creating a shortcut link via vbscript:

http://visualbasic.about.com/od/quicktips/qt/createshortcut.htm

As for your filters, it appears you have a 2 step filtering process.  What information will you have in order to identify that User A is from Property A and User B is from Property B?  Usually I'd rely on Active Directory to determine who should get what, but that's obviously not a reality for you.

In my opinion, if you do not control their networked environment, you will have no reliable filter for the Property a user belongs to, so I'd create different scripts for each Property and provide them the script that's relevant for their Property.

Not difficult, just change the path for your shortcut for that Property and you're good to go.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39759777
Sorry that's a bit confusing I was just trying to convey that it needs to apply to 100 different user profiles (ie. Betty at Property A) Desktop's and link to 100 different paths on C: drive.  Because each user works at a different Apartment complex so for instance here's a few example paths...

c:\Local Cloud\Shared\Sites\Arp Manor
c:\Local Cloud\Shared\Sites\Bailey Lane
c:\Local Cloud\Shared\Sites\Clearwater
c:\Local Cloud\Shared\Sites\Meadow Oaks

The User part was just saying that at each of these properties there is a Manager that will be running the file.
0
 
LVL 12

Expert Comment

by:piattnd
ID: 39759792
When you're doing scripting, you have to identify what criteria you have available to make sure your logic operates correctly IF you desire to have multiple possibilities within the same script.  What information do you have from each site that tells you "this is site A" or "this is site B"?  Computer name?  IP address?  Keep in mind that you want to script information that is as static as possible.

For example, a computer name can change.  If you have a naming convention and you stick to it, that is a really good way to go.  Going by user name is a logic nightmare, so don't count on that one.

If you don't have any information to confidently identify the location of the computer/user programmatically, then you're better off creating separate scripts for each location and you send it to the person who is going to run it for that location.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39759807
well the only things that change are the User name and folder path that's the target.  Computer name and ip address aren't really relevant right?
0
 
LVL 10

Expert Comment

by:Korbus
ID: 39759832
Ah, so you want to use the system's currently logged in username to determine which drives to map?  Is that corecct?  
And the first part of your script specifies what SITE to use for every possible username?

Please confirm if I understand correctly.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39759852
I think so....  all I'm trying to accomplish is creating 1 shortcut on the desktop of 100 different users to that particularly user's folder path.  The username is irrelevant right because they will be logged into their account when running the script.  The issue is the different folder paths.  That's probably what piattnd is referring to.  Since the target path changes per user than I may have to customize the script for each user.
0
 
LVL 10

Expert Comment

by:Korbus
ID: 39759925
"Since the target path changes per user than I may have to customize the script for each user."

Either that, or your script will need to be able to, at run-time, compare the system's username with what you have in the script, and then map the appropriate drives based on that.  However, as paittnd metnioned this could be a real nightmare, especially if usernames change.  That being said, it CAN be done this way, (let me know if you want more details).

I also agree with paittnd on this: assuming you have fewer "sites" than users, and assuming that each "site" with require the same drive mappings, then generate one script for each SITE, rather than for each user.  If users can belong to multiple "sites" you might need to send them one script for each.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39759957
K, I may do this.  How about deleting a shortcut from their desktop in the same script?  Right now my script looks like this...

L_Welcome_MsgBox_Message_Text = _
    "A shortcut to the PM Master" & _
    vbcrlf & "will be created on your desktop."
 L_Welcome_MsgBox_Title_Text = _
    "Windows Scripting Host Sample"
 Call Welcome()
 Dim WSHShell
 Set WSHShell = _
    WScript.CreateObject("WScript.Shell")
 Dim MyShortcut, MyDesktop, DesktopPath
 ' Read desktop path using WshSpecialFolders object
 DesktopPath = _
    WSHShell.SpecialFolders("Desktop")
 ' Create a shortcut object on the desktop
 Set MyShortcut = _
    WSHShell.CreateShortcut( _
    DesktopPath & "\PM-Master-ALL.lnk")
 ' Set shortcut object properties and save it
 MyShortcut.TargetPath = _
    WSHShell.ExpandEnvironmentStrings( _
    "c:\Local Cloud\Shared\Sites\Bailey Lane\PM-Master-ALL")
 MyShortcut.WorkingDirectory = _
    WSHShell.ExpandEnvironmentStrings( _
    "")
 MyShortcut.WindowStyle = 4
 MyShortcut.IconLocation = _
    WSHShell.ExpandEnvironmentStrings( _
    "c:\Local Cloud\Shared\Sites\Bailey Lane\PM-Master-ALL, 0")
MyShortcut.Save
 WScript.Echo _
    "A shortcut to the PM Master has been successfully created.  Please right click and delete the old shortcut."
 Sub Welcome()
    Dim intDoIt
    intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, _
       vbOKCancel + vbInformation, _
       L_Welcome_MsgBox_Title_Text )
    If intDoIt = vbCancel Then
       WScript.Quit
    End If
 End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:piattnd
ID: 39759999
Test it out on your own machine first, but it looks good.  If you get an error, post what line it's on and we'll see what we can figure out.  You probably want to create a section to see if a file exists, so you can either trigger an overwrite, or as you mentioned, a delete.
0
 
LVL 12

Expert Comment

by:piattnd
ID: 39760020
Actually, I think you've got some stuff messed up there.

You left in a bunch of "WSHShell.ExpandEnvironmentString" and put in your own string.  You don't want to do this.  For example:

MyShortcut.IconLocation = WSHShell.ExpandEnvironmentStrings("c:\Local Cloud\Shared\Sites\Bailey Lane\PM-Master-ALL, 0")

2 things. First, it's looking for the location of the icon.  Do you have an icon you want for this shortcut or is the standard windows explorer icon fine?  If you did have a path:

MyShortcut.IconLocation = "C:\MyLocation\SomeFolder"

That's what it would look like UNLESS you really did want to use an environment string.

Review some of your other statements for similar errors.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39760921
Hi, you could do this using a "map" with a dictionary object.  In the script, add the username, and the corresponding folder to link to.

I have also commented out an objFSO.DeleteFile line that you can use to delete an old shorcut, if you know the name of it.

Regards,

Rob.

Dim dctMap
Set dctMap = CreateObject("Scripting.Dictionary")
dctMap.CompareMode = vbTextCompare

dctMap.Add "usera", "c:\Local Cloud\Shared\Sites\Arp Manor\PM-Master-ALL"
dctMap.Add "userb", "c:\Local Cloud\Shared\Sites\Bailey Lane\PM-Master-ALL"
dctMap.Add "userc", "c:\Local Cloud\Shared\Sites\Clearwater\PM-Master-ALL"
dctMap.Add "userd", "c:\Local Cloud\Shared\Sites\Meadow Oaks\PM-Master-ALL"

L_Welcome_MsgBox_Message_Text = "A shortcut to the PM Master" & vbcrlf & "will be created on your desktop."
L_Welcome_MsgBox_Title_Text = "Windows Scripting Host Sample"

Call Welcome()

Dim WSHShell, WSHNetwork, strUsername
Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
strUsername = WSHNetwork.UserName
If dctMap.Exists(strUsername) = True Then
	Dim MyShortcut, MyDesktop, DesktopPath
	' Read desktop path using WshSpecialFolders object
	DesktopPath = WSHShell.SpecialFolders("Desktop")
	' Create a shortcut object on the desktop
	Set MyShortcut = WSHShell.CreateShortcut(DesktopPath & "\PM-Master-ALL.lnk")
	' Set shortcut object properties and save it
	MyShortcut.TargetPath = dctMap(strUsername)
	MyShortcut.WorkingDirectory = WSHShell.ExpandEnvironmentStrings("")
	MyShortcut.WindowStyle = 4
	MyShortcut.IconLocation = dctMap(strUsername) & ",0"
	MyShortcut.Save
	WScript.Echo "A shortcut to the PM Master has been successfully created.  Please right click and delete the old shortcut."
	' Delete a shorcut
	Dim objFSO
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	'objFSO.DeleteFile DesktopPath & "\PM-Master.lnk", True
Else
	WScript.Echo "You do not have a path associated with your username. No shorcut will be created."
End If

Sub Welcome()
	Dim intDoIt
	intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, vbOKCancel + vbInformation, L_Welcome_MsgBox_Title_Text )
	If intDoIt = vbCancel Then
		WScript.Quit
	End If
End Sub

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:Vontech615
ID: 39761956
Hey thanks Robsampson, that looks handy.  I don't exactly know the username of every machine. I do know the folder paths though.  I could maybe get all this information collected though and if so this would be perfect.  Thanks for this!

Just a question on deleteing the folder.  

Here's the code...

Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = Shell.SpecialFolders("Desktop")
FSO.DeleteFolder "C:\Users\dvaughn\Desktop\PM Master - ALL.lnk"

Open in new window


a.) If I'm adding it to the other lines of code I don't need to set the Shell right?
b.) Where do I insert this code, does it matter?  Obviously not at the end.
c.) I'm getting error line 4, character 1, Path not found.  Possibly the spaces in the folder name?  That can't change because that's the name of the folder on all of their desktops.
0
 
LVL 12

Assisted Solution

by:piattnd
piattnd earned 250 total points
ID: 39762879
You're trying to delete a folder, but specifying a file.  Change the FSO.DeleteFolder to FSO.DeleteFile.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39762934
ah yes.  I actually had FSO.DeleteFile first but I get "File not found".
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39762939
Should I start a new question for this?
0
 
LVL 12

Expert Comment

by:piattnd
ID: 39763011
I will let Rob respond to your other points.  I haven't gone through the entire set of responses to the question to accurately answer your new question :)
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 250 total points
ID: 39763884
You should be able to use
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = Shell.SpecialFolders("Desktop")
FSO.DeleteFile DesktopPath & "\PM Master - ALL.lnk", True

Open in new window


to delete a single file, as long as it exists.  To extend that to check for it, you can use
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = Shell.SpecialFolders("Desktop")
strFile = DesktopPath & "\PM Master - ALL.lnk"
If FSO.FileExists(strFile) = True Then
   FSO.DeleteFile strFile, True
Else
   MsgBox strFile & " was not found."
End If

Open in new window


Once you have declared the WScript.Shell object, Scripting.FileSystemObject object, and set the DesktopPath, you do not need to declare or set those any more further down in the code.  So as long as those are near the top, you only need to have them once, and then you can use any amount of DeleteFile calls that you need.

Regards,

Rob.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39765137
Many thanks Rob for your help!  I've got it all working now.  I promise this is the last question :)  

Some of the shortcuts may be "PM Master - All - Shortcut".  Can add a line that says "if this exist delete.. if not delete this" ?

So basically if the "PM Master - ALL.ink" exists delete that but if the "PM Master - ALL - Shortcut.ink" exists delete that.
0
 
LVL 12

Expert Comment

by:piattnd
ID: 39766220
**EDIT**

Rob's syntax includes the if/then statement needed if a file exists.  If you want to have another condition, simply make another If statement with the appropriate file to check.
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 250 total points
ID: 39766882
Yes, but I assign the file name to strFile to avoid duplicating the full path....that doesn't sit well with multiple If statements.

Perhaps use something like this:
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = Shell.SpecialFolders("Desktop")
arrFiles = Array(DesktopPath & "\PM Master - ALL.lnk", DesktopPath & "\PM Master - ALL - Shortcut.lnk")
For Each strFile In arrFiles
	If FSO.FileExists(strFile) = True Then
	   FSO.DeleteFile strFile, True
	Else
	   'MsgBox strFile & " was not found."
	End If
Next

Open in new window


Regards,

Rob.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39769038
Well this is what I got but I'm still getting line 31 syntax error.  


L_Welcome_MsgBox_Message_Text = "A shortcut to the PM Master" & vbcrlf & "will be created on your desktop."
L_Welcome_MsgBox_Title_Text ="Windows Scripting Host Sample"
 
Call Welcome()

Dim WSHShell
Set WSHShell =CreateObject("WScript.Shell")
 
	Dim MyShortcut, MyDesktop, DesktopPath
	' Read desktop path using WshSpecialFolders object
 	DesktopPath =WSHShell.SpecialFolders("Desktop")
 	' Create a shortcut object on the desktop
 	Set MyShortcut =WSHShell.CreateShortcut(DesktopPath & "\PM-Master-ALL.lnk")
 	' Set shortcut object properties and save it
 	MyShortcut.TargetPath =WSHShell.ExpandEnvironmentStrings( "c:\Local Cloud\Shared\Sites\Bailey Lane\PM-Master-ALL")
	MyShortcut.Save
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
DesktopPath = Shell.SpecialFolders("Desktop")
arrFiles = Array(DesktopPath & "\PM Master - ALL.lnk", DesktopPath & "\PM Master - ALL - Shortcut.lnk")
For Each strFile In arrFiles
	If FSO.FileExists(strFile) = True Then
	   FSO.DeleteFile strFile, True
	Else
	   'MsgBox strFile & " was not found."
	End If


WScript.Echo "A shortcut to the PM Master has been successfully created. The older PM Master shortcut has been deleted."
 
Sub Welcome()
    Dim intDoIt
    intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, vbOKCancel + vbInformation, L_Welcome_MsgBox_Title_Text )
    If intDoIt = vbCancel Then
       WScript.Quit
	   
	   End if
End Sub   

Open in new window

0
 
LVL 1

Author Comment

by:Vontech615
ID: 39769048
Nevermind!  It's working now.  I had to add "Next" as Rob did in his last post.
0
 
LVL 1

Author Comment

by:Vontech615
ID: 39769051
Now I just need to add a statement to refresh the desktop and this puppy's ready to go.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39769811
Hi, unfortunately, I think that's where you're stuck!  VBScript doesn't provide any reliable way to invoke a desktop refresh.  You may just need to instruct users to press F5 while viewing the Desktop.

I think Powershell can do it more reliable, since it can use API calls, so you could search for some Powershell code if you wanted to do it that way.

Regards,

Rob.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now