最简单的方法是炮击:
Set sh = CreateObject("WScript.Shell")
sh.Run "net share sharename=C:\some\folder /grant:Everyone,FULL", 0, True
完全在 VBScript 中完成它是可能的,但需要更多的代码。此处已发布了一个示例脚本,可以根据您的特定要求对其进行简化,如下所示:
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