Sign in with
Sign up | Sign in
Your question

login scripts and mapped drives...

Last response: in Windows 2000/NT
Share
Anonymous
August 23, 2004 8:35:02 AM

Archived from groups: microsoft.public.win2000.group_policy (More info?)

i have written a login script in VBscript that was supposed to check for the
user's required drives before trying to remap them in order to speed up the
login process. the code below shows what i was trying to do.

Set allDrives = Net.EnumNetworkDrives
For j = 0 To allDrives.Count - 1 Step 2
Select Case allDrives(j)
Case "G:"
Gmap = allDrives(j) & allDrives(j + 1)
Case "K:"
Kmap = allDrives(j) & allDrives(j + 1)
Case "M:"
Mmap = allDrives(j) & allDrives(j + 1)
Case "O:"
Omap = allDrives(j) & allDrives(j + 1)
Case "P:"
Pmap = allDrives(j) & allDrives(j + 1)
Case "Q:"
Qmap = allDrives(j) & allDrives(j + 1)
Case "R:"
Rmap = allDrives(j) & allDrives(j + 1)
End Select
Next

i use the variables above to check which drives are mapped on these letters
only remapping them if they are wrong. the problem is that when this script
runs there are no drives mapped on the system even if the user has mapped
drives in their profile.
Q. at what point to previously mapped drives reappear on the local system
during the login process ?
i am assuming that the profile stuff is applied to the computer before after
the login script runs. if this is true, how can i test the mapped drives ?
or can the order be changed, i.e. profile then scripts ?

thanks,

Jamie
Anonymous
August 23, 2004 10:58:51 PM

Archived from groups: microsoft.public.win2000.group_policy (More info?)

On Mon, 23 Aug 2004 04:35:02 -0700, jimbldoo wrote:

> i have written a login script in VBscript that was supposed to check for the
> user's required drives before trying to remap them in order to speed up the
> login process. the code below shows what i was trying to do.
> i use the variables above to check which drives are mapped on these letters
> only remapping them if they are wrong. the problem is that when this script
> runs there are no drives mapped on the system even if the user has mapped
> drives in their profile.
> Q. at what point to previously mapped drives reappear on the local system
> during the login process ?
> i am assuming that the profile stuff is applied to the computer before after
> the login script runs. if this is true, how can i test the mapped drives ?
> or can the order be changed, i.e. profile then scripts ?

The persistent drive mappings are made when the profile is loaded, before
policies applied (and therefore scripts run). I overcame this by using
the following script... Just call it as you would "net use" for each
drive mapping you want. I still have to add an option to allow non
persistent mapping.

Cheers,

Kenny.

' $Id: mapdrive.lgns,v 1.2 2004/05/31 13:15:42 kenny Exp $
' Robustly maps a network drive
' Unmaps existing mapping and removes persistent mapping
' information from the user registry hive.

Option Explicit

' We're working in the H_KEY_CURRENT_USER hive
Const HKCU = &H80000001

' Declare all variables
Dim i
Dim objNetwork, colNetworkDrives, objShell, objReg
Dim strLocalName, strRemoteName, strExistingRemoteName, strMessage
Dim bolPersistent, bolFoundExisting, bolFoundRemembered

strLocalName = UCase(Left(WScript.Arguments.Item(0), 2))
strRemoteName = WScript.Arguments.Item(1)
bolPersistent = True
bolFoundExisting = False

If Right(strLocalName, 1) <> ":" Then
ShowError "Must supply a drive letter, e.g. T:"
WScript.Quit(1)
End If

If Left(strRemoteName, 2) <> "\\" Then
ShowError "Must supply a UNC path."
WScript.Quit(1)
End If

' Get a network object
Set objNetwork = WScript.CreateObject("WScript.Network")

' First check if there's a network drive already mapped on the requested letter
Set colNetworkDrives = objNetwork.EnumNetworkDrives
If colNetworkDrives.Count > 0 Then
' Loop through the drives to look for the one we're mapping
For i = 0 To colNetworkDrives.Count-1 Step 2
'WScript.Echo "Checking Drive " & colNetworkDrives.Item(i) & " against " & strLocalName
If colNetworkDrives.Item(i) = strLocalName Then
strExistingRemoteName = colNetworkDrives.Item(i+1)
'WScript.Echo "Drive " & strLocalName & " already mapped to " & strExistingRemoteName & ". Forcing disconnect ..."
' Force disconnect and remove from registry
objNetwork.RemoveNetworkDrive strLocalName, True, True
i=colNetworkDrives.Count-1
bolFoundExisting = True
End If
Next
End If

' Now check if there's a remembered location (persistent mapping)
' If we find one, simply delete the registry key associated with it.
If bolFoundExisting <> True Then
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
objReg.GetStringValue HKCU, "Network\" & Left(strLocalName, 1), "RemotePath", strExistingRemoteName
If strExistingRemoteName <> "" Then
'WScript.Echo "Found remembered drive location for " & strLocalName & " to " & strExistingRemotename
objReg.DeleteKey HKCU, "Network\" & Left(strLocalName, 1)
Set objReg = Nothing
bolFoundRemembered = True
End If
End If


'WScript.Echo "Mapping the drive..."
Err.Clear
On Error Resume Next
objNetwork.MapNetworkDrive strLocalName, strRemoteName, bolPersistent
If Err <> 0 Then
Select Case Err.Number
Case -2147023694
On Error Goto 0
'WScript.Echo "Persistent drive mapped."
objNetwork.RemoveNetworkDrive strLocalName, True, True
objNetwork.MapNetworkDrive strLocalName, strRemoteName, bolPersistent
Case -2147024829
On Error GoTo 0
'WScript.Echo "Unavailable location."
Case Else
On Error GoTo 0
End Select
Err.Clear
ShowError "Failed to map network drive " & strLocalName & " to " & strRemoteName
If bolFoundExisting = True Then
On Error Resume Next
' Attempt to remap the orginal location
objNetwork.MapNetworkDrive strLocalName, strExistingRemoteName, bolPersistent
On Error GoTo 0
End If
If bolFoundRemembered = True Then
On Error Resume Next
' Attempt to temporarily remap the orginal location
' to get the persistent information into the registry.
objNetwork.MapNetworkDrive strLocalName, strExistingRemoteName, bolPersistent
' Delete the mapping, but leaving it persistent
objNetwork.RemoveNetworkDrive strLocalName, True
On Error GoTo 0
End If
End If

Set objNetwork = Nothing
'WScript.Echo "Done"
WScript.Quit(0)

Sub ShowError(strMessage)
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Popup strMessage, 0, "Network Drives", vbExclamation + vbOKOnly
Set objShell = Nothing
End Sub
August 24, 2004 10:02:40 AM

Archived from groups: microsoft.public.win2000.group_policy (More info?)

The login scrips that we use here go something like:
net use n: /delete
net use n: \\server\share

.... this way if the user has an old mapping from when we
changed way back...hey they might have a laptop, they'd
have the correct mapping.

HTH,

Ken
>-----Original Message-----
>On Mon, 23 Aug 2004 04:35:02 -0700, jimbldoo wrote:
>
>> i have written a login script in VBscript that was
supposed to check for the
>> user's required drives before trying to remap them in
order to speed up the
>> login process. the code below shows what i was trying
to do.
>> i use the variables above to check which drives are
mapped on these letters
>> only remapping them if they are wrong. the problem is
that when this script
>> runs there are no drives mapped on the system even if
the user has mapped
>> drives in their profile.
>> Q. at what point to previously mapped drives reappear
on the local system
>> during the login process ?
>> i am assuming that the profile stuff is applied to the
computer before after
>> the login script runs. if this is true, how can i
test the mapped drives ?
>> or can the order be changed, i.e. profile then
scripts ?
>
>The persistent drive mappings are made when the profile
is loaded, before
>policies applied (and therefore scripts run). I
overcame this by using
>the following script... Just call it as you would "net
use" for each
>drive mapping you want. I still have to add an option
to allow non
>persistent mapping.
>
>Cheers,
>
>Kenny.
>
>' $Id: mapdrive.lgns,v 1.2 2004/05/31 13:15:42 kenny Exp
$
>' Robustly maps a network drive
>' Unmaps existing mapping and removes persistent mapping
>' information from the user registry hive.
>
>Option Explicit
>
>' We're working in the H_KEY_CURRENT_USER hive
>Const HKCU = &H80000001
>
>' Declare all variables
>Dim i
>Dim objNetwork, colNetworkDrives, objShell, objReg
>Dim strLocalName, strRemoteName, strExistingRemoteName,
strMessage
>Dim bolPersistent, bolFoundExisting, bolFoundRemembered
>
>strLocalName = UCase(Left(WScript.Arguments.Item(0), 2))
>strRemoteName = WScript.Arguments.Item(1)
>bolPersistent = True
>bolFoundExisting = False
>
>If Right(strLocalName, 1) <> ":" Then
> ShowError "Must supply a drive letter, e.g. T:"
> WScript.Quit(1)
>End If
>
>If Left(strRemoteName, 2) <> "\\" Then
> ShowError "Must supply a UNC path."
> WScript.Quit(1)
>End If
>
>' Get a network object
>Set objNetwork = WScript.CreateObject("WScript.Network")
>
>' First check if there's a network drive already mapped
on the requested letter
>Set colNetworkDrives = objNetwork.EnumNetworkDrives
>If colNetworkDrives.Count > 0 Then
> ' Loop through the drives to look for the one we're
mapping
> For i = 0 To colNetworkDrives.Count-1 Step 2
> 'WScript.Echo "Checking Drive " &
colNetworkDrives.Item(i) & " against " & strLocalName
> If colNetworkDrives.Item(i) = strLocalName Then
> strExistingRemoteName = colNetworkDrives.Item(i+1)
> 'WScript.Echo "Drive " & strLocalName & " already
mapped to " & strExistingRemoteName & ". Forcing
disconnect ..."
> ' Force disconnect and remove from registry
> objNetwork.RemoveNetworkDrive strLocalName, True,
True
> i=colNetworkDrives.Count-1
> bolFoundExisting = True
> End If
> Next
>End If
>
>' Now check if there's a remembered location (persistent
mapping)
>' If we find one, simply delete the registry key
associated with it.
>If bolFoundExisting <> True Then
> Set objReg = GetObject("winmgmts:
{impersonationLevel=impersonate}!
\\.\root\default:StdRegProv")
> objReg.GetStringValue HKCU, "Network\" & Left
(strLocalName, 1), "RemotePath", strExistingRemoteName
> If strExistingRemoteName <> "" Then
> 'WScript.Echo "Found remembered drive location for "
& strLocalName & " to " & strExistingRemotename
> objReg.DeleteKey HKCU, "Network\" & Left
(strLocalName, 1)
> Set objReg = Nothing
> bolFoundRemembered = True
> End If
>End If
>
>
>'WScript.Echo "Mapping the drive..."
>Err.Clear
>On Error Resume Next
>objNetwork.MapNetworkDrive strLocalName, strRemoteName,
bolPersistent
>If Err <> 0 Then
> Select Case Err.Number
> Case -2147023694
> On Error Goto 0
> 'WScript.Echo "Persistent drive mapped."
> objNetwork.RemoveNetworkDrive strLocalName, True,
True
> objNetwork.MapNetworkDrive strLocalName,
strRemoteName, bolPersistent
> Case -2147024829
> On Error GoTo 0
> 'WScript.Echo "Unavailable location."
> Case Else
> On Error GoTo 0
> End Select
> Err.Clear
> ShowError "Failed to map network drive " &
strLocalName & " to " & strRemoteName
> If bolFoundExisting = True Then
> On Error Resume Next
> ' Attempt to remap the orginal location
> objNetwork.MapNetworkDrive strLocalName,
strExistingRemoteName, bolPersistent
> On Error GoTo 0
> End If
> If bolFoundRemembered = True Then
> On Error Resume Next
> ' Attempt to temporarily remap the orginal location
> ' to get the persistent information into the
registry.
> objNetwork.MapNetworkDrive strLocalName,
strExistingRemoteName, bolPersistent
> ' Delete the mapping, but leaving it persistent
> objNetwork.RemoveNetworkDrive strLocalName, True
> On Error GoTo 0
> End If
>End If
>
>Set objNetwork = Nothing
>'WScript.Echo "Done"
>WScript.Quit(0)
>
>Sub ShowError(strMessage)
> Set objShell = WScript.CreateObject("WScript.Shell")
> objShell.Popup strMessage, 0, "Network Drives",
vbExclamation + vbOKOnly
> Set objShell = Nothing
>End Sub
>.
>
Anonymous
September 6, 2004 5:09:07 AM

Archived from groups: microsoft.public.win2000.group_policy (More info?)

hi kenneth
i thought the same as your statement below. it dosent seem to work this way
though.
> The persistent drive mappings are made when the profile is loaded, before
> policies applied (and therefore scripts run).

the problem is that when this statement

Set colNetworkDrives = objNetwork.EnumNetworkDrives

is executed in my sccript, no mapped drives seem to exist and the variable i
use to hold the collection is empty. because of this all drives are remapped
anyway.

jim
!