Friday, 14 March 2014

VBScript to Get List of Network Shares using WMI Service

Description:

In this article, I am going write VBScript code to find and get list of Network Shares/Share Folders in Local Machine and Remote Server using VBScript by different methods.

Summary:


VBScript to List Network Shares in Local Computer

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ListNetworkSharesinLocalComputer.vbs
3. Run usage in CMD:
C:\> CScript <vbscript file path> 
Example: CScript C:\Scripts\ListNetworkSharesinLocalComputer.vbs 
Run the above command to list Network Share Folders with VBScript using WMI Service in Local Computer

Click to get VBScript source code as a file Download ListNetworkSharesinLocalComputer.vbs
 
' ListNetworkSharesinLocalComputer.vbs
' Sample VBScript code with WMI Service to find and list Network Shares .
' Author: http://www.morgantechspace.com/
' ------------------------------------------------------

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(".", "root\cimv2")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption   
Wscript.Echo "---------------------------------"
Next

' Wscript.Echo "Network Share Folders in Local computer listed successfully"

WScript.Quit
VBScript to Get List of Network Shares/Share Folders using WMI Service

Note: Here, I have filtered Win32_Share collections by its Type=0 to list only Share Folders. You can also filter by Type value other than 0 to get other shared items like Printers, Devices, IPC, and etc...Check the following Type values for corresponding Shared Items.

Shared Item Type
Disk Drive 0 (0x0)
Print Queue 1 (0x1)
Device 2 (0x2)
IPC 3 (0x3)
Disk Drive Admin 2147483648 (0x80000000)
Print Queue Admin 2147483649 (0x80000001)
Device Admin 2147483650 (0x80000002)
IPC Admin 2147483651 (0x80000003)


VBScript to Get Network Shares/Share Folders  in Remote Server

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Change the value of string variable 'strComputer' to your own Computer or Server name.
3. Save the file with a .vbs extension, for example: ListNetworkSharesinRemoteServer.vbs
3. Run usage in CMD:
C:\> CScript <vbscript file path> 
Example: CScript C:\Scripts\ListNetworkSharesinRemoteServer.vbs 
Run this command from Command Prompt to get list of Network Shares with VBScript using WMI Service in Remote Server.

Click to get VBScript source code as a file Download ListNetworkSharesinRemoteServer.vbs
 
' ListNetworkSharesinRemoteServer.vbs
' Sample VBScript to find and list Network Shares in Remote Machine .
' Author: http://www.morgantechspace.com/
' ------------------------------------------------------
strComputer = "hp-PC"
' you can change your own Computer or Server.

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(strComputer , "root\cimv2")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption   
Wscript.Echo "---------------------------------"
Next

' Wscript.Echo "Network Share Folders in Remote Computer listed successfully"

WScript.Quit

Find Network Share Folders in VBScript by using other User Credential

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Change the value of string variable 'strComputer' to your own Computer or Server name.
3. Change the user account 'Administrator' to your own user credential and give valid password.
4. Save the file with a .vbs extension, for example: ListNetworkSharesAsDiffUser.vbs
5. Run usage in CMD:
C:\> CScript <vbscript file path> 
Example: CScript C:\Scripts\ListNetworkSharesAsDiffUser.vbs 
    Run this command from Command Prompt to get list of Network Shares with VBScript using WMI Service by using different User Credential.

Note: You can not use User Credentials for local computer to connect WMI Service.

Click to get VBScript source code as a file Download ListNetworkSharesAsDiffUser.vbs
' ListNetworkSharesbyDiffUser.vbs
' Sample VBScript by WMI to find and list Network Shares.
' Author: http://www.morgantechspace.com/
' ------------------------------------------------------

strComputer = "myComputer"
' you can change your own Computer or Server.

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(strComputer , "root\cimv2","Administrator","YourPassword")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption   
Wscript.Echo "---------------------------------"
Next

' Wscript.Echo "Network Share Folders in Remote Computer listed successfully"

WScript.Quit

VBScript to Get List of Network Shares by Dynamic Inputs

1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ListNetworkSharesbyDynamicComputer.vbs
3. Run usage in CMD:
C:\> CScript <vbscript file path> 
Example: CScript C:\Scripts\ListNetworkSharesbyDynamicComputer.vbs 
   Run this command in Command prompt to List Network Share Folders with VBScript using WMI Service.
4. Then give the Computer name in the opened Computer name text box and click OK to proceed...

Click to get VBScript source code as a file Download ListNetworkSharesbyDynamicComputer.vbs
' ListNetworkSharesbyDynamicComputer.vbs
' Sample VBScript by WMI to find and list Network Shares.
' Author: http://www.morgantechspace.com/
' ------------------------------------------------------
strComputer = "."

Do
   strComputer = InputBox ("Please enter computer name")
   If strComputer = "" then
      Msgbox "No computer name entered"
   end if
Loop Until strComputer <> ""

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService= objSWbemLocator.ConnectServer(strComputer , "root\cimv2")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

For each objShare in colShares

    Wscript.Echo "Name: " & objShare.Name  & vbCrLf _
                       & "Path: " & objShare.Path & vbCrLf  _ 
                       & "Type: " & objShare.Type  & vbCrLf _
                       & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
                       & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
                       & "Caption: " & objShare.Caption  
Wscript.Echo "---------------------------------" 
Next

' Wscript.Echo "Network Share Folders in Remote Computer listed successfully"

WScript.Quit

Advertisements
Advertisements

4 comments:

  1. Hi.. when I tried too call objShare.Path I m getting an error
    "Object doesn't support this property or method: 'objService.Path'" can you help me out

    ReplyDelete
    Replies
    1. can you provide the complete vbscript code to test here?

      Delete
  2. Hi,

    I ran "VBScript to Get Network Shares/Share Folders in Remote Server" its worked and thanks.

    Could you please share the code for If I wanted to run multiple servers share path details...

    ReplyDelete
    Replies
    1. Hi mahi, nice to see asking help from tamilan.

      I think, this is the script you wanted

      ' ListNetworkSharesinRemoteServer.vbs
      ' Sample VBScript to find and list Network Shares in Remote Machine .
      ' Author: http://www.morgantechspace.com/
      ' ------------------------------------------------------

      Dim lstServers
      lstServers = Array("Server1","Server2","Server3")

      For Each strComputer In lstServers

      Wscript.Echo "Share Folders in "& strComputer
      Wscript.Echo "---------------------------------"

      Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
      Set objWMIService= objSWbemLocator.ConnectServer(strComputer , "root\cimv2")

      Set colShares = objWMIService.ExecQuery("Select * from Win32_Share Where Type=0")

      For each objShare in colShares

      Wscript.Echo "Name: " & objShare.Name & vbCrLf _
      & "Path: " & objShare.Path & vbCrLf _
      & "Type: " & objShare.Type & vbCrLf _
      & "Allow Maximum: " & objShare.AllowMaximum & vbCrLf _
      & "Maximum Allowed: " & objShare.MaximumAllowed & vbCrLf _
      & "Caption: " & objShare.Caption
      Wscript.Echo "--------"
      Next
      Wscript.Echo "-------------------------------------"
      Next

      WScript.Quit


      Delete