Link to home
Start Free TrialLog in
Avatar of Vontech615
Vontech615Flag for United States of America

asked on

Script to Create shortcut on Users Desktop

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.
Avatar of Korbus
Korbus

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?
ASKER CERTIFIED SOLUTION
Avatar of piattnd
piattnd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vontech615

ASKER

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.
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.
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?
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.
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.
"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.
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

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.
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.
Avatar of RobSampson
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

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ah yes.  I actually had FSO.DeleteFile first but I get "File not found".
Should I start a new question for this?
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 :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
**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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Nevermind!  It's working now.  I had to add "Next" as Rob did in his last post.
Now I just need to add a statement to refresh the desktop and this puppy's ready to go.
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.