Any VBScript guru's? Need help

I wrote a VBScript recently that is basically doing a copy from a network (Windows 2003 domain) share to the users desktop. Here is the code FWIW:

objFSO.CopyFile “\server\share$\desktop*.lnk” , DesktopPath , OverwriteExisting

For some reason I’m getting a VBScript error of path not found. However, when I go to the command line and type in \server\share$\desktop it works fine. It’s like the path isn’t there when the script runs but once the script finishes the path is there fine. I’ve put in an error check to get rid of the error message, but it doesn’t really solve the problem. It seems random…sometimes it works fine, other times it doesn’t work after 2 or even 3 login attempts.

If anyone has any advice or idea of what’s happening here I would very much appreciate it. I’m no VB guru myself, nor am I an expert with Windows 2003 Server (my MCSE was NT 4 Server/WS :stuck_out_tongue: ). I’ve used this script before at other sites though and haven’t run into this issue.

-XT

Can you hard code the path? Maybe using UNCs is barfing?

Well, that’s what’s weird. It is only happening periodically, not all the time. If it was happening all the time then I’d know better how to fix it.

Just an FYI for anyone interested, what I’m trying to do atm is put in a Do/While statement on the theory that what’s happening is that the script is somehow running before the user is fully logged in (or has all of their registered rights on the domain I suppose). So, it’s going to keep checking the UNC a set number of times until it either find it or runs through 10 tries (just arbitrarily picked 10 as a first attempt).

If anyone has a better idea I’m certainly open to suggestions. :slight_smile:

-XT

Without going into lecture mode, unless you have defined DesktopPath and OverwriteExisting, they are null variables when executed as shown.

It’s usually easier to troubleshoot these with all of the relevent code to look at.

But based on what you’ve posted, try this and see what happens.

'---------------------------------------------------------

Const OverwriteExisting = True

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objShell = WScript.CreateObject(“WScript.Shell”)

DesktopPath = objShell.SpecialFolders.Item(“Desktop”) ’ AllUsersDesktop for All Users

objFSO.CopyFile “\server\share$\desktop*.lnk” , DesktopPath , OverwriteExisting

ETA: Was typing this as you submitted you last post. Can we see your full code?

Projammer, thanks. All that stuff is in already though and working fine. It’s not a variable problem but seems to be related to the UNC pathing…and the problem seems intermittent as well (which is always a royal pain). Putting in the Do/While code seems to have ‘fixed’ the problem, which leads me to believe it has something to do with timing. My GUESS at this point is that for some reason the login script is running to fast, and is executing before all the users account creds are fully realized by the domain…IOW, while the user is logged in they don’t have all their rights on the domain yet, and so when it tries to query the UNC for the files they aren’t there because the user has no rights yet to look.

-XT

Don’t want to have to rip out all the user specific stuff, but here is part of the code if you like:

Option Explicit

Dim objNetwork, objShell, DriveLetter1, wshSysEnv, objFSO, FA(10), SM(10)

Dim MyShortcut, MyDesktop, DesktopPath, V

dim filesys, filetxt, getname, path, path2, mspath

Const OverwriteExisting = True

Set objNetwork = CreateObject(“WScript.Network”)
Set objShell = CreateObject(“WScript.Shell”)
set wshSysEnv = objShell.Environment(“PROCESS”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”)

DesktopPath = objShell.SpecialFolders(“Desktop”) 'Sets up desktop path object


If not objFSO.FileExists(DesktopPath & FA(N)) Then 'checks for existance of Students Home shortcut and puts on desktop if not found

path = objFSO.GetAbsolutePathName("\lhs\student$\desktop*.lnk")

’ WScript.Echo “got this far”

Do While V < 10

If objFSO.FileExists(path) Then 'checks for existance of Students Home shortcut and puts on desktop if not found


  objFSO.CopyFile "\\lhs\student$\desktop\*.lnk" , DesktopPath , OverwriteExisting 'Writes .lnk files from Student$\desktop to users desktop
  V = 11

Else

V = V + 1


end if	

Loop

End If
I know all the code isn’t in there but this is the part that is blowing up. The FA array is working fine btw…it’s simply an array of files listed that should be on the desktop.

-XT

Your guess is the same as mine would be. It’s trying to access the network before the network is available.

I’d add

WScript.Sleep 1000 ’ number is milliseconds

inside the loop. The way it’s written it executes as fast as it can so there’s no real wait to authenticate to the network.

And while there’s nothing wrong with copying the links, you might look into the CreateShortcut method. You can create the shortcuts before the network becomes available so the timing becomes largely irrelevent.

The for/while loop didn’t work…not enough time. I tried to add the sleep function myself but still haven’t tooned it right.

I know how to create short cuts for programs but am unsure how to create them for drive mappings with the icons I want. In fact, not sure how to create a short cut on the desktop for a drive mapping in any case. I’ve always just copied the short cuts for drive mappings (set up with the icons the customer wants), while making direct shortcuts for applications using the method you are describing. If you know how to make shortcuts of drive mappings (and even better, how to make them using a custom icon) then I’m all for it!

And thanks for the advice! :slight_smile:

-XT

Making a shortcut to a drive is just like making a shortcut to an application. You just leave off the application name but leave the final backslash.

Here’s a sample I put together and tested here.

Set objNetwork = WScript.CreateObject(“Wscript.Network”)
Set oFSO = CreateObject(“Scripting.FileSystemObject”)

If Not oFSO.DriveExists(“T:”) Then
objNetwork.MapNetworkDrive “T:”, “\FileServer\Vol1\Tech”
End If

set WshShell = WScript.CreateObject(“Wscript.Shell”)
strDesktop = WshShell.SpecialFolders(“Desktop”)

set objShortcut = WshShell.CreateShortcut(strDesktop & “\Sourceware.lnk”)

objShortcut.Description = “Network Source File Storage”

objShortcut.TargetPath = “\FileServer\Vol1\Tech\Sourceware” ’ or
'objShortcut.TargetPath = “T:\Sourceware”

objShortcut.IconLocation = “C:\Windows\System32\shell32.dll, 19”

objShortcut.Save
You can use the mapped drive or the UNC for the targetpath.

IconLocation is the full path to the file containing the icon. The number after the comma is the index to the icon image you want to use if the file contains more than one. The index is 0 base.

Windows 2000 Scripting Guide is an excellent resource for new or veteran administrators. ISBN 0-7656-1867-4

Excellent…I never knew that! Thanks for the help there. I’m going to try a re-write of that section of code tonight in my hotel room and see how it goes. If I can do it this way it will save having to do a bunch of checks. My only concern is that if it needs to access the UNC I may run into the same problem as that seems to be what is happening to the code I wrote.

At any rate, I’m going to give it a try and put this code snippet into my library thumbdrive for future reference.

Thanks again!

-XT

The MapNetworkDrive command will fail if the UNC is not accessable when executed.

Assuming you start off with an On Error Resume Next you can put the mapdrive in a loop with a Sleep 1000 or 2500 for 10 or 30 iterations.

CreateShortcut doesn’t care.

Glad I could help and good luck!