Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple vbs script failing

Status
Not open for further replies.

superhl

IS-IT--Management
Dec 16, 2001
20
0
0
US
What am I doing wrong? I have verified the unc path is correct.

' VBScript to map a user s home drive using string variables
' Guy Thomas January 2005. Guy Thomas Page 15 of 70 LogonScripts V52
' ----------------------------------------
Dim objNetwork, strDriveLetter, strUNCpath, strUser
strDriveLetter = "K:"
strUNCpath = "\\encsd3\data\users\admin\"
Set objNetwork= CreateObject("Wscript.Network")
strUser =objNetwork.UserName
objNetwork.MapNetworkDrive strDrvieLetter, strUNCpath & struser
WScript.echo " Drive Mapped " & DriveLetter
WScript.Quit

Error: Line 9
char 1
Error: The specified network resource or device is no longer availabe
code 80070037
WSHNetwork.MapNetworkDrive
thanks
From ONScript, here is the error: WSHNetwork.MapNetworkDrive: The specified network resource or device is no longer available.
 
But have you check what strUser equals. If there is a space in strUser, the UNC path becomes invalid and will need to be surrounded by quotes.

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
And

Code:
WScript.echo " Drive Mapped " & [green]str[/green]DriveLetter

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks for the reply. Here is the complete path to the user folder.
\\encsd3\data\USERS\Admin\HLANCAST
Is the name of the folder(HLANCAST) the problem or do I need to change the syntax: strUser =objNetwork.UserName ?
 
You could trap errors at the MapNetworkDrive method, and then see if the folder actually exists.
Code:
Dim objNetwork, strDriveLetter, strUNCpath, strUser
strDriveLetter = "K:"
strUNCpath = "\\encsd3\data\users\admin\"
Set objNetwork= CreateObject("Wscript.Network")
strUser =objNetwork.UserName
[COLOR=blue]On Error Resume Next[/color]
objNetwork.MapNetworkDrive strDrvieLetter, strUNCpath & struser
[COLOR=blue]If Err.Number <> 0 Then
   WScript.echo "Got Error#" & Err.Number & ": " & Err.Description
   WScript.echo "While mapping " & strUNCpath & struser & " to " & strDriveLetter
   WScript.quit
End If
On Error Goto 0[/color]
WScript.echo " Drive Mapped " & DriveLetter
WScript.Quit
 
Here is the error message: Got Error#-2147024841. The specified network resource or device is no longer available, while mapping \\encsd3\data\users\admin\HBLancaster to K:
 
or fix the typos :)

Code:
objNetwork.MapNetworkDrive str[red]Drvie[/red]Letter, strUNCpath & struser

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Yea Geates, I missed that... I think that may be the problem:

superhl: I recommend adding the following line as the first line in all your scripts:
Code:
Option Explicit
This forces you to declare all your variables, which will prevent you from making typo's in variable names. strDrvieLetter (misspelled), was never assigned a value (only strDriveLetter was), so the MapNetworkDrive is trying to map a blank string to K:, which will fail.
 
Thanks! Here is the updated script. I added the Option Explicit and corrected the typos. Still getting same error: Here is the error message: Got Error#-2147024841. The specified network resource or device is no longer available, while mapping \\encsd3\data\users\admin\HBLancaster to K:

Option Explicit
Dim objNetwork, strDriveLetter, strUNCpath, strUser
strDriveLetter = "K:"
strUNCpath = "\\encsd3\data\users\admin\"
Set objNetwork= CreateObject("Wscript.Network")
strUser =objNetwork.UserName
On Error Resume Next
objNetwork.MapNetworkDrive strDriveLetter, strUNCpath & struser
If Err.Number <> 0 Then
WScript.echo "Got Error#" & Err.Number & ": " & Err.Description
WScript.echo "While mapping " & strUNCpath & struser & " to " & strDriveLetter
WScript.quit
End If
On Error Goto 0
WScript.echo " Drive Mapped " & DriveLetter
WScript.Quit
 
Can you browse to \\encsd3\data\users\admin\HBLancaster in Windows Explorer? I ask because earlier you called it HLANCAST.

Also, can you map the drive in Windows explorer, of with a NET USE command:

NET USE K: \\encsd3\data\users\admin\HBLancaster

 
I can browse. I changed the name for testing but still no go. Just perplexed why it will not work. I can map from cam line it works




C:\Users\hblancaster>net use K: /persistent:yes \\encsd3\data\users\admin\hblanc
ast
The command completed successfully.


C:\Users\hblancaster>
 
It there another way I can test this? thanks
 
Interesting.

OPTION EXPLICIT is specific so an error should occur before the script is run because DriveLetter is undefined.

Code:
WScript.echo " Drive Mapped " & [red]DriveLetter[/red]

However, an error occurs when mapping the UNCPath. I think your WMI is corrupt.

Move your error trap to the objNetwork definition. Any errors? If so, Microsoft now has an XP hotfix (KB933062)

Code:
Option Explicit
Dim objNetwork, strDriveLetter, strUNCpath, strUser
strDriveLetter = "K:"
strUNCpath = "\\encsd3\data\users\admin\"
[green]On Error Resume Next[/green]
Set objNetwork= CreateObject("Wscript.Network")
[green]if (err.Number) then msgbox err.description[/green]
strUser = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strUNCpath & struser

On Error Goto 0
WScript.echo " Drive Mapped " & strDriveLetter
WScript.Quit

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Again, thanks for your help!!!
I ran the following script:
Option Explicit
Dim objNetwork, strDriveLetter, strUNCpath, strUser
strDriveLetter = "K:"
strUNCpath = "\\encsd3\data\users\admin\"
On Error Resume Next
Set objNetwork= CreateObject("Wscript.Network")
if (err.Number) then msgbox err.description
strUser = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strUNCpath & struser

On Error Goto 0
WScript.echo " Drive Mapped " & strDriveLetter
WScript.Quit

I get popup that says: Drive Mapped K: However, I don't see the map drive within explorer.
I am running a windows 2003 server and a windows 7 workstation.
 
You got "Drive Mapped K:" because error trapping was on (on error resume next). An error probably occurred at "objNetwork.MapNetworkDrive strDriveLetter, strUNCpath & struser" but the error handler disregarded it.

Although, this did tell us that you WMI is not corrupt. If it was, you would have been prompted with an error.

Such a simple script yet such an elusive error.

What about user rights? does strUser have read/execute rights to the UNCPath?

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Sorry if I keep harping on this, but are you sure you are using the correct user folder name? I only ask because you have provided three different spellings so far: HLANCAST, hblancast, and HBLancaster

Otherwise, I haven't yet seen NET USE work, and MapNetworkDrive fail... definitely a strange one.
 
I can browse to the directory from the user login, save data and delete. I checked the rights on data= a shared folder with domain users permissions=full control and NTFS rights = read+execute,list, read
Users = domain user has NTFS read+execute, list, read
HBLancast=NTFS full control.


 
I'm not sure what to look at. You may just want to use NET USE via objShell to map your drives.

Code:
set objNetwork = WScript.CreateObject("Wscript.Network")
set objShell = Wscript.CreateObject("Wscript.Shell")

strDrive = "K:"
strPath = ""\\server\share\"
strUser = objNetwork.UserName

objShell.Run "net use " & strDrive & " " & strPath & strUser

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
The user login name is hblancaster and the folder name hblancaster. I had changed the name to hbancast for testing. When the mapping process is taking place, what exactly is it looking for. Sorry, I am a little confused when you say " are you using the correct folder name". How to do I verify this? There is only one folder that hblancaster has rights to in the user directory
\\encsd3\data\users\admin\hblancaster
 
superhl said:
Sorry, I am a little confused when you say " are you using the correct folder name". How to do I verify this?
I mean that if objNetwork.UserName is not returning hblancaster, the mapping will fail. Was just a guess on my part as to the possible cause of the problem.

If you know for a fact that \\encsd3\data\users\admin\hblancaster exists, then hardcode that path and test:
Code:
Option Explicit
Dim objNetwork, strDriveLetter, strUNCpath, strUser
strDriveLetter = "K:"
strUNCpath = "\\encsd3\data\users\admin\hblancaster"
Set objNetwork= CreateObject("Wscript.Network")
On Error Resume Next
objNetwork.MapNetworkDrive strDriveLetter, strUNCpath
If Err.Number <> 0 Then
   WScript.echo "Got Error#" & Err.Number & ": " & Err.Description
   WScript.echo "while mapping " & strUNCpath & " to " & strDriveLetter
   WScript.quit
End If
On Error Goto 0
WScript.echo " Drive Mapped " & strDriveLetter
WScript.Quit

If this works, then "objNetwork.UserName" in your other code is not returning the value you are expecting.

If this still doesn't work, explore Geate's code to use NET USE.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top