Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vbscript - list all shares, export to spreadsheet, don't include default shares

Posted on 2016-08-04
8
Medium Priority
?
98 Views
Last Modified: 2016-08-08
I'm trying to write a script that will list all shares on all drives
Then ignore the default shares:  $ADMIN, Apps$, C$, IPC$, Sys$ and only report (send email and dump to spreadsheet) the shares other than this.
Also trying to have this run locally on each computer and send email only when computer is detected to be authenticated to the domain it's on.  

Here is what I have so far, any help is appreciated.




Dim sSysDrive : sSysDrive = CreateObject( "Scripting.FileSystemObject" ) _
      .GetDriveName(  _
          CreateObject( "WScript.Shell" ).Environment( "PROCESS" )( "SYSTEMROOT" ) )
Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject( "winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2" )

Dim colDisks
  Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")

Dim drives : drives = ""
Dim objDisk
  For Each objDisk in colDisks
      wscript.echo objdisk.drivetype &" "&objDisk.DeviceID

      If objDisk.DriveType = 3 And objDisk.DeviceID <> sSysDrive Then
         If drives > "" Then
            drives = drives & ";"
         End If
         drives = drives & objDisk.DeviceID & "\"
      End if
  Next
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

WScript.Echo drives
0
Comment
Question by:Rob Shorts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 41742663
You can use the Type to filter the results so that the Administrative Shares are not retrieved; e.g. -
Dim strComputer, strResult

strComputer = Inputbox("Enter Computer Name","Enter Computer Name")

If Trim(strComputer) = "" Then strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colShares = objWMIService.ExecQuery("SELECT * FROM Win32_Share WHERE NOT Type=2147483648 AND NOT Type=2147483649 AND NOT Type=2147483650 AND NOT Type=2147483651")
For each objShare in colShares
strResult = strResult + "AllowMaximum: " & vbTab & objShare.AllowMaximum & vbcrlf &_
	"Caption: " & vbTab & objShare.Caption & vbcrlf &_
	"MaximumAllowed: " & vbTab & objShare.MaximumAllowed  & vbcrlf &_
	"Name: " & vbTab & objShare.Name & vbcrlf &_
	"Path: " & vbTab & objShare.Path & vbcrlf &_
	"Type: " & vbTab & objShare.Type & vbcrlf & vbcrlf
Next
wscript.echo "Shares on computer: " & strComputer & vbcrlf &vbcrlf & strResult
wscript.echo "Done"

Open in new window

Produces the following results -Capture.JPG
The Type values were retrieved from here: Win32_Share class

-saige-
1
 

Author Comment

by:Rob Shorts
ID: 41742729
Thank you.  Any idea how I could get the other shares excluded along with the admin shares? IPC$ and Sys$ for example?
0
 
LVL 34

Expert Comment

by:it_saige
ID: 41742775
This is excluding all Administrative shares; namely:

  • Disk Drive Admin (2147483648)
  • Examples - ADMIN$, C$, D$, E$
  • Print Queue Admin (2147483649)
  • Device Admin (2147483650)
  • IPC Admin (2147483651)
  • Examples - IPC$

-saige-
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rob Shorts
ID: 41743031
Yes, it excludes Administrative shares, but to recognize other shares I would need different method correct?
0
 
LVL 34

Accepted Solution

by:
it_saige earned 2000 total points
ID: 41743156
In that case you could use a combination of UBound and Filter in conjunction with an array of exclusions and check against the sharename; e.g. -

My current output with the above script -Capture.JPGNow I want to exclude print$, CSDemo and for grins I will add sys$:
Dim strComputer, strResult
Dim exclusions

strComputer = Inputbox("Enter Computer Name","Enter Computer Name")

If Trim(strComputer) = "" Then strComputer = "."
exclusions = Array("print$", "csdemo", "sys$")

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colShares = objWMIService.ExecQuery("SELECT * FROM Win32_Share WHERE NOT Type=2147483648 AND NOT Type=2147483649 AND NOT Type=2147483650 AND NOT Type=2147483651")
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add

For each objShare in colShares
	If UBound(Filter(exclusions, objShare.Name, True, 1)) < 0 Then
		strResult = strResult + "AllowMaximum: " & vbTab & objShare.AllowMaximum & vbcrlf &_
			"Caption: " & vbTab & objShare.Caption & vbcrlf &_
			"MaximumAllowed: " & vbTab & objShare.MaximumAllowed  & vbcrlf &_
			"Name: " & vbTab & objShare.Name & vbcrlf &_
			"Path: " & vbTab & objShare.Path & vbcrlf &_
			"Type: " & vbTab & objShare.Type & vbcrlf & vbcrlf
	End If
Next
wscript.echo "Shares on computer: " & strComputer & vbcrlf & vbcrlf & strResult
wscript.echo "Done"

Open in new window

I now get the following output -Capture.JPG-saige-
1
 

Author Comment

by:Rob Shorts
ID: 41743193
Thank you so much, exactly what I was looking to do!
0
 

Author Comment

by:Rob Shorts
ID: 41746257
One more question, how would I get the output "Name:" and "Path" to output and send via email?

Thank you
0
 

Author Closing Comment

by:Rob Shorts
ID: 41747228
Much appreciated!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question