Advertisement
6_2008-2009 Security #208654

Create a NT Share

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...

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'<pre>
' 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(objSWbem.name = strShareName)Then  
			blnShareExists = True 
			Exit For 
		Else            
			blnShareExists = False 
	    End If 
	Next  
	' 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) 
    Else 
    	msgbox("Folder aready shared") 
    End If 
End Sub
'</pre>
Original Comments (3)
Recovered from Wayback Machine