• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Wscript Login works when executed from Client (XP), but when tested on DC (Server 2008), fails using scripting.FileSystemObject.

Using Login script saved in %SYSVOL%\domain\scripts; I do not understand How to Create SHR folder\sub-folder\file on DC when login from client PC. The SHR folder\sub-folder\file is created on the client PC, causing a MAP drive, e.g., objNetwork.MapNetworkDrive method to fail. The script executes successfully on the client, but FAILS (when mapping the drive) on the DC because the SHR is not built on the DC.

Statement line 215 is original point of failure, e.g. routine MapDrive will map drive "L:", but folder\sub-folder\file doesn't exist on DC resulting in error: Line 290 char 5; "The Network Path was not found" Error 0x80070035.

How do I cause the following to associate the DC as object target?
        Set objFSO = CreateObject("Scripting.FileSystemObject")

Sample Script follows:
101 '
102 ' Do Action if member of "ELS-Students"
103 If MemberOf("ELS-Students","USER") Then
104 Call AddToLog(strUserName, " - Mapping Students Printers and SHRs")
105 objNetwork.AddWindowsPrinterConnection "\\ELCSH-S1\ELS-ADM-PRT"
106 objNetwork.AddWindowsPrinterConnection "\\ELCSH-S1\ELS-PRT-RM160"
107 If Not MemberOf("ELS-Teachers","USER") Then
108 'debug Wscript.echo "Setting Students Printer Default"
109 objNetwork.SetDefaultPrinter "\\ELCSH-S1\ELS-PRT-RM160"
110 End If
111 '
112 '**********************************************************************************
113 '
114 '*********[ Create/Map Student LAB Folder ]**********
115 '************ Assumes TGT Directory on ELCSH-S1 *************
116 d = Now() ' Get Current Date & Time
117 strDteTime = FormatDateTime(d,0)
118
119 strSvrDrv = "E:" ' ELCSH-S1 SHR Volume
120 strSvrName = "\\ELCSH-S1"
121 strDirectory = "\ELS_Lab_SHR"
122 strSavDir = strDirectory
123 strDirectory = strSvrDrv & StrDirectory
124 strSubDir = "\" & strUserName
125 strFile = "\_MyLabFolder.txt"
126 strText = "Note: This Folder is used for " & strUserName & "'s" & _
127 " individual Lab Exercises" & vbNewLine & _
128 " File Created: " & strDteTime
129
130 Call AddToLog(strSvrDrv, " - Creating Student SHR: " & strDirectory)
131 '*********[ Check if FOLDER Exists ]*****************
132 '
133 If objFSO.FolderExists(strDirectory) Then
134 Set objFolder = objFSO.GetFolder(strDirectory)
135 Else
136 Set objFolder = objFSO.CreateFolder(strDirectory)
137 End If
138
139 If err.number = vbEmpty then
140 Call AddToLog(strDirectory, " - SHR Folder OK")
141 ' Set objShell = CreateObject("WScript.Shell")
142 'debug objShell.run ("Explorer" &" " & strDirectory & "\" )
143 Else
144 WScript.echo "ELCSH VBScript Error: " & err.number
145 End If
146 '
147 '*********[ Check if Sub-FOLDER Exists ]*****************
148 '
149 If objFSO.FolderExists(strDirectory & StrSubDir) Then
150 Set objFolder = objFSO.GetFolder(strDirectory & strSubDir)
151 'debug WScript.Echo strDirectory & strSubDir & " already created "
152 Else
153 Set objFolder = objFSO.CreateFolder(strDirectory & strSubDir)
154 'debug WScript.Echo "Just created " & strDirectory & strSubDir
155 End If
156
157 If err.number = vbEmpty then
158 Call AddToLog(strDirectory & strSubDir, " - SHR Sub-Folder OK")
159 ' Set objShell = CreateObject("WScript.Shell")
160 'debug objShell.run ("Explorer" &" " & strDirectory & strSubDir & "\" )
161 Else
162 WScript.echo "ELCSH VBScript Error: " & err.number
163 End If
164 '
165 '*********[ Check if FILE Exists ]*****************
166 '
167 If objFSO.FileExists(strDirectory & strSubDir & strFile) Then
168 Set objFolder = objFSO.GetFolder(strDirectory)
169 Else
170 Set objFile = objFSO.CreateTextFile(strDirectory & strSubDir & strFile)
171 'debug Wscript.Echo "Just created " & strDirectory & strSubDir & strFile
172 End If
173
174 set objFolder = nothing
175 set objFile = nothing
176
177 If err.number = vbEmpty then
178 Call AddToLog(strDirectory & strSubDir & strFile, " - SHR Sub-Folder\File
OK")
179 ' Set objShell = CreateObject("WScript.Shell")
180 'debug objShell.run ("Explorer" & " " & strDirectory & strSubDir & "\" )
181 Else
182 WScript.echo "ELCSH VBScript Error: " & err.number
183 End If
184 '****************[ Write Text to Created File ]************
185 ' OpenTextFile Method needs a Const value
186 ' ForAppending = 8 ForReading = 1, ForWriting = 2
187 Const ForWriting = 2
188
189 Set objTextFile = objFSO.OpenTextFile _
190 (strDirectory & strSubDir & strFile, ForWriting, True)
191
192 ' Writes strText every time you run this VBScript
193 objTextFile.WriteLine(strText)
194 objTextFile.Close
195
196 ' Bonus or cosmetic section to launch explorer to check file
197 If err.number = vbEmpty then
198 Call AddToLog(strDirectory & strSubDir & strFile, " - SHR Sub-Folder\File
Loaded")
199 ' Set objShell = CreateObject("WScript.Shell")
200 'debug objShell.run ("Explorer" &" " & strDirectory & "\" )
201 Else WScript.echo "ELCSH VBScript Error: " & err.number
202 End If
203
204 '**********[ Map Students Folder ]*******************
205
206 strDrv = "S:" ' Identify Network Drive to be Mapped
207 MapDrive strDrv, strSvrName, "\Students"
208
209 '**********[ Map Students LAB Folder ]*******************
210
211 strDrv = "L:" ' Identify Network Drive to be Mapped
212 strSavDir = strSavDir & strSubDir
213 Call AddToLog(strDrv, " - Map Lab: " & strSavDir)
214
215 MapDrive strDrv, strSvrName, strSavDir
216 'debug Wscript.echo strUserName & " - Students Folders have been Mapped"
217 Call AddToLog(strSavDir, " - Students Folders have been Mapped")
218 End If
219
220 Wscript.Quit
221
222 '
223 '**********************************************************************************
224 '**********[ Function: MemberOF ]*******************
225 ' Function returns true if given context (user/computer) is member of given group
226 ' MemberOf
227 '**********************************************************************************
228 Function MemberOf(groupName, className)
229 DIM ADSysInfo, obj
230 DIM strGroups
231
232 Set ADSysInfo = CreateObject("ADsystemInfo")
233 '
234 If uCase(className) = "COMPUTER" Then
235 set obj = GetObject("LDAP://" & ADSysInfo.ComputerName)
236 Else
237 set obj = GetObject("LDAP://" & ADSysInfo.UserName)
238
239 End If
240 '
241 strGroups = UCase(Join(obj.MemberOf))
242 If InStr(strGroups, uCase(groupName)) Then
243 MemberOf = True
244 Else
245 MemberOf = False
246 End IF
247 End Function
0
wimbre042
Asked:
wimbre042
  • 4
  • 4
1 Solution
 
Robberbaron (robr)Commented:
any share needs to be setup on the "server". in your case the DC , as clients dont have permissions to create, only connect.

And you cant Map a drive to a folder, only to a Share  eg \\ELCSH-S1\ELS_Lab_SHR

but have you tried the subst dos command ?  

subst L: \\ELCSH-S1\ELS_Lab_SHR\subfolder

0
 
Robberbaron (robr)Commented:
see Richard Mueller example http://www.vistax64.com/vb-script/238794-logon-vbscript-mapping-drives.html  for login script usage.
0
 
wimbre042Author Commented:
The SHR has been already created on the Server with Security Rights consistent with the user at the client. Mapping the folders are not the issue. It is how to create a SHR sub-folder on the server using the logon name of the user and subsequently creating a file in that sub-folder. The subsequent mapping is to bind the user to the created sub-folder. The sub-folder should inherit the rights from the SHR.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wimbre042Author Commented:
Maybe, what I am attempting to do is not possible. e.g., your 1st comment: "as clients dont have permissions to create, only connect."

What I would like to do on the server at logon by a specific Group of Users is create a sub-folder within the root SHR "\\ELCSH-S1\ELS_Lab_SHR" using the user's logon name if the sub-folder does not exist. This sub-folder will subsequently be mapped as the playpen for the user to store files.

Is there any way to accomplish this? From a support stand point, the users will access the server from a pool of laptops, many times they will not use the same laptop from day to day, thus their work is desired to be saved on the Server.
0
 
Robberbaron (robr)Commented:
Ok, that makes more sense. I'll do some testing as it should work as you have tried.
0
 
wimbre042Author Commented:
Thanks, I appreciate you Help
0
 
Robberbaron (robr)Commented:
this code works on my home network.

I changed a number of items and some of the logic needed update.

And it is only a subset of your code.

Before your testing, remove my test settings.  i used X drive letter and my network details.

C:\ee>cscript q_27393791.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

14/Oct/2011 8:39:41 PM Mapping to Student SHR: \\asus_study\samba2
14/Oct/2011 8:39:41 PM -- OK

14/Oct/2011 8:39:41 PM  - Creating Student folder: \\asus_study\samba2\test123
14/Oct/2011 8:39:41 PM  - folder exists
14/Oct/2011 8:39:41 PM -- OK

14/Oct/2011 8:39:41 PM Map user drive  X:=\\asus_study\samba2\test123

14/Oct/2011 8:39:42 PM  - SHR Sub-Folder\FileOK : X:\_MyLabFolder.txt

14/Oct/2011 8:39:42 PM  - SHR Sub-Folder\File Loaded : X:\_MyLabFolder.txt

C:\ee>dir x:
 Volume in drive X is samba2
 Volume Serial Number is B8DA-0245

 Directory of X:\

14/Oct/2011  07:36 PM    <DIR>          .
17/Jul/2011  10:36 PM    <DIR>          ..
14/Oct/2011  08:39 PM               139 _MyLabFolder.txt
               1 File(s)            139 bytes
               2 Dir(s)     727,203,840 bytes free
'**********************************************************************************
'updated by Robberbaron @ EE.
'*********[ Create/Map Student LAB Folder ]**********
Dim objNetwork, objFSO
Set objNetwork = WScript.CreateObject("WScript.Network")
set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

'************ Assumes TGT Directory on ELCSH-S1 *************
d = Now() ' Get Current Date & Time
strDteTime = FormatDateTime(d,0)

strLocalDrv = "E:" ' ELCSH-S1 SHR Volume
strSvrName = "\\ELCSH-S1"
strDirectory = "\ELS_Lab_SHR"

'TESTING
strLocalDrv = "X:" ' ELCSH-S1 SHR Volume
strSvrName = "\\asus_study"
strDirectory = "\samba2"
strUserName = "test123"
'////end testing

strMapUNC = strSvrName & StrDirectory
strSavDir = strMapUNC & "\" & strUserName
strFile = "\_MyLabFolder.txt"


strText = "Note: This Folder is used for " & strUserName & "'s" & _
           " individual Lab Exercises" & vbNewLine & _
           "---File Created: " & strDteTime & "---"
          

          
           
'try mapping
Call AddToLog("Mapping to Student SHR: " & strMapUNC)

on error resume next
'clear any old connection
objNetwork.RemoveNetworkDrive strLocalDrv
err.clear
objNetwork.MapNetworkDrive strLocalDrv, strMapUNC, true

if err.number > 0 then
    Call AddToLog("-- FAILED " & vbCRLf)
    WScript.echo "ELCSH VBScript Error mapping network folder: " & err.number 
  else
    Call AddToLog("-- OK " & vbCRLf)

    Call AddToLog(" - Creating Student folder: " & strSavDir)
    '*********[ Check if FOLDER Exists ]*****************
    '
    If objFSO.FolderExists(strSavDir) Then
        Set objFolder = objFSO.GetFolder( strSavDir)
        if err.number > 0 then
            Call AddToLog(" - error finding folder")
          else
              Call AddToLog(" - folder exists")
        end if
      Else
        Set objFolder = objFSO.CreateFolder(strSavDir)
        if err.number > 0 then
            Call AddToLog(" - error creating folder")
            wscript.quit
          else
              Call AddToLog(" - folder created")
        end if        
        
    End If

    If err.number > 0 then
        Call AddToLog("-- FAILED " & vbCRLf)
        WScript.echo "ELCSH VBScript Error mapping network folder: " & err.number 
      else
        Call AddToLog("-- OK " & vbCRLf) 
              
        'remove the mapped network share
        objNetwork.RemoveNetworkDrive strLocalDrv
        
        'map to the user folder using subst              
        Call AddToLog("Map user drive  " & strLocalDrv & "=" & strSavDir & vbCRLf)
        call Subst (strLocalDrv,strSavDir)

        '*********[ Check if FILE Exists ]*****************
        '
        If objFSO.FileExists(strSavDir & strFile) Then
            Call AddToLog(" - SHR Sub-Folder\FileOK : " & strLocalDrv & strFile  & vbcrlf)
        Else
            Call AddToLog(" - Creating File  " & strLocalDrv & strFile)
            Set objFile = objFSO.CreateTextFile(strLocalDrv & strFile)
            If err.number > 0 then
                Call AddToLog(" - FAILED" & vbcrlf)
              else
                Call AddToLog(" -- OK" & vbcrlf)
            end if
            objFile.Close
        End If

        set objFolder = nothing
        set objFile = nothing

        '****************[ Write Text to Created File ]************
        ' OpenTextFile Method needs a Const value
        ' ForAppending = 8 ForReading = 1, ForWriting = 2
        Const ForWriting = 2
        err.clear
        Set objTextFile = objFSO.OpenTextFile _
                  (strLocalDrv & strFile, ForWriting, True)

        ' Writes strText every time you run this VBScript
          d = Now() ' Get Current Date & Time
          strDteTime = FormatDateTime(d,0)
        objTextFile.WriteLine("-----" & strDteTime & "-----")
        objTextFile.WriteLine(strText)
        objTextFile.Close
        
        If err.number = 0 then
            Call AddToLog(" - SHR Sub-Folder\File Loaded : " & strLocalDrv & strFile)

        Else 
        
            Call AddToLog(" - FAILED to append to logfile : " & strLocalDrv & strFile)
            WScript.echo "ELCSH VBScript Error: " & err.number
        End If

    end if
End if

Wscript.Quit

'--------------------------------------------
Sub Subst(strDrive , strPath)

  Dim objShell, strCmd
  
  Set objShell = CreateObject("Wscript.Shell")

  strCmd = "%comspec% /c subst " & strDrive & " " & strPath
  'wscript.echo strCmd
  objShell.Run strCmd,0,true
end sub

'--------------------------------------------
Sub AddtoLog ( part1)
  d = Now() ' Get Current Date & Time
  strDteTime = FormatDateTime(d,0)
    wscript.echo strDteTime,Part1 
End sub

Open in new window

0
 
wimbre042Author Commented:
RobberBaron
Many Kudos to you. This is worth 1,000 points if I was Grading. I have tested extensively and your solution WORKS

wimbre
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now