none
Add a Network Location via Script

    Question

  • I am looking to script some office standard "Network Locations" which happen to also be DFS shares. We have some software (AutoCAD & Revit) that really doesn't like accessing DFS shares directly, but will work happily with a Network Location that points at a DFS share. But I can find no automated way to roll these out, just going machine to machine manually right clicking Computer in Windows Explorer and choosing Add a Network Location. Five Network Locations, multiple clicks each, and 30 users, makes this a lot of work. Hopefully there is a PowerShell or WindowsScriptHost alternative.

    All machines are Windows 7 x64 if that makes a difference.

     

    Thanks!

    Gordon

    Wednesday, March 31, 2010 1:21 AM

Answers

  • Hi Gordon,

    I've put together a PowerShell function which will create a Network Location based on a name and a target path. It shouldn't be too hard to take this and run it remotely on all machines.

    Hope it helps,
    Chris.

    function Add-NetworkLocation
    {
        param(
            [string]$name,
            [string]$targetPath
        )
        
        # Get the basepath for network locations
        $shellApplication = New-Object -ComObject Shell.Application
        $nethoodPath = $shellApplication.Namespace(0x13).Self.Path
    
        # Only create if the local path doesn't already exist & remote path exists
        if ((Test-Path $nethoodPath) -and !(Test-Path "$nethoodPath\$name") -and (Test-Path $targetPath))
        {
            # Create the folder
            $newLinkFolder = New-Item -Name $name -Path $nethoodPath -type directory
    
            # Create the ini file
            $desktopIniContent = @"
    [.ShellClassInfo]
    CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
    Flags=2
    ConfirmFileOp=1
    "@
            $desktopIniContent | Out-File -FilePath "$nethoodPath\$name\Desktop.ini"
    
            # Create the shortcut file
            $shortcut = (New-Object -ComObject WScript.Shell).Createshortcut("$nethoodPath\$name\target.lnk")
            $shortcut.TargetPath = $targetPath
            $shortcut.IconLocation = "%SystemRoot%\system32\SHELL32.DLL, 85"
            $shortcut.Description = $targetPath
            $shortcut.WorkingDirectory = $targetPath
            $shortcut.Save()
            
            # Set attributes on the files & folders
            Set-ItemProperty "$nethoodPath\$name\Desktop.ini" -Name Attributes -Value ([IO.FileAttributes]::System -bxor [IO.FileAttributes]::Hidden)
            Set-ItemProperty "$nethoodPath\$name" -Name Attributes -Value ([IO.FileAttributes]::ReadOnly)
        }
    }
    

     

    Wednesday, April 14, 2010 11:23 AM
    Moderator