[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to map a drive to the desktop but machine specific not user?

Posted on 2007-10-11
9
Medium Priority
?
259 Views
Last Modified: 2012-06-27
The problem is I need to map a drive (shortcut) to a generic users desktop but only to specific machines in the domain. All the machines are the same build as well as hardware. All machines are XP Pro sp2 and fully patched. I had no problem with mapping by user so the GPs are working etc and I was told by user:  oBdA that you can only map to user specific accounts and not to machine specific accounts. I do believe him but maybe someone has a work around?
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Q_22886300.html
Heres the script which works fine by user&
Set objNet = WScript.CreateObject("WScript.Network")
Set WshShell = CreateObject("WScript.Shell")
DesktopPath = WshShell.SpecialFolders("Desktop")

'map network drive
objNet.MapNetworkDrive "T:", "\\Bst-main\Student\_Reception\Yr R 2007 - 2008", "True"

'create a icon on the desktop
Set link = WshShell.CreateShortcut(DesktopPath & "\Yr R 2007 - 2008.lnk")
link.Description = "User Share Drive"
link.HotKey = "CTRL+ALT+SHIFT+T"
link.IconLocation = "explorer.exe,0"
link.TargetPath = "T:"
link.WindowStyle = 3
link.Save

So many thanks for your help here guys&.(Gals)
0
Comment
Question by:AndyinJapan
  • 6
  • 3
9 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 20057630
You can put all the desired computers in a security group, then write a logon script which checks to see if the computer it's running on is a member of that group.  If so, it maps the drive.

'The Distinguished Name of the group containing the computers.
strGroup = "cn=ScriptMap,cn=Users,dc=yourdomain,dc=local"

'Determines the local computername
Set objNetwork = CreateObject("Wscript.Network")
strComputerName = objNetwork.ComputerName

Set objGroup = GetObject ("LDAP://" & objGroupList.Item(strGroup))
For Each objComputer in objGroup.Members
            If objComputer.Class = "computer" And objComputer.CN = strComputerName Then
                strGroupMembership = strGroup
            End If
        Next
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20057706
Whoops, hit Submit by accident.  Here's what the full script would look like:

'The Distinguished Name of the group containing the computers.
strGroup = "cn=ScriptMap,cn=Users,dc=yourdomain,dc=local"

'Determines the local computername
Set objNet = CreateObject("Wscript.Network")
strComputerName = objNet.ComputerName

Set objGroup = GetObject ("LDAP://" & objGroupList.Item(strGroup))
For Each objComputer in objGroup.Members
      If objComputer.Class = "computer" And objComputer.CN = strComputerName Then
            'map network drive
            objNet.MapNetworkDrive "T:", "\\Bst-main\Student\_Reception\Yr R 2007 - 2008", "True"

            'create a icon on the desktop
            Set WshShell = CreateObject("WScript.Shell")
            DesktopPath = WshShell.SpecialFolders("Desktop")
            Set link = WshShell.CreateShortcut(DesktopPath & "\Yr R 2007 - 2008.lnk")
            link.Description = "User Share Drive"
            link.HotKey = "CTRL+ALT+SHIFT+T"
            link.IconLocation = "explorer.exe,0"
            link.TargetPath = "T:"
            link.WindowStyle = 3
            link.Save
      End If
Next
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 20057806
And I found another mistake.  This should be correct:

'The Distinguished Name of the group containing the computers.
strGroup = "cn=ScriptMap,cn=Users,dc=yourdomain,dc=local"

'Determines the local computername
Set objNet = CreateObject("Wscript.Network")
strComputerName = objNet.ComputerName

Set objGroup = GetObject ("LDAP://" & strGroup)
For Each objComputer in objGroup.Members
      If objComputer.Class = "computer" And objComputer.CN = strComputerName Then
            'map network drive
            objNet.MapNetworkDrive "T:", "\\Bst-main\Student\_Reception\Yr R 2007 - 2008", "True"

            'create a icon on the desktop
            Set WshShell = CreateObject("WScript.Shell")
            DesktopPath = WshShell.SpecialFolders("Desktop")
            Set link = WshShell.CreateShortcut(DesktopPath & "\Yr R 2007 - 2008.lnk")
            link.Description = "User Share Drive"
            link.HotKey = "CTRL+ALT+SHIFT+T"
            link.IconLocation = "explorer.exe,0"
            link.TargetPath = "T:"
            link.WindowStyle = 3
            link.Save
      End If
Next
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 

Author Comment

by:AndyinJapan
ID: 20063017
Hi there Shift-3
Firstly, many thanks for your help on this one (its a bit beyond me....)
Nearly worked just on the client machine there is the following error:
Line 8
Char1
Error: Object required: 'objGroupList'
Code:800A01A8
VB Runtime error

Thanks again Andy :)
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20064843
That was a leftover from a old script of mine.  See my last reply; that revision got rid of it.
0
 

Author Comment

by:AndyinJapan
ID: 20076108
Hi there Shift-3,
Sorry...it works now but the desktop shortcut doesn't appear......any ideas my good friend?
Many thanks
Andy


'The Distinguished Name of the group containing the computers.
strGroup = "cn=ScriptMap,cn=Users,dc=bst,dc=local"

'Determines the local computername
Set objNet = CreateObject("Wscript.Network")
strComputerName = objNet.ComputerName

Set objGroup = GetObject ("LDAP://" & strGroup)
For Each objComputer in objGroup.Members
      If objComputer.Class = "computer" And objComputer.CN = strComputerName Then
            'map network drive
            objNet.MapNetworkDrive "K:", "\\Bst-main\Student\_Reception\Yr R 2007 - 2008", "True"

            'create a icon on the desktop
            Set WshShell = CreateObject("WScript.Shell")
            DesktopPath = WshShell.SpecialFolders("Desktop")
            Set link = WshShell.CreateShortcut(DesktopPath & "\Yr R 2007 - 2008.lnk")
            link.Description = "User Share Drive"
            link.HotKey = "CTRL+ALT+SHIFT+T"
            link.IconLocation = "explorer.exe,0"
            link.TargetPath = "K:"
            link.WindowStyle = 3
            link.Save
      End If
Next
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20077307
It looks good to me, but you might have to introduce a delay between mapping the drive and creating the shortcut.

Try inserting "Wscript.Sleep 5000" just before the icon-creating section.  If that works, gradually decrease the interval until you find the shortest one that works.
0
 

Author Comment

by:AndyinJapan
ID: 20098810
Hi there Shift-3
Still didn't get there after all that....any other ideas?
Thanks mate
Andy
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 20108833
It appears to be erroring out on the MapNetworkDrive line if the K: drive is already mapped.  This prevents the shortcut portion from running.

To avoid this you need to check if the drive already exists and, if it does, to unmap it.


'The Distinguished Name of the group containing the computers.
strGroup = "cn=ScriptMap,cn=Users,dc=bst,dc=local"

'Determines the local computername
Set objNet = CreateObject("Wscript.Network")
strComputerName = objNet.ComputerName

Set objGroup = GetObject ("LDAP://" & strGroup)

For Each objComputer in objGroup.Members
      If objComputer.Class = "computer" And objComputer.CN = strComputerName Then
        
                  'checks for network drive and unmaps it if it exists
                  Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
                  Set colDrives = objWMIService.ExecQuery("Select * From Win32_NetworkConnection")
                  For Each objDrive in colDrives
                        If UCase(objDrive.LocalName) = "K:" Then
                              objNet.RemoveNetworkDrive "K:", True, True
                        End If
                  Next                  
                  
            'map network drive
            objNet.MapNetworkDrive "K:", "\\nisdep02\downloads", "True"  
                  
                  'create an icon on the desktop
                  Set WshShell = CreateObject("WScript.Shell")
            DesktopPath = WshShell.SpecialFolders("Desktop")
            Set link = WshShell.CreateShortcut(DesktopPath & "\Yr R 2007 - 2008.lnk")
            link.Description = "User Share Drive"
            link.HotKey = "CTRL+ALT+SHIFT+T"
            link.IconLocation = "explorer.exe,0"
            link.TargetPath = "K:"
            link.WindowStyle = 3
            link.Save
            End If
Next
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question