' www.jensdevries.nl Synology's WebDAV Share.vbs
'

Option Explicit
Dim errReturn, strURL, strDomainPort

' *************************************************************************
' Configurable Variables
' *************************************************************************
strURL = "\\myds01.vpnplus.jensdevries.nl@SSL@10000\DavWWWRoot" ' This is the URL to the WebDAV share.
strDomainPort = "myds01.vpnplus.jensdevries.nl@SSL@10000"


' *************************************************************************
' This subroutine searches for the WebDAV service known as 'WebClient' and
'  makes sure that it is configured for automatic startup, and that it is 
'  currently running.
' *************************************************************************
Sub ConfigureService()
   Dim objWMIService
   Set objWMIService = GetObject("winmgmts:"_
       & "{impersonationLevel=impersonate}!\\.\root\cimv2")


   ' We only care about one service, so the search is only for
   '    the WebClient service.
   Dim colServiceList
   Set colServiceList = objWMIService.ExecQuery _
      ("Select * from Win32_Service where Name = 'WebClient'")


   ' If more than one service was returned, something is funky.
   ' Likewise, if no services are returned, we shouldn't be running.
   If colServiceList.Count = 1 Then
      Dim objService
      For Each objService in colServiceList
         ' Test to see if the service is scheduled to run on startup, if not, configure it to.
         If objService.StartMode <> "Automatic" Then
            errReturn = objService.Change( , , , , "Automatic")
         End If
         ' Test to see if the service is currently running, if not, start it.
         If objService.State <> "Started" Then
            objService.StartService()
         End If
      Next
   Else
      WScript.Echo "Kan de “WebClient” service niet vinden."
   End If
End Sub

' *********************************************************************
' This subroutine creates a shortcut to the web disk.
' *********************************************************************

Sub CreateShorty ()
   Dim strName, blnDeleteMode, objWSHShell, strDesktop, oMyShortCut
   
   MsgBox "Verbinding maken met de WebDAV Share op uw Synology NAS. Dit kan enkele minuten duren...", 64, "Verbinding maken met de Synology's WebDAV Share"

   strName = "Synology's WebDAV Share"
   Set objWshShell = CreateObject("WScript.Shell")
   Const MAXIMIZE_WINDOW = 3
 
   strDesktop = objWshShell.SpecialFolders("Desktop")
   Set oMyShortCut = objWshShell.CreateShortcut(strDesktop & "\" & strName & ".lnk")
   oMyShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,9"
   oMyShortCut.TargetPath = strURL
   oMyShortCut.Description = strName
   oMyShortCut.WorkingDirectory = strDomainPort
   oMyShortCut.Save

   objWshShell.Run chr(34) & strDesktop & "\" & strName & ".lnk" & chr(34), MAXIMIZE_WINDOW
    
End Sub

' *********************************************************************
' This subroutine opens Windows Explorer to My Network Places to show
' the location of the Synology's WebDAV Share.
' *********************************************************************

' *********************************************************************
' Main Function Area.  This is where it all goes down.
' *********************************************************************
ConfigureService
CreateShorty
