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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to put mapped network drive into my docs 2

Status
Not open for further replies.

itsteapot

Technical User
Mar 27, 2009
49
GB
Hi All, is there,or does anyone know of a script to place the mapped drive shortcut into a users My Documents folder?
Thanks guys & gals for any help you can offer
 
untested...

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objNetwork = CreateObject("WScript.Network")

strUser = objNetwork.Username
strShortcut = "C:\path\to\shortcut.lnk"
strMyDocuments = "C:\Documents and Settings\" & strUser & "My Documents"

objFSO.CopyFile strShortcut, strMyDocuments, true

-Geates
 
Thanks i will give it a spin, will this work if i have used a script to map the drive to start with?
Ie:-

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj,objWMIServices, Path


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

Sub MapDrives
On Error Resume Next
Dim UserString, UserPassword, WSHNetwork
UserString = login.Value
UserPassword = password.Value

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.MapNetworkDrive "P:", "\\gandalf\PUPILS\" & UserString,True, UserString,UserPassword
 
Sorry the script fails, no errors just wont work.
Thanks
 
no errors just wont work"
take out the On Error Resume Next and you will begin to see the errors...if there are any
 
Thanks its failing on the line

objFSO.CopyFile strShortcut, strMyDocuments, true

with the error, file not found.
 
sorry that should have said, path not found.
i assume it is this bit?

strShortcut = "C:\path\to\shortcut.lnk"

thanks guys i have a script which maps the drive when a user inputs the username/password I just want to also add the link into the users My docs as well

 
before teh copyfile method call do something like

Wscript.echo "will copy " & strShortcut & " to " & strMyDocuments

you may find the strMyDocuments is not formatted correctly, or needs a "\" at the end or should define the file name as well in order to get the .CopyFile method to work

wscript.echo is your friend, or more precisely auditing is
 
Geates example copies an existing shortcut file. Your post implied you had created a shortcut, but simply mapping a drive does not do that.

Assuming your P: drive is already mapped this will create your shortcut
Code:
Set WSHShell = CreateObject("WScript.Shell")
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
strDoc = WshShell.SpecialFolders("MyDocuments")
strShortcut = strDoc & "\P-Drive.lnk"
wscript.echo strShortcut

If Not FSO.FileExists(strShortcut) Then
    Set Link = WshShell.CreateShortcut(strShortcut)
    Link.TargetPath = "P:\"
    Link.IconLocation = "P:\SpecialIcon.ico, 0"
    Link.Description = "P Drive"
    Link.Save
End If
You can comment out the icon line if you don't want to use a custom icon.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Thanks, MasterRacker, this works great as a stand alone vbs,
any ideas how i could intigrate it ito my script?



<head>
<title>Bungay Middle School</title>
<HTA:APPLICATION
APPLICATIONNAME="HTALogin"
SCROLL="no"
SINGLEINSTANCE="yes"
CAPTION="NO"
ShowInTaskbar="NO"
windowstate="Maximize"
BORDER = "thin"
BorderStyle = Complex


>
</head>
<SCRIPT LANGUAGE="VBScript">

sub Window_onLoad


'Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True

next
End Sub

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj,objWMIServices, Path


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")



Sub MapDrives
'On Error Resume Next
Dim UserString, UserPassword, WSHNetwork
UserString = login.Value
UserPassword = password.Value

Set WSHNetwork = CreateObject("WScript.Network")
WSHNetwork.MapNetworkDrive "P:", "\\gandalf\PUPILS\" & UserString,True, UserString,UserPassword


If err then
createobject("wscript.shell").popup "CHECK USERNAME or PASSWORD", 5," LOGON FAILED" ,0+16


else

createobject("wscript.shell").popup "LOGON SUCCESSFULL", 5, "AUTHORISED USERS ONLY" ,64
End if

'Clear the boxes now that we are done using the info
login.Value = ""
password.Value =""
UserString=Nothing
UserPassword=Nothing

window.resizeTo 370,270
window.moveTo 650,490


End Sub
Sub disconnectdrives
'Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
'NEW REMOVE SHORTCUT
'objFSO.CopyFile strShortcut, strMyDocuments, FALSE
'NEW REMOVE SHORTCUT

createobject("wscript.shell").popup "LOGGED OFF", 5, "AUTHORISED USERS ONLY" ,64



next

windowstate=maximize



End sub



</SCRIPT>


<body bgcolor="cyan"><center>
<h1><font color="BLUE" face="Times New Roman" size="6">Bungay Middle School</h1>
<table align="center">
<tr><td>User Number:</td><td><input type="text" name="login" size="5" maxsize="5"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" size="30"></td></tr>
<tr><td colspan="2" align="center"><input id=runbutton type="button" value="Logon" name="run_button"

onClick="MapDrives"></td></tr>

<tr><td colspan="4" align="center"><input id=runbutton type="button" value="logoff" name="run_button"

onClick="disconnectdrives"></td></tr>
</td></tr>
</table>
<p>
<span id = "DataArea"></span>
</center></body>

 
The quick and dirty way would be to paste the code into the MapDrives subroutine just after you map the drive.

Better would be to put the code into its own subroutine and then call that one from MapDrives.

Better yet, change your run button to call a subroutine that calls MapDrives then CreateLink. That would give you the best modularity.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Thanks MasterRacker, excellent help.
You peeps are brill
 
Well thats the mapping to the my docs, how do I unmap from the my docs? if I wanted to.......
 
I think you're confusing 2 different concepts here. There's nothing "mapped" in My Docs. Mapping is done at the operating system level. You point a drive letter at a shared folder somewhere. What you see in My computer is a representation of each drive, but that's not a "shortcut".

What my script is doing is creating a shortcut, which is an actual file. Disconnecting a mapped drive will remove it from My Computer but not affect any shortcuts. To get rid of the shortcut you simply delete the file.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
thanks MasterRacker, i realize that the item in my docs is not a shortcut, My script has a part that disconnects the drive, I was looking for a a way to remove the shortcut in my docsby script, rather than going to it and deleting it.
Thanks for your help on my HTA and script
 
Well, you know where you created it, so build the same FSO object and call the DeleteFile method
Code:
Set WSHShell = CreateObject("WScript.Shell")
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
strDoc = WshShell.SpecialFolders("MyDocuments")
strShortcut = strDoc & "\P-Drive.lnk"

If FSO.FileExists(strShortcut) Then
	[red][b]FSO.DeleteFile (strShortcut)[/b][/red]
End If

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Fantastic, absolutely brill!
Thanks MasterRacker. Job done
Stars all round.
 
Hi all, thought I had it sussed,
However it will only place shortcut (or rather change)shortcut in my docs if it all ready has a shortcut in it.
It seems to bypass the first request for a shortcut and never puts one in unless one exists.
Oh well. Back to the drawing board.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top