Create a NT Share

Submitted on: 1/7/2015 10:37:00 AM
By: Rick Casady (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VbScript (browser/client side)
Views: 3737
     Create a share in a windows environment without using DOS commands. This code was based on information learned in WMI Scripting Primer: Part 3 on the MSDN
This code can easily be converted to VB.NET...

' Purpose: Create a network share for NT users. You will
' need to have correct permision to do this and it can be
' done to remote computers.
' We pass in the folder name, path of the folder and 
' description of the share folder.
Private Sub CreateShare(strShareName, strPath, strDescription) 
	Dim objSWbemServices as object  
	Dim objSWbemObject as object     
	Dim colSWbemObject as object  
    Dim intRet as integer  
    Dim blnExists as boolean  
    Dim objSWbem as object 
    ' Next we call the standard GetObject function for 
    ' returning COM objects and pass it the connection 
    ' string for connecting to the WMI.
    set objSWbemServices = GetObject("winmgmts:\\.\root\cimv2") 
    ' This same line can be executed on a remote computer 
    ' with a differnt connection string like this:
    ' objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    ' Now we enumrate the Shares on the target computer and
    ' return it to a collection
    set colSWbemObject = objSWbemServices.InstancesOf("Win32_Share")  
    ' Loop through each share on the machine to see if it already exists 
   	For each objSWbem in colSWbemObject  
   		If( = strShareName)Then  
			blnShareExists = True 
			Exit For 
			blnShareExists = False 
	    End If 
	' if the share didnt exisit our boolean will be false
	' and we can try to add it.
	If (blnShareExists = False)Then 
    	' Create the share 
    	' Now we need to get 
   		set objSWbemObject = objSWbemServices.Get("Win32_Share")  
   		' Last we call the create passing our pathg, name,
   		' description and 10 is for max number of users 
    	intRet = objSWbemObject.Create(strPath, strShareName, , 10, strDescription) 
    	msgbox("Folder aready shared") 
    End If 
End Sub

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.