Hi Jeff,
I am sorry if I have made any confusion.
Here goes the code. Please take a look at the comments in the code.
Basically we need to read the links below to achieve the job.
How to use the Win32_Share to create network share
Win32_Share
http://msdn.microsoft.com/library/de...us/wmisdk/wmi/
win32_share.asp
Create Method of the Win32_Share Class
http://msdn.microsoft.com/library/de...us/wmisdk/wmi/
create_method_in_class_win32_share.asp
How to set the security account
Win32_ACE
http://msdn.microsoft.com/library/de...us/wmisdk/wmi/
win32_ace.asp
Win32_Trustee
http://msdn.microsoft.com/library/de...us/wmisdk/wmi/
win32_trustee.asp
How to locate a user account by ldap path
LDAP ADsPath
http://msdn.microsoft.com/library/de...us/adsi/adsi/l
dap_adspath.asp
<code>
Imports System.Management
Imports System.DirectoryServices
Module Module1
Sub Main()
Dim serverName, UserName, AdminName, UserAcct As String
Dim Ace1 As Management.ManagementObject
serverName = "TestServer"
Dim scope As New
Management.ManagementScope("\\serverName\root\cimv 2")
'Win32_Share
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/win32_share.asp
Dim wmiShare As New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_Share"), Nothing)
'Define the LDAP path to the desired user account.
'LDAP ADsPath
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/
ldap_adspath.asp
UserAcct = "CN=Jeff Smith,CN=users,DC=fabrikam,DC=com"
'Get the user's information from the Active Directory
Dim UserNamePath As String = "LDAP://" & UserAcct
Dim DirEnt As New DirectoryServices.DirectoryEntry(UserNamePath)
'Get the User's SID and samAccountname. It will be used when
creating the DACL
UserName = DirEnt.Properties("samaccountname")(0)
Dim UserSID As Byte() = DirEnt.Properties("objectsid")(0)
DirEnt.Dispose()
'Set an ACE for the user
Ace1 = SetAce(scope, 2032127, 3, 0, SetTrustee(scope, serverName,
UserName, UserSID))
'Place the aces into a Win32_SecurityDescriptor
Dim secDescriptor As Management.ManagementObject
secDescriptor = New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_SecurityDescripto r"),
Nothing).CreateInstance()
secDescriptor("ControlFlags") = 4
secDescriptor("DACL") = New Management.ManagementObject() {Ace1}
'Prepare to invoke the CREATE method on Win32_Share by specifying
the parameters
Dim inParams As Management.ManagementBaseObject
inParams = wmiShare.GetMethodParameters("Create")
inParams("Access") = secDescriptor
inParams("Path") = "C:\TestFolder"
inParams("Name") = "TestFolder"
inParams("Type") = 0
inParams("Description") = "FolderDescriptionHere"
Dim outParams As Management.ManagementBaseObject
'Invoke the CREATE method and create the share.
'Create Method of the Win32_Share Class
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/create_method_in_class_win32_share.asp
outParams = wmiShare.InvokeMethod("Create", inParams, Nothing)
End Sub
Function SetTrustee(ByVal scope As Management.ManagementScope, ByVal
domain As String, ByVal name As String, ByVal UserSid As Byte()) As
Management.ManagementObject
Dim wmiTrustee As Management.ManagementObject
'Win32_Trustee
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/win32_trustee.asp
wmiTrustee = New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_Trustee"), Nothing).CreateInstance()
wmiTrustee("Domain") = domain
wmiTrustee("Name") = name
wmiTrustee("SID") = UserSid
Return wmiTrustee
End Function
Function SetAce(ByVal scope As Management.ManagementScope, ByVal
accessMask As Integer, ByVal aceFlags As Integer, ByVal aceType As Integer,
ByVal wmiTrustee As Management.ManagementObject) As
Management.ManagementObject
Dim wmiAce As Management.ManagementObject
'Win32_ACE
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi
/win32_ace.asp
wmiAce = New Management.ManagementClass(scope, New
Management.ManagementPath("Win32_ACE"), Nothing).CreateInstance()
wmiAce("AccessMask") = accessMask
wmiAce("AceFlags") = aceFlags
wmiAce("AceType") = aceType
wmiAce("Trustee") = wmiTrustee
Return wmiAce
End Function
End Module
</code>
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.