自动脚本添加新网络位置NOT映射网络驱动器

时间:2015-06-18 09:55:26

标签: vb.net excel-vba batch-file networking drive

情境: 假设我有100个用户,我需要为每个用户添加几个网络位置。它们不能是网络驱动器,因为一些用户已经映射了超过26个驱动器。

我希望使用批处理文件或VB脚本来执行此操作。我通过添加网络快捷方式设法使用VB脚本使其工作,但这不是用户需要的解决方案。我也通过映射网络驱动器找到了这样做的方法,但就像我说用户已经有大量的驱动器映射,因此也不能使用这种方法。

我已经搜索了很长一段时间,无法找到与网络位置相关的任何内容。在此先感谢您的帮助。我也愿意尝试新的方法来实现这个目标,但是在一分钟之内就是出于想法。

1 个答案:

答案 0 :(得分:1)

已编辑以正确回答此问题。在网络位置中创建快捷方式的原始答案最终会保留。

经过一些测试后,网络位置是位于%AppData%\Microsoft\Windows\Network Shortcuts文件夹中的ReadOnly文件夹,里面有两个文件:desktop.ini,内容精确(参见代码) )和目标的target.lnk快捷方式。

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$"

在Windows 7中测试。

原始回答 - 万一有人发现它有用

您只需在适当的文件夹中创建快捷方式:

%AppData%\Microsoft\Windows\Network Shortcuts

只是一个VBScript示例(如问题所示,不确定标签是否指向其他需求)

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