February 23, 2012

Updated - Automate Creating SharePoint and Insight Network Location Shortcuts

Several months ago I posted a script for automating the creation of SharePoint and Insight Network Location shortcuts.  Unfortunately that script only accounted for http addresses and failed to correctly implement https.  Thanks to a Chris who commented on the original post about the oversight with https addresses.  So without much ado, here is an updated version of that script which takes into account both http and https addresses.

You can download the script directly from here.

Or simply copy and paste the code below into a text file and save it with a .vbs file extension.


' CreateSharePointNetworkLocations
' Copyright (C) 2011, David C. Merritt, david.c.merritt@siemens.com
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>.
'
' ---------------------------------------------------------------------
'
' A script to automate the creation of SharePoint network location
' shortcuts on Windows 7 clients.
'
' This script is particularly useful for those customers running Solid
' Edge Insight needing a way to automate the creation of the Insight
' required Network location shortcuts i.e. user logon scripts etc.
'
' To use edit, add and remove the array members ShortcutNames and
' ShortcutPaths.  Adjust the array counter accordingly to match the
' number of network locations to be created.
'
' This is a simple script and does minimal error checking. It should be
' fleshed out to best suit your specific needs and environment.
'
' ---------------------------------------------------------------------
'
' 11/01/2011  merritt  initial release
' 02/23/2012  merritt  added support for https
' 02/27/2012  merritt  fixed http shortcuts broken by the https fix
' 02/27/2012  merritt  fixed port numbers in addresses
'

' ---------------------------------------------------------------------
' edit, add and remove your network locations below here
' ---------------------------------------------------------------------
dim ShortcutNames(8), ShortcutPaths(8)
ShortcutNames(0) = "hsvnt416z06 - PreReleased"
ShortcutPaths(0) = "http://hsvnt416z06/Pre/PreDL"
ShortcutNames(1) = "hsvnt416z06 - Released"
ShortcutPaths(1) = "http://hsvnt416z06/Rel/RelDL"
ShortcutNames(2) = "hsvnt416z06 - Obsolete"
ShortcutPaths(2) = "http://hsvnt416z06/Obs/ObsDL"
ShortcutNames(3) = "hsvnt416z01 - PreReleased"
ShortcutPaths(3) = "http://hsvnt416z01:16133/Pre"
ShortcutNames(4) = "hsvnt416z01 - Released"
ShortcutPaths(4) = "http://hsvnt416z01:16133/Rel"
ShortcutNames(5) = "hsvnt416z01 - Obsolete"
ShortcutPaths(5) = "http://hsvnt416z01:16133/Obs"
ShortcutNames(6) = "SSL hsvnt416z01 - PreReleased"
ShortcutPaths(6) = "https://hsvnt416z01.net.plm.eds.com/Pre/PreDL"
ShortcutNames(7) = "SSL hsvnt416z01 - Released"
ShortcutPaths(7) = "https://hsvnt416z01.net.plm.eds.com/Rel/RelDL"
ShortcutNames(8) = "SSL hsvnt416z01 - Obsolete"
ShortcutPaths(8) = "https://hsvnt416z01.net.plm.eds.com/Obs/ObsDL"
' ---------------------------------------------------------------------
' edit, add and remove your network locations above here
' ---------------------------------------------------------------------

' create network place shortcut for each location specified
intCounter = 0
For Each strShortcutName In ShortcutNames
    CreateNetworkPlace strShortcutName, ShortcutPaths(intCounter)
     intCounter = intCounter + 1
Next
wscript.echo "Network place shortcuts created"
WScript.Quit

' the subroutine that does all the work
Sub CreateNetworkPlace(strShortcutName, strShortcutPath)
    ' change our shortcut path to the Win 7 webdav format
    If InStr(UCase(strShortcutPath), UCase("https")) = 0 Then
        strShortcutMod = Replace(strShortcutPath, "http://", "\\")
        strShortcutMod = Replace(strShortcutMod, "/", "\DavWWWRoot\", 1, 1)
    Else
        strShortcutMod = Replace(strShortcutPath, "https://", "\\")
        strShortcutMod = Replace(strShortcutMod, "/", "@SSL\DavWWWRoot\", 1, 1)
    End If
    strShortcutMod = Replace(strShortcutMod, "/", "\")
    strShortcutMod = Replace(strShortcutMod, ":", "@")
   
    ' determine on the OS where to create the network place shortcut
    Const NETHOOD = &H13&
    Set objWSHShell = CreateObject("Wscript.Shell")
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(NETHOOD)
    Set objFolderItem = objFolder.Self
    strNetHood = objFolderItem.Path

    ' create the network place shortcut folder
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strShortcutFolder = strNetHood & "\" & strShortcutName
    If objFSO.FolderExists(strShortcutFolder) Then
        wscript.echo strShortcutFolder & " already exists"
    Else
        Set objFolder = objFSO.CreateFolder(strShortcutFolder)
       
        ' create the Desktop.ini file under the network place shortcut folder
        strDesktopIni = strShortcutFolder & "\Desktop.ini"
        If Not objFSO.FileExists(strDesktopIni) Then
            set fText = objFSO.OpenTextFile(strDesktopIni, 2, True)
            fText.WriteLine "[.ShellClassInfo]"
            fText.WriteLine "CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}"
            fText.WriteLine "Flags=2"
            fText.Close
        End If

        ' set Desktop.ini with file attributes system & hidden
        Set fFile = objFSO.GetFile(strDesktopIni)
        fFile.Attributes = 6

        ' set network place shortcut folder as read-only
        Set fFolder = objFSO.GetFolder(strShortcutFolder)
        fFolder.Attributes = 1

        ' create the shortcut file target.lnk under the network place shortcut folder
        Set objShortcut = objWSHShell.CreateShortcut(strShortcutFolder & "\target.lnk")
        objShortcut.TargetPath = strShortcutMod
        objShortcut.Description = strShortcutPath
        objShortcut.Save
    End If
End Sub

Once you have the code, locate the section where indicated and change, add and remove your SharePoint URL network locations and the names for the shortcuts as necessary.


Save the script and run it.  Voilà!  Automated SharePoint Network Location shortcuts.

 
I can’t guarantee I’ll be able to incorporate any requests but if you would like to see certain functionality incorporated into this script it never hurts to ask :-)

As always YMMV but please feel free to give me feedback in the comments sections.  Thanks.

Update 02/27:  Doh!  When adding support for https addresses I accidentally broke http addresses and port numbers.  Thankfully  the error was caught and I have updated the script to fix this.  Now both http and https addresses along with port numbers work as expected.  My apologies for the mistake :-(

9 comments:

  1. Thank you so much! This was extrememly helpful. I have been tearing my hair out trying to map sharepoint folders through group policy and getting the folders to show up correctly under the computer node in Windows Explorer. Much appreciated!

    ReplyDelete
    Replies
    1. You are welcome and thank you for the feedback.

      -Dave

      Delete
  2. Thanks a huge bunch for sharing this code!

    I tested it in Windows 8 (x64) and it seemed to have worked quite well.

    When two colleagues tested it on Win7 x64, they both got 'path not found errors on this line:

    Set objFolder = objFSO.CreateFolder(strShortcutFolder)

    Could it be because it's 64-bit? (Somehow I doubt it...)

    ReplyDelete
    Replies
    1. Robin,

      Thanks for reading and your feedback.

      FYI, I originally developed this script on Windows 7 64-bit and it worked (and still continues to work without issue) for me on my Windows 7 64-bit systems.

      Have your colleagues changed the default location for the user profiles or network location folders on their workstations? Perhaps upgraded the workstation to Windows 7 instead of a clean install? This script uses the defined nethood location which is typically “C:\Users\\AppData\Roaming\Microsoft\Windows\Network Shortcuts”. If some reason the shortcuts cannot be created under this location then you would typically see the “path not found errors”.

      Check the registry key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\NetHood. Can your colleagues manually create files in this location i.e. from a command prompt?

      Regards,

      -Dave

      Delete
  3. What about ftp addresses? I tried modifying the script to handle them with no success.

    ReplyDelete
    Replies
    1. Donat,

      Thanks for stopping by!

      In regards to your question, this blog post/script was only ever intended for SharePoint http(s) addresses.

      With that said, it should be a simple matter of identifying what the Network Place shortcut strings are for ftp addresses and modifying the script accordingly.

      If I get some free time I will certainly look at this.

      Thanks again for reading,

      -Dave

      Delete
    2. Donat,

      Check out my new post which should allow you to also use FTP addresses. Please try and let me know how it works.

      http://david-merritt.blogspot.co.uk/2013/01/new-automate-creating-sharepoint.html

      Regards,

      -Dave

      Delete
  4. Thanks for the script:) it did work very well but since couple of weeks the network location to sharepoint online creates white icon and not accessable ( no error just nothing) anyone know if this is caused due to windows update or security patch?

    ReplyDelete
  5. Hi David
    Thanks for this very nice script, I'm having problems getting it to work in Windows 10, any ideas?

    Regards
    Franz

    ReplyDelete