VBScript to copy shortcut based on Operating System

Answered VBScript to copy shortcut based on Operating System

  • Wednesday, May 02, 2012 8:35 PM
     
     

    I need a little help completing my script.  I am trying to run a script that determines the PC's operating system and then run an additional script based on the type of OS to install a shortcut on the All Users Desktop for an executable file that is saved on a server.  Below is the code that I have, but I keep getting errors.  Can someone please help?

    If it will help the two operating systems that I am dealing with are XP and Windows 7.

    Also, how could I change this script to take the PC names from a Text file?

    Code:

    Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime") 
    strComputer = "DA13021"
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem") 

    ForEach os in oss         
        'Wscript.Echo "Caption: " & os.Caption
        If os.Caption = "Microsoft Windows XP Professional"Then
            Call MSXP()
        Else    
            Call WIN7()      

    PrivateSub MSXP()
        strWorkDir ="\\R5nas01\Shares\R5DC_SP\Self Help Tool\"
        strAppPath = "\\R5nas01\Shares\R5DC_SP\Self Help Tool\Self Help Tool.exe
        strIconPath = "C:\Self Help Tool.exe,5"

        Set objShell = CreateObject("WScript.Shell")
        objDesktop = objShell.SpecialFolders("AllUsersStartMenu")
        Set objLink = objShell.CreateShortcut(objDesktop & "\Self Help Tool.lnk")

        objLink.TargetPath = strAppPath
        objLink.WindowStyle = 3
        objLink.WorkingDirectory = strWorkDir
        objLink.Save    
    EndSub

    PrivateSub WIN7()
        strWorkDir ="\\R5nas01\Shares\R5DC_SP\Self Help Tool\"
        strAppPath = "\\R5nas01\Shares\R5DC_SP\Self Help Tool\Self Help Tool.exe
        strIconPath = "C:\Self Help Tool.exe,5"

        Set objShell = CreateObject("WScript.Shell")
        objDesktop = objShell.SpecialFolders("AllUsersStartMenu")
        Set objLink = objShell.CreateShortcut(objDesktop & "\Self Help Tool.lnk")

        objLink.TargetPath = strAppPath
        objLink.WindowStyle = 3
        objLink.WorkingDirectory = strWorkDir
        objLink.Save
    EndSub

All Replies

  • Wednesday, May 02, 2012 8:52 PM
    Moderator
     
      Has Code

    Hi,

    I think it's much simpler to just ask the system the path to the all users desktop; e.g.:

    Dim WshShell
    Set WshShell = CreateObject("WScript.Shell")
    WScript.Echo WshShell.SpecialFolders.Item("AllUsersDesktop")

    Bill

  • Wednesday, May 02, 2012 8:56 PM
    Moderator
     
     

    Sorry, I see you're already doing that.

    But: When you say that something didn't work, you have to say how it didn't work. What errors are you getting (please copy and paste the exact error message)?

    Bill

  • Wednesday, May 02, 2012 9:14 PM
     
     

    Line: 13 Char: 9

    Syntax error

    Code: 800A03EA

    Microsoft VBScript compilation error

  • Wednesday, May 02, 2012 9:17 PM
    Moderator
     
     

    And that line of code is...? (Remember, I can't see your screen)

    Bill

  • Wednesday, May 02, 2012 9:23 PM
     
     
    Private Sub MSXP()
  • Wednesday, May 02, 2012 9:23 PM
    Moderator
     
     Answered

    Also, I don't know if your copy and paste wasn't successful, but I noticed some odd things about your code:

    • Your first For Each statement doesn't have a matching Next
    • Your first If statement doesn't have a matching End If
    • You have two subroutines (MSXP and WIN7) that contain what looks like exactly the same code

    Bill

    • Marked As Answer by C-Garst Wednesday, May 02, 2012 9:58 PM
    •  
  • Wednesday, May 02, 2012 9:33 PM
     
     
    I adjusted the first two issues you addressed.  The 3rd issue is because i haven't worked out the difference in installing the the All User Desktop in Windows 7 instead of XP
  • Wednesday, May 02, 2012 9:35 PM
    Moderator
     
     
    The 3rd issue is because i haven't worked out the difference in installing the the All User Desktop in Windows 7 instead of XP

    I don't understand the problem. The SpecialFolders "AllUsersDesktop" returns the path to the "all users" desktop regardless of platform (that's the entire point of using it).

    Bill

  • Wednesday, May 02, 2012 9:42 PM
     
     
    Got it!!!  That makes it so much easier!!  I was still researching any coding differences between Windows 7 and XP.  What can i do to change the script in the beginning though to feed the Computer names by a text file?
  • Wednesday, May 02, 2012 9:49 PM
    Moderator
     
     

    Hi,

    The FileSystemObject object has the TextStream object which lets you open and loop through the contents of a text file. Don't be helpless: You can find information too, if you try.

    Bill