Solved

Shares count. Scan all machines and get the count of shares in each machine.

Posted on 2010-08-30
17
244 Views
Last Modified: 2012-05-10
Hi,

Shares count. Scan all machines and get the count of shares in each machine.
I will have a txt file with machine names. when run has to show results as this

machine name1 : 10
machine name2 : 4
machine name3 : 0

Can anyone help me with a script.

regards
Sharath
0
Comment
Question by:bsharath
[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
  • 7
  • 6
  • 4
17 Comments
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33559111
Do you want to count the ADMIN shares?  ADMIN$, IPC$, C$, D$, etc..

If not, you can run this:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

On Error Resume Next
ServerList = "Servers.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(ServerList, ForReading, True) Set objResults = fso.CreateTextFile("ServerShares.txt", ForWriting)

Do While f.AtEndOfStream <> True
      strServer = f.ReadLine

      Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")

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

      If err.Number = 0 Then
            For each objShare in colShares
              x = x + 1
            Next
            objResults.Writeline strComputer & ": " & x
      Else
            objResults.Writeline strComputer & vbTab & err.Number
            err.Clear
      End If
      x = 0
Loop
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33559115
Sorry, this will include the ADMIN shares in the count.
0
 
LVL 17

Accepted Solution

by:
Tony Massa earned 250 total points
ID: 33559139
EE really needs to fix the paste function...here's the script attached...
Shares-Count.vbs.txt
0
Independent Software Vendors: 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!

 
LVL 11

Author Comment

by:bsharath
ID: 33559142
i want to exclude them
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33559155
I just keep messing this up...first file had strComputer in the output instead of strServer
Shares-Count.vbs.txt
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33559424
Here you go...just change the WMI query to look for share type of '0'
Shares-Count.vbs.txt
0
 
LVL 11

Author Comment

by:bsharath
ID: 33559637
Printers also should be excluded...
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33559715
This is just folder shares...printers are not included.  Printer Shares are of TYPE 1

So if you wanted to find all printer shares, change the "WHERE Type = '0'  to "WHERE Type = '1'"
0
 
LVL 11

Author Comment

by:bsharath
ID: 33559737
Machines that are off or permission error i get the no as 462 or 70
0
 
LVL 12

Expert Comment

by:Rant32
ID: 33562440
That's because of the output in the last Else statement.

Remove line (24) objResults.Writeline strComputer & vbTab & err.Number

For no output on error.
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33562520
or change the text of the objResults.writeline to strServer & ": ERROR " & err.Number

I like to know if I get errors in most of my scripts.  Just a bad error check message on my part.
0
 
LVL 11

Author Comment

by:bsharath
ID: 33603511
Can i have the full code please after the changes
0
 
LVL 12

Expert Comment

by:Rant32
ID: 33603557
Computer names/IP address go in Servers.txt
ServerList = "Servers.txt"

arrServers = Split(CreateObject("Scripting.FilesystemObject").OpenTextFile(ServerList).ReadAll, vbNewline)

On Error Resume Next

For Each Server in arrServers
    Err.Clear
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Server & "\root\cimv2")
    If Err.Number = 0 Then
        WScript.Echo Server & ": " & objWMIService.ExecQuery("SELECT * from Win32_Share WHERE Type = '0'").Count
    End If
Next

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 33603586
Thanks
I want the results into a file in a rwadable format. i get in msg boxes now
0
 
LVL 12

Expert Comment

by:Rant32
ID: 33603603
Ah, 'm sorry, forgot to mention. I usually run it from a command prompt with cscript, so:

cscript scanshares.vbs

You can redirect the output to file with:

cscript scanshares.vbs > results.txt

Hope that helps.
0
 
LVL 11

Author Comment

by:bsharath
ID: 33603612
Thanks
But can we have it inbuilt so it exports the results when run
0
 
LVL 12

Assisted Solution

by:Rant32
Rant32 earned 250 total points
ID: 33603674
Yes, absolutely. This will also work when opening from Explorer. No message boxes.
ServerList = "Servers.txt"
ResultFile = "Results.txt"

Const ForWriting = 2

arrServers = Split(CreateObject("Scripting.FilesystemObject").OpenTextFile(ServerList).ReadAll, vbNewline)

Set oResultfile = CreateObject("Scripting.FilesystemObject").CreateTextFile(ResultFile, ForWriting)

On Error Resume Next

For Each Server in arrServers
    Err.Clear
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Server & "\root\cimv2")
    If Err.Number = 0 Then
        oResultfile.WriteLine Server & ": " & objWMIService.ExecQuery("SELECT * from Win32_Share WHERE Type = '0'").Count
    End If
Next

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

631 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