sam_7_h
sam_7_h

Reputation: 800

Add new network location, not map network drive

Let's say I have 100 users and I need to add several network locations for each user. They cannot be network drives (e.g. Q:) as some users already have more than 26 drives mapped.

I was hoping to do this using either a batch file or a VB script. I've managed to get it working using a VB script by adding network shortcuts but this isn't the solution the users need.

I've been searching and can't find anything related specifically to Network Locations.

I'm open to trying other methods.

Upvotes: 5

Views: 11814

Answers (1)

MC ND
MC ND

Reputation: 70923

EDITED to properly answer the question; the original answer, that creates a shortcut in Network Locations, is kept at the end.

After some testing, a network location is a read-only folder located in the %AppData%\Microsoft\Windows\Network Shortcuts folder, with two files inside: desktop.ini with a precise content (see in code) and a target.lnk shortcut to the target.

Option Explicit

Function CreateNetworkLocation( networkLocationName, networkLocationTarget )
    Const ssfNETHOOD  = &H13&
    Const fsATTRIBUTES_READONLY = 1
    Const fsATTRIBUTES_HIDDEN = 2
    Const fsATTRIBUTES_SYSTEM = 4

    CreateNetworkLocation = False 

    ' Instantiate needed components
    Dim fso, shell, shellApplication
        Set fso =               WScript.CreateObject("Scripting.FileSystemObject")
        Set shell =             WScript.CreateObject("WScript.Shell")
        Set shellApplication =  WScript.CreateObject("Shell.Application")

    ' Locate where NetworkLocations are stored
    Dim nethoodFolderPath, networkLocationFolder, networkLocationFolderPath
        nethoodFolderPath = shellApplication.Namespace( ssfNETHOOD ).Self.Path

    ' Create the folder for our NetworkLocation and set its attributes
        networkLocationFolderPath = fso.BuildPath( nethoodFolderPath, networkLocationName )
        If fso.FolderExists( networkLocationFolderPath ) Then 
            Exit Function 
        End If 
        Set networkLocationFolder = fso.CreateFolder( networkLocationFolderPath )
        networkLocationFolder.Attributes = fsATTRIBUTES_READONLY

    ' Write the desktop.ini inside our NetworkLocation folder and change its attributes    
    Dim desktopINIFilePath
        desktopINIFilePath = fso.BuildPath( networkLocationFolderPath, "desktop.ini" )
        With fso.CreateTextFile(desktopINIFilePath)
            .Write  "[.ShellClassInfo]" & vbCrlf & _ 
                    "CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}" & vbCrlf & _ 
                    "Flags=2" & vbCrlf
            .Close
        End With 
        With fso.GetFile( desktopINIFilePath )
            .Attributes = fsATTRIBUTES_HIDDEN + fsATTRIBUTES_SYSTEM
        End With 

    ' Create the shortcut to the target of our NetworkLocation
    Dim targetLink
        targetLink = fso.BuildPath( networkLocationFolderPath, "target.lnk" )
        With shell.CreateShortcut( targetLink )
            .TargetPath = networkLocationTarget
            .Save
        End With        

    ' Done
        CreateNetworkLocation = True 

End Function

CreateNetworkLocation "Tests", "\\192.168.1.2\c$"

Tested in Windows 7.


Original answer - Just in case someone finds it useful.

All you need to do is to create a shortcut in the folder:

%AppData%\Microsoft\Windows\Network Shortcuts

Just a VBScript sample (as indicated in the question, not sure if the tags points to another needs):

Option Explicit

Const ssfNETHOOD  = &H13&

Dim fso, shell, shellApplication

    Set fso =               WScript.CreateObject("Scripting.FileSystemObject")
    Set shell =             WScript.CreateObject("WSCript.Shell")
    Set shellApplication =  WScript.CreateObject("Shell.Application")

Dim networkLocationsFolder
    networkLocationsFolder = shellApplication.Namespace( ssfNETHOOD ).Self.Path

    With shell.CreateShortcut(fso.BuildPath( networkLocationsFolder, "Test PC.lnk" ))
        .TargetPath = "\\192.168.1.10\c$"
        .WindowStyle = 1
        .IconLocation = "shell32.dll, 9"
        .Description = "Access to Test computer drive"
        .WorkingDirectory = "\\192.168.1.10\c$"
        .Save
    End With

Upvotes: 8

Related Questions