Automate file sharing
Asked Answered
L

1

0

I've found some vbscript examples on how to enable file sharing and share a folder, however they leave the "Public folder sharing" off so that you have to be logged in rather than just allowing anyone with network access to read/write to public folders.

For all my googling, I can't seem to find out how to automate that last piece. Vbscript or anything that can be run via command line would be my preferred method of doing this, but really I'm open to hearing about any solution that would automate this.

EDIT:

In case it's relevant, I should point out that I'm going to be running this locally with admin privileges on non-domain computers.

EDIT2:

I've tried the solutions listed below, however the option I need to change affects the ability to connect to a computer to see the list of shares; it's not specific to a particular share.

Landahl answered 18/9, 2013 at 7:32 Comment(0)
S
0

The simplest way would be shelling out:

Set sh = CreateObject("WScript.Shell")
sh.Run "net share sharename=C:\some\folder /grant:Everyone,FULL", 0, True

Doing it entirely in VBScript is possible, but requires significantly more code. A sample script for this has been published here, which could be simplified for your particular requirements like this:

path      = "C:\some\folder"
sharename = "name"
comment   = "foo"

Set wmi = GetObject("winmgmts://./root/cimv2")

Set trustee = wmi.Get("Win32_Trustee").SpawnInstance_()
trustee.Domain = Null
trustee.Name   = "Everyone"
trustee.SID    = Array(1,1,0,0,0,0,0,1,0,0,0,0) 'SID S-1-1-0 (binary)

Set ace = wmi.Get("Win32_Ace").SpawnInstance_()
ace.AccessMask = 2032127  'full access
ace.AceFlags   = 3        'object inheritance + container inheritance
ace.AceType    = 0        'allow access
ace.Trustee    = trustee

Set sd = wmi.Get("Win32_SecurityDescriptor").SpawnInstance_()
sd.DACL = Array(ace)

Set share = wmi.Get("Win32_Share")
rc = share.Create(path, sharename, 0, 10, comment, "", sd)

If rc = 0 Then
  WScript.Echo "Folder " & path & " has been shared as " & sharename & "."
Else
  WScript.Echo "Error sharing folder " & path & ": " & rc
End If
Spermic answered 18/9, 2013 at 8:55 Comment(2)
It doesn't have to be vbscript, and I probably should have mentioned that I tried the first option you suggested in a simple batch file already. The problem is that when I open the share from another computer in the workgroup I am still prompted for a user name and password. I know how to fix this by going to advanced sharing options and changing the "Public folder sharing" option, but I don't know how to change that option in a command line or script.Landahl
I tried your vbscript, but as I suspected it is the same as using the net share command. The option I need to change isn't specific to a particular share so I don't think something that actually creates the share is what I need.Landahl

© 2022 - 2024 — McMap. All rights reserved.