Announcement

Collapse
No announcement yet.

what's wrong with my logon script?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • what's wrong with my logon script?

    What is wrong with this login script? Keep getting error (see jpg attachment for display)

    The script works great, but throws an error often.

    Code:
    Const CSIDL_DESKTOP = &H10
    Dim strLNKPath, strLNKname
    Dim strTarget, strArguments
    Dim sDriveLetter, strUNC
    Dim objNetwork: set objNetwork = CreateObject("WScript.Network")
    Dim WshShell: set WshShell = CreateObject("WScript.Shell")
    Dim oShell: Set oShell = CreateObject("Shell.Application")
    Dim oDesktop: Set oDesktopFolder = oShell.Namespace(CSIDL_DESKTOP)
    Dim strDesktopFolder: strDesktopFolder = oDesktopFolder.Self.path
    'duplicate part between ---- sDriveLetter = "Z:"  strUNC = "\\server\sharedfolder"  strLNKname = "Network Share 1"
    '-----------------------------------------------------------------------
    '                          Mapping 1
    '
    '# Map drive:
    sDriveLetter = "N:"
    strUNC = "\\LPSERVER\Users Documents\Inventory"
    Call MapDrive(sDriveLetter, strUNC)
    '# Create shortcut:
    strLNKPath = strDesktopFolder
    strLNKname = "Inventory"
    strTarget = "explorer.exe"
    strArguments = "/e,/root," & sDriveLetter & "\"
    Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
    '# Mapping's alias: (optional - remove 'rem' to activate this routine)
    rem Call NameMapping(strLNKname)
    '-----------------------------------------------------------------------
    '-----------------------------------------------------------------------
    '                          Mapping 2
    '
    '# Map drive:
    sDriveLetter = "R:"
    strUNC = "\\LPSERVER\Users Documents\Receptionists"
    Call MapDrive(sDriveLetter, strUNC)
    '# Create shortcut:
    strLNKPath = strDesktopFolder
    strLNKname = "Receptionist"
    strTarget = "explorer.exe"
    strArguments = "/e,/root," & sDriveLetter & "\"
    Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
    '# Mapping's alias: (optional - remove 'rem' to activate this routine)
    rem Call NameMapping(strLNKname)
    '-----------------------------------------------------------------------
    '-----------------------------------------------------------------------
    '                          Mapping 2
    '
    '# Map drive:
    sDriveLetter = "S:"
    strUNC = "\\LPSERVER\HPDigitalSender"
    Call MapDrive(sDriveLetter, strUNC)
    '# Create shortcut:
    strLNKPath = strDesktopFolder
    strLNKname = "Scanner"
    strTarget = "explorer.exe"
    strArguments = "/e,/root," & sDriveLetter & "\"
    Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
    '# Mapping's alias: (optional - remove 'rem' to activate this routine)
    rem Call NameMapping(strLNKname)
    '-----------------------------------------------------------------------'-----------------------------------------------------------------------
    '                          Mapping 2
    '
    '# Map drive:
    sDriveLetter = "V:"
    strUNC = "\\LPSERVER\Users Documents\Doctors"
    Call MapDrive(sDriveLetter, strUNC)
    '# Create shortcut:
    strLNKPath = strDesktopFolder
    strLNKname = "Veterinarians"
    strTarget = "explorer.exe"
    strArguments = "/e,/root," & sDriveLetter & "\"
    Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
    '# Mapping's alias: (optional - remove 'rem' to activate this routine)
    rem Call NameMapping(strLNKname)
    '-----------------------------------------------------------------------
    ' next mappings and shortcuts....
    wscript.quit
    
    Sub CreateShortcut(sFolder, sName, sTarget, sArgs)
       With WshShell '(has a global scope)
          With .CreateShortcut(sFolder &"\" & sName  &".LNK")
             .TargetPath = sTarget
             .Description = sName
             .Arguments = sArgs
             .IconLocation = "shell32.dll, 9"
             .WindowStyle = 3
             .WorkingDirectory = "."
             .Save
          End With
       End With
    End Sub
    Sub MapDrive(DriveLetter, Share)
       On Error Resume Next
       With objNetwork
          .MapNetworkDrive DriveLetter, Share, False
       End With
    End Sub
    Sub NameMapping(sAlias)
       With oShell
          .NameSpace(sDriveLetter).Self.Name = sAlias
       End With
    End Sub
    Attached Files

  • #2
    Re: what's wrong with my logon script?

    Is the error every time it runs, or occasionally, or only for certain users?
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: what's wrong with my logon script?

      I would say it happens most of the time but not all of the time. And it's for all users on all machines. From what I found on google, it may have something to do with the fact that the links are already there (since it will re-create them each login) and *may* have read only attributes. Again, not sure but found similar issues with google search but no clear indication of what to do to fix it.

      Comment


      • #4
        Re: what's wrong with my logon script?

        What could causing this is that the LNK with that name was already created during a previous logon for this user, and for some reason it can not be overwritten. Verify that you are not trying to save over a read-only file, a file that is open in another application, or a file locked by an administrator.

        A possible workaround, you could try is to add an On error Resume Next statement to the "Sub CreateShortcut(sFolder, sName, sTarget, sArgs)" at the top of this sub routine. Note that this only suppress the error pop-up, the particular existing shotcut will not be overwritten still.
        Otherwise ,try a forced deletion of the shortcut if it exist already on the desktop before saving the new (updated) shortcut.


        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: what's wrong with my logon script?

          Originally posted by chakotay2 View Post
          I would say it happens most of the time but not all of the time. And it's for all users on all machines. From what I found on google, it may have something to do with the fact that the links are already there (since it will re-create them each login) and *may* have read only attributes. Again, not sure but found similar issues with google search but no clear indication of what to do to fix it.
          Using roaming user profiles perhaps?

          The error you posted here is for: strLNKname = "Inventory" (the first shortcut to be created) Does it does it occur only with that shortcut?
          After the user got the error is there or is there not already the shortcut that could not be saved during the last logon on the desktop for that user?


          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: what's wrong with my logon script?

            Originally posted by Rems View Post
            Using roaming user profiles perhaps?

            The error you posted here is for: strLNKname = "Inventory" (the first shortcut to be created) Does it does it occur only with that shortcut?
            After the user got the error is there or is there not already the shortcut that could not be saved during the last logon on the desktop for that user?


            \Rems
            I don't think we are using roaming profiles. And no, it occurs with more than one of the shortcuts, but I haven't really noticed if it's always the first few or what order.

            Yes, the folders are still on the desktop and saved each time, so it really doesn't need them again, this is just to create them again in case they got deleted (and to set them up on first login for a user).

            Allen

            Comment


            • #7
              Re: what's wrong with my logon script?

              Can anyone please help?

              Thanks.

              Comment


              • #8
                Re: what's wrong with my logon script?

                Have you tried adding 'On error Resume Next' into the subroutine "Sub CreateShortcut" at the top?


                \Rems




                EDIT
                Try this to work around the error

                Add this line somewhere at the top of your main script:
                Code:
                Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
                Then, replace the "Sub CreateShortcut" Sub routine with:
                Code:
                Sub CreateShortcut(sFolder, sName, sTarget, sArgs)
                   On Error Resume Next
                   strFile = sFolder &"\" & sName  &".LNK"
                   With FSO: If NOT .FileExists(strFile) then
                     With WshShell: With .CreateShortcut(strFile)
                         .TargetPath = sTarget
                         .Description = sName
                         .Arguments = sArgs
                         .IconLocation = "shell32.dll, 9"
                         .WindowStyle = 3
                         .WorkingDirectory = "."
                         .Save
                     End With: End With
                   End If: End With
                End Sub
                Last edited by Rems; 17th July 2009, 10:58.

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: what's wrong with my logon script?

                  I really don't like solutions that relies on killing error detection. Personally, unless there is an insurmountable problem I won't release a script to production if it contains ON ERROR RESUME NEXT. And then I ensure it is enabled for the minimum number of lines possible.

                  If your script isn't reporting errors, what do you do when something unexpected turns up and it silently fails for months on end?

                  Maybe in this case, you would be better served by checking to see if the link exists first, then delete if it does and recreate.

                  Comment


                  • #10
                    Re: what's wrong with my logon script?

                    Candy,

                    Can you show me what to do to make that happen? I really don't know scripting so I am really relying on online help to make this work.

                    I like the method you are suggesting as if I ever change the locations, it would be more dynamic in updating.

                    Thanks,
                    Allen

                    Comment


                    • #11
                      Re: what's wrong with my logon script?

                      Originally posted by chakotay2 View Post
                      Candy,

                      Can you show me what to do to make that happen? I really don't know scripting so I am really relying on online help to make this work.

                      I like the method you are suggesting as if I ever change the locations, it would be more dynamic in updating.

                      Thanks,
                      Allen
                      The updated Sub Routine in my last reply is doing so already. So you can comment out the On Error statement if you like.

                      I second Candy's remark, it is absolutely true. I reply that very often to people myself.

                      Any script you want to use in production needs to be tested in all conditions for errors and any problem arise need to be solved - not by just adding the Error Resume Next.!
                      That statement should only be used if you know what it exacly is you using it for in the script and you know what could be the consequences of using it.
                      (Still, some people agreed on this and add the On Error in, eventually turned out there do were unexpected consequences. Therefore this rule of thumb - Never start your script with On Error Resume Next. If you have to use it later on, be sure it is for error handeling not for hidding possible errors. Use On Error Goto 0 when you don't need error handling any more.)


                      In this particular case
                      If there turns out that there are errors while the purpose of using the script is essential and already running as startup or logon script, then you have to make a wisely decision, is it save to add the Error Resume Next and keep the script in use while you trying to find the cause of the error?
                      (That is why you should not use the On Error staments on fore hand, you would never be able to dicover there was a problem in the fisrt place)

                      In my original sample script there was the On Error Statement in just one of the subroutines, and that was a decision.


                      For the issue now - We know what the error is about, Allen already stated that it is not nesseray to overwrite the existing shortcut, but what important is that the users have a the shortcut and are not bothered with the error saying the existing short cut cannot be overwritten.

                      In my previous reply where I showed a possible work around, the On Error statement can be left out again in the new subroutine. The script is now checking for presence of the file and will write the schort-cut only if it does not exist. While the On Error stament here would do exaclty the samething, so you now can comment out that line.


                      \Rems

                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      __________________

                      ** Remember to give credit where credit's due **
                      and leave Reputation Points for meaningful posts

                      Comment


                      • #12
                        Re: what's wrong with my logon script?

                        Great! Thanks for the assistance. I will try it out this weekend.

                        Cheers!

                        Allen

                        Comment


                        • #13
                          Re: what's wrong with my logon script?

                          I was away for the weekend, thanks for stepping in Rems.

                          Comment


                          • #14
                            Re: what's wrong with my logon script?

                            Finally had time to go on site and test this...

                            Works like a champ! thanks all!

                            Comment


                            • #15
                              Re: what's wrong with my logon script?

                              ooops... spoke too soon... seems to work fine on some computers and gives this error on others (see attachment).
                              Here is the current script:

                              Code:
                              Const CSIDL_DESKTOP = &H10
                              Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
                              Dim strLNKPath, strLNKname
                              Dim strTarget, strArguments
                              Dim sDriveLetter, strUNC
                              Dim objNetwork: set objNetwork = CreateObject("WScript.Network")
                              Dim WshShell: set WshShell = CreateObject("WScript.Shell")
                              Dim oShell: Set oShell = CreateObject("Shell.Application")
                              Dim oDesktop: Set oDesktopFolder = oShell.Namespace(CSIDL_DESKTOP)
                              Dim strDesktopFolder: strDesktopFolder = oDesktopFolder.Self.path
                              'duplicate part between ---- sDriveLetter = "Z:"  strUNC = "\\server\sharedfolder"  strLNKname = "Network Share 1"
                              '-----------------------------------------------------------------------
                              '                          Mapping 1
                              '
                              '# Map drive:
                              sDriveLetter = "N:"
                              strUNC = "\\LPSERVER\Users Documents\Inventory"
                              Call MapDrive(sDriveLetter, strUNC)
                              '# Create shortcut:
                              strLNKPath = strDesktopFolder
                              strLNKname = "Inventory"
                              strTarget = "explorer.exe"
                              strArguments = "/e,/root," & sDriveLetter & "\"
                              Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
                              '# Mapping's alias: (optional - remove 'rem' to activate this routine)
                              Call NameMapping(strLNKname)
                              '-----------------------------------------------------------------------
                              '-----------------------------------------------------------------------
                              '                          Mapping 2
                              '
                              '# Map drive:
                              sDriveLetter = "R:"
                              strUNC = "\\LPSERVER\Users Documents\Receptionists"
                              Call MapDrive(sDriveLetter, strUNC)
                              '# Create shortcut:
                              strLNKPath = strDesktopFolder
                              strLNKname = "Receptionist"
                              strTarget = "explorer.exe"
                              strArguments = "/e,/root," & sDriveLetter & "\"
                              Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
                              '# Mapping's alias: (optional - remove 'rem' to activate this routine)
                              Call NameMapping(strLNKname)
                              '-----------------------------------------------------------------------
                              '-----------------------------------------------------------------------
                              '                          Mapping 2
                              '
                              '# Map drive:
                              sDriveLetter = "S:"
                              strUNC = "\\LPSERVER\HPDigitalSender"
                              Call MapDrive(sDriveLetter, strUNC)
                              '# Create shortcut:
                              strLNKPath = strDesktopFolder
                              strLNKname = "Scanner"
                              strTarget = "explorer.exe"
                              strArguments = "/e,/root," & sDriveLetter & "\"
                              Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
                              '# Mapping's alias: (optional - remove 'rem' to activate this routine)
                              Call NameMapping(strLNKname)
                              '-----------------------------------------------------------------------'-----------------------------------------------------------------------
                              '                          Mapping 2
                              '
                              '# Map drive:
                              sDriveLetter = "V:"
                              strUNC = "\\LPSERVER\Users Documents\Doctors"
                              Call MapDrive(sDriveLetter, strUNC)
                              '# Create shortcut:
                              strLNKPath = strDesktopFolder
                              strLNKname = "Veterinarians"
                              strTarget = "explorer.exe"
                              strArguments = "/e,/root," & sDriveLetter & "\"
                              Call CreateShortcut(strLNKPath, strLNKname, strTarget, strArguments)
                              '# Mapping's alias: (optional - remove 'rem' to activate this routine)
                              Call NameMapping(strLNKname)
                              '-----------------------------------------------------------------------
                              ' next mappings and shortcuts....
                              wscript.quit
                              
                              Sub CreateShortcut(sFolder, sName, sTarget, sArgs)
                              rem   On Error Resume Next
                                 strFile = sFolder &"\" & sName  &".LNK"
                                 With FSO: If NOT .FileExists(strFile) then
                                   With WshShell: With .CreateShortcut(strFile)
                                       .TargetPath = sTarget
                                       .Description = sName
                                       .Arguments = sArgs
                                       .IconLocation = "shell32.dll, 9"
                                       .WindowStyle = 3
                                       .WorkingDirectory = "."
                                       .Save
                                   End With: End With
                                 End If: End With
                              End Sub
                              Sub MapDrive(DriveLetter, Share)
                              rem   On Error Resume Next
                                 With objNetwork
                                    .MapNetworkDrive DriveLetter, Share, False
                                 End With
                              End Sub
                              Sub NameMapping(sAlias)
                                 With oShell
                                    .NameSpace(sDriveLetter).Self.Name = sAlias
                                 End With
                              End Sub
                              Attached Files
                              Last edited by chakotay2; 25th July 2009, 10:54.

                              Comment

                              Working...
                              X