Friday, December 17, 2010

Create Windows Share on a remote computer

This subroutine will add a Windows (or NT) share to any computer provided you have a user with permission on that machine.
Private Sub CreateShare(strShareName, strPath, strDescription, strComputer, strDomain, strUser, strPassword) 
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
    Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, _
         "root\cimv2", strUser, strPassword, "MS_409", "ntlmdomain:" + strDomain) 
    Set colSwbemObjectSet = objSWbemServices.InstancesOf("Win32_Share") 

    For each objSWbem in colSwbemObjectSet 
        If(objSWbem.name = strShareName)Then  
            blnShareExists = True 
            Exit For 
        Else            
            blnShareExists = False 
        End If 
    Next  

    If (blnShareExists = False)Then 
        set objSWbemObject = objSWbemServices.Get("Win32_Share")  
        intRet = objSWbemObject.Create(strPath, strShareName, 0, _
            10, strDescription) 
    End If 
    msgbox(intret) 
End Sub
Sample call:
CreateShare "Share1", "D:\mydir", "this is mydir", "MyComp", "MyDomain.Net", "MyUser" , "MyPassword"

No comments: