Scripts to manage Shared Folders
Creating a Network ShareDeleting a Network Share
Deleting a Published Folder
Enumerating Network Shares
Enumerating Published Shares
Mapping All Network Shares to Local Folders
Mapping a Network Share to a Local Folder
Modifying a Network Share
Publishing a Shared Folder
Searching for Published Folders
Creating a Network Share
Creates a shared folder named FinanceShare, setting the maximum number of simultaneous connections to 25, and adding a share description.
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.") Wscript.Echo errReturn
Deleting 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
Deleting a Published Folder
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)
Enumerating Network Shares
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 "AllowMaximum: " & vbTab & objShare.AllowMaximum Wscript.Echo "Caption: " & vbTab & objShare.Caption Wscript.Echo "MaximumAllowed: " & vbTab & objShare.MaximumAllowed Wscript.Echo "Name: " & vbTab & objShare.Name Wscript.Echo "Path: " & vbTab & objShare.Path Wscript.Echo "Type: " & vbTab & objShare.Type Next
Enumerating Published Shares
Enumerates 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("Timeout") = 30 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = False 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
Mapping 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
Mapping 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
Modifying 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 Wscript.Echo errReturn
Publishing a Shared Folder
Publishes a shared folder in Active Directory, assigning the folder a description and three keywords.
Set objComputer = GetObject _ ("LDAP://OU=Finance, DC=fabrikam, DC=com") Set objShare = objComputer.Create("volume", "CN=FinanceShare") objShare.Put "uNCName", "\\atl-dc-02\FinanceShare" objShare.Put "Description", "Public share for users in the Finance group." objShare.Put "Keywords", Array("finance", "fiscal", "monetary") objShare.SetInfo
Searching for Published Folders
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("Timeout") = 30 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = False 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