Contact Info

Crumbtrail

ActiveXperts.com » Administration » VBScript Collection » Storage » Shared Folders

Shared Folders Scripting

Create a Network Share
Delete a Network Share
Delete a Published Folder
Lists all the shared folders on a computer.
List Shared Folders Published in Active Directory
Map All Network Shares to Local Folders
Modify a Network Share
Map a Network Share to a Local Folder
Add a Terminal Services Direct Connect License Server
Search for Specific Published Folders in Active Directory


You can use any of the VBScript programs below in ActiveXperts Network Monitor. Click here for an explanation about how to include scripts in ActiveXperts Network Monitor.



Create a Network Share


Const FILE_SHARE = 0 Const MAXIMUM_CONNECTIONS = 25 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objNewShare = objWMIService.Get("Win32_Share") errReturn = objNewShare.Create _ ("C:\Finance", "FinanceShare", FILE_SHARE, _ MAXIMUM_CONNECTIONS, "Public share for the Finance group.")
Creates a shared folder named FinanceShare, setting the maximum number of simultaneous connections to 25 and adding a share description.

Delete a Network Share


Stops sharing a shared folder named FinanceShare. This does not delete the folder from the local hard drive, but simply stops making it available over the network.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = 'FinanceShare'")

For Each objShare in colShares
    objShare.Delete
Next
	

Delete a Published Folder


Description Deletes a published folder named FinanceShare from Active Directory. This does not delete the share itself, it simply removes it from Active Directory.
Set objContainer = GetObject("LDAP://CN=FinanceShare, " _
    & "OU=Finance, DC=fabrikam, DC=com")

objContainer.DeleteObject (0)
	

Lists all the shared folders on a computer.


strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colShares = objWMIService.ExecQuery("Select * from Win32_Share") For each objShare in colShares Wscript.Echo "Allow Maximum: " & objShare.AllowMaximum Wscript.Echo "Caption: " & objShare.Caption Wscript.Echo "Maximum Allowed: " & objShare.MaximumAllowed Wscript.Echo "Name: " & objShare.Name Wscript.Echo "Path: " & objShare.Path Wscript.Echo "Type: " & objShare.Type Next
List Network Shares

List Shared Folders Published in Active Directory


Lists all the shared folders that have been published in Active Directory.
Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, unCName, ManagedBy from " _
    & "'LDAP://DC=Fabrikam,DC=com' where objectClass='volume'"
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Wscript.Echo "Share Name: " & objRecordSet.Fields("Name").Value
    Wscript.Echo "UNC Name: " & objRecordSet.Fields("uNCName").Value
    Wscript.Echo "Managed By: " & objRecordSet.Fields("ManagedBy").Value
    objRecordSet.MoveNext
Loop
	

Map All Network Shares to Local Folders


Uses a WMI Associators of query to return the local path of a network share named Scripts.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery("Select * From Win32_Share")

For Each objShare in colShares
    Set colAssociations = objWMIService.ExecQuery _
        ("Associators of {Win32_Share.Name='" & objShare.Name & "'} " _
            & " Where AssocClass=Win32_ShareToDirectory")
    For Each objFolder in colAssociations
        Wscript.Echo objShare.Name & vbTab & objFolder.Name
    Next
Next
	

Modify a Network Share


Accesses a shared folder named FinanceShare, changes the maximum number of simultaneous connections to 50, and provides a new share description.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = 'FinanceShare'")

For Each objShare in colShares
    errReturn = objShare.SetShareInfo(50, _
        "Public share for HR administrators and the Finance Group.")
Next
	

Map a Network Share to a Local Folder


Uses a WMI Associators of query to return the local path of all the network shares on a computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery _
    ("Associators of {Win32_Share.Name='Scripts'} Where " _
        & "AssocClass=Win32_ShareToDirectory")

For Each objFolder in colShares
    Wscript.Echo objFolder.Name
Next
	

Add a Terminal Services Direct Connect License Server


Adds the computer atl-ts-01 as a direct connect license server to a computer running Terminal Services.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_TerminalServiceSetting")

For Each objItem in colItems
    errResult = objItem.AddDirectConnectLicenseServer("atl-ts-01")
Next
	

Search for Specific Published Folders in Active Directory


Searches Active Directory for any shared folders that have the keyword "finance."
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, unCName, ManagedBy from "
    & "'LDAP://DC=Reskit,DC=com'" _
        & " where objectClass='volume' and Keywords = 'finance*'"
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Wscript.Echo "Share Name: " & objRecordSet.Fields("Name").Value
    Wscript.Echo "UNC Name: " & objRecordSet.Fields("uNCName").Value
    Wscript.Echo "Managed By: " & objRecordSet.Fields("ManagedBy").Value
    objRecordSet.MoveNext
Loop