I know a few ways that you don't. First attempt was to use the winnt://localhost/lanmanserver/ object to try updating the path of the share. No luck the property is read only!
At the same time I have learned or realized a few things that I didn't thought about. Most scripts I have written in the past has been for controlled environments. By that I mean a system where a specific task was asked from the script and the run was always with the same parameters. Here I'm writing a script that should run on different versions of the operating system and with several different variables.
So the window version of the script is pretty useless with a standard setup. How ever if you run the script with elevation from the cmd you will be able to move shares around on the disk. The script is pretty ruff around the edges and some more error handling and stuff needs to be added. There probably will be solvable to get the share to keep it's access list as well. This script should be seen as a proof of concept.
Option explicit
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If (InStr(1, WScript.FullName, "cscript", vbTextCompare)) Then
InitConsole
ElseIf (InStr(1, WScript.FullName, "wscript", vbTextCompare)) Then
InitWindow
Else
wscript.echo "How the hell did you start this script?"
End If
Set FSO = Nothing
Sub InitConsole()
If (Wscript.Arguments.Count < 2) Then
wscript.echo "Move share script"
wscript.echo ""
wscript.echo "Use: cscript moveshare.vbs {source} {destination}"
wscript.echo ""
wscript.echo "source = complete path of the shared folder"
wscript.echo "destination = full path of the folder where the shared folder should be moved"
wscript.echo ""
wscript.echo "moveshare.vbs Kristofer Källsbo http://www.hackviking.com"
Else
Dim sourcePath, destPath
sourcePath = Wscript.Arguments(0)
destPath = Wscript.Arguments(1)
If Not (FSO.FolderExists(sourcePath)) Then
wscript.echo "Source path was not found!"
Exit Sub
End If
If Not (FSO.FolderExists(destPath)) Then
wscript.echo "Destination path was not found!"
Exit Sub
End If
wscript.echo MoveShare(sourcePath, destPath)
End If
End Sub
Sub InitWindow()
Dim sourcePath, destPath, msgboxRetval
sourcePath = InputBox("This script will move an entire shared folder to a new location withou losing the share!" & vbNewLine & vbNewLine & "Enter path of share:", "Move Share Script")
If (sourcePath = "") Then
msgboxRetval = MsgBox("Source path not entered or cancel was clicked!", vbOKOnly+vbCritical, "Source path error!")
Exit Sub
Else
If Not (FSO.FolderExists(sourcePath)) Then
msgboxRetval = MsgBox("Source path was not found!", vbRetryCancel+vbCritical+vbDefaultButton1, "Source path error!")
If (msgboxRetval = 4) Then
InitWindow
End If
Exit Sub
End If
End if
destPath = InputBox("Enter destination path of share:", "Move share script")
If (destPath = "") Then
msgboxRetval = MsgBox("Destination path not entered or cancel was clicked!", vbOKOnly+vbCritical, "Destination path error!")
Exit Sub
Else
If Not (FSO.FolderExists(destPath)) Then
msgboxRetval = MsgBox("Destination path was not found!", vbRetryCancel+vbCritical+vbDefaultButton1, "Destination path error!")
If (msgboxRetval = 4) Then
InitWindow
End If
Exit Sub
End If
End If
wscript.echo MoveShare(sourcePath, destPath)
End Sub
Function MoveShare(sourcePath, destPath)
Dim retval, objWMI, objInstances, objInstance, sourceFolder, sourceName, AccessMask, AllowMaximum, Caption, Description, MaximumAllowed, Name, shareType, delRetval, objNewShare, createRetval
destPath = destPath & "\"
Set objWMI = GetObject("winmgmts://./root\cimv2")
Set objInstances = objWMI.InstancesOf("Win32_Share",48)
Set sourceFolder = FSO.GetFolder(sourcePath)
sourcePath = sourceFolder.Path
sourceName = sourceFolder.Name
For Each objInstance in objInstances
If(objInstance.Path = sourcePath) Then
With objInstance
AccessMask = .AccessMask
AllowMaximum = .AllowMaximum
Caption = .Caption
Description = .Description
MaximumAllowed = .MaximumAllowed
Name = .Name
shareType = .Type
End With
delRetval = objInstance.Delete()
If Not(delRetval = 0) Then
retval = "Error: Unable to delete share!" & vbNewLine & "Error code: " & delRetval
MoveShare = retval
Exit Function
End If
End If
Next
On Error Goto 0
FSO.MoveFolder sourcePath, destPath
Set objNewShare = objWMI.Get("Win32_Share")
createRetval = objNewShare.Create(destPath & sourceName, Name, shareType, MaximumAllowed, Description, null, null)
If Not(createRetval = 0) Then
retval = "Error: New share not created!"
End If
MoveShare = retval
End Function