Question

Add/Sum Totals from two different computers

Asked by: richrumble

I have the following code (below), and I'd like to add the "objItem.Size" for each computer together and echo that total...
I also have to specify multiple IP's or FQDN without a space after each comma, can that be fixed too? " /s:1.2.3.4,1.2.3.5,1.2.3.6 " but I'd like to enter "/s:1.2.3.4, 1.2.3.5, 1.2.3.6"

This is the output I have now, and i'd like to add the last line that adds to Size_1 totals for each computer or HD it gets a total from...
Caption: C:
Size_1: 32201936896 <----
Size: 30 GB
Serial: E85B1B6A

Caption: C:
Size_1: 79990812672   <----
Size: 74.5 GB
Serial: B40E65D6

Total= 112192749568  <------------

' Usage:
'cscript /nologo inventory.vbs /s:ip.ip.ip.ip  /u:user_name /p:password
' Multiple IP's should be comma seperated... IP's could also be dns-name(s)
' if no parametes are given local host and all parameters are run...
'Encrypts WMI Connections
Function ConvertSize(Size)
Do While InStr(Size,",") 'Remove commas from size
    CommaLocate = InStr(Size,",")
    Size = Mid(Size,1,CommaLocate - 1) & _
        Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
Loop
Suffix = " Bytes"
If Size >= 1024 Then suffix = " KB"
If Size >= 1048576 Then suffix = " MB"
If Size >= 1073741824 Then suffix = " GB"
If Size >= 1099511627776 Then suffix = " TB"
Select Case Suffix
    Case " KB" Size = Round(Size / 1024, 1)
    Case " MB" Size = Round(Size / 1048576, 1)
    Case " GB" Size = Round(Size / 1073741824, 1)
    Case " TB" Size = Round(Size / 1099511627776, 1)
End Select
ConvertSize = Size & Suffix
End Function
 
Const WbemAuthenticationLevelPktPrivacy = 6 
On Error Resume Next
If Wscript.Arguments.Named("u") <> "" And Wscript.Arguments.Named("p") <> "" Then
    boolAlternate = True
    strUser = WScript.Arguments.Named("u")
    strPassword = WScript.Arguments.Named("p")
    WScript.Echo "Using alternate credentials..."
Else
        boolAlternate = False
        WScript.Echo "Using current credentials..."
End If
If WScript.Arguments.Named("s") = "" Then
        arrComputers = Array(".")
Else
    arrComputers = Split(WScript.Arguments.Named("s"), ",")
End If
 
For Each strComputer In arrComputers
    If boolAlternate = True Then
        strNamespace = "root\cimv2"
        Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
        Set objWMIService = objwbemLocator.ConnectServer(strComputer, strNamespace, strUser, strPassword)
        objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
    Else
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where Description = 'Local Fixed Disk'",,48)
	For Each objItem in colItems
		WScript.Echo "Caption: " & objItem.Caption
		WScript.Echo "Size_1: " & objItem.Size
		Wscript.Echo "Size: " & ConvertSize(objItem.Size)
		Wscript.Echo "Serial: " & objItem.VolumeSerialNumber
	Next
Next

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-19 at 11:14:40ID24824526
Tags

VBS

,

VBScript

,

WMI

Topic

VB Script

Participating Experts
1
Points
500
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. sum
    Experts, how can I sum up the total result of the fetched array? $query="select * FROM adding where board='$board'"; $results = mysql_query ($query); while ($subtotal = mysql_fetch_array($results)) { echo "Sub amount: $subtotal[numbers]"; // example m...
  2. Sum an array - producing totals for a reporting system
    Hi, I'm building a reporting system in PHP and Mysql. I would like to know how to sum arrays. The following is part of my script: ============================ $rep9=mysql_query("SELECT zerouno, COUNT(cod_persone) AS 'count for zerouno' FROM persone where YEAR(times...
  3. Sum Total
    Hi Brad Thanks for your solution, it works a dream and does just what I want, but now I have another question/problem for the same. The solution you gave works but now I need the solution , of how to get it to work if I enter a number into 2 diffrent cells and get a running t...
  4. Excel: Copying Subtotal Captions and SUMs to Summary …
    I have a 19 column transactional worksheet with subtotals based on change of day. How do I copy subtotal captions (e.g., 01-Mar-06 Total) and SUMs (e.g., $1001.77) to a summary worksheet for further analysis? Please advise? Best wishes, John

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: purplepomegranitePosted on 2009-10-20 at 01:15:48ID: 25611863

The below should do what you want.  It trims any whitespace from the input parameter (meaning it doesn't matter how many spaces surround the commas anymore), and keeps a running total of the size.

' Usage:
'cscript /nologo inventory.vbs /s:ip.ip.ip.ip  /u:user_name /p:password
' Multiple IP's should be comma seperated... IP's could also be dns-name(s)
' if no parametes are given local host and all parameters are run...
'Encrypts WMI Connections
Function ConvertSize(Size)
	Do While InStr(Size,",") 'Remove commas from size
		CommaLocate = InStr(Size,",")
		Size = Mid(Size,1,CommaLocate - 1) & _
		Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
	Loop
	Suffix = " Bytes"
	If Size >= 1024 Then suffix = " KB"
	If Size >= 1048576 Then suffix = " MB"
	If Size >= 1073741824 Then suffix = " GB"
	If Size >= 1099511627776 Then suffix = " TB"
	Select Case Suffix
		Case " KB" Size = Round(Size / 1024, 1)
		Case " MB" Size = Round(Size / 1048576, 1)
		Case " GB" Size = Round(Size / 1073741824, 1)
		Case " TB" Size = Round(Size / 1099511627776, 1)
	End Select
	ConvertSize = Size & Suffix
End Function
 
Const WbemAuthenticationLevelPktPrivacy = 6 
On Error Resume Next
If Wscript.Arguments.Named("u") <> "" And Wscript.Arguments.Named("p") <> "" Then
    boolAlternate = True
    strUser = WScript.Arguments.Named("u")
    strPassword = WScript.Arguments.Named("p")
    WScript.Echo "Using alternate credentials..."
Else
	boolAlternate = False
	WScript.Echo "Using current credentials..."
End If
If WScript.Arguments.Named("s") = "" Then
	arrComputers = Array(".")
Else
	arrComputers = Split(WScript.Arguments.Named("s"), ",")
End If
 
dim lTotalSize as long
For Each strComputer In arrComputers
	strComputer = Trim(strComputer)
	If boolAlternate = True Then
		strNamespace = "root\cimv2"
		Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
		Set objWMIService = objwbemLocator.ConnectServer(strComputer, strNamespace, strUser, strPassword)
		objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
	Else
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where Description = 'Local Fixed Disk'",,48)
	For Each objItem in colItems
		WScript.Echo "Caption: " & objItem.Caption
		WScript.Echo "Size_1: " & objItem.Size
		Wscript.Echo "Size: " & ConvertSize(objItem.Size)
		Wscript.Echo "Serial: " & objItem.VolumeSerialNumber
		lTotalSize = lTotalSize + objItem.Size
	Next
Next
WScript.Echo "Total size = " & CStr(lTotalSize)

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:

Select allOpen in new window

 

by: richrumblePosted on 2009-10-20 at 05:54:36ID: 25613494

It doesn't seem to like the "dim lTotalSize as long", I commented it out and it ran, however the total is concatenating the totals from each computer. Using spaces in the script for the host's also causes it to reuse the first host:
Caption: C:
Size_1: 32201936896
Size: 30 GB
Serial: E85B1B6A
Caption: C:
Size_1: 32201936896
Size: 30 GB
Serial: E85B1B6A
Total size = 3220193689632201936896

===========Without spaces===========
C:\scripto>cscript test.vbs /s:10.1.1.120,10.1.1.60,10.1.1.52 /u:administrator /p:"**********"
Using alternate credentials...
Caption: C:
Size_1: 32201936896
Size: 30 GB
Serial: E85B1B6A
Caption: C:
Size_1: 79990812672
Size: 74.5 GB
Serial: B40E65D6
Caption: C:
Size_1: 39997927424
Size: 37.3 GB
Serial: BEBC616A
Caption: D:
Size_1: 39998976000
Size: 37.3 GB
Serial: B6395BFA
Total size = 32201936896799908126723999792742439998976000

I've managed to get the addition to work with the code below, however I'm sure it's bad form or hacky. I don't know vbs that well so this actually took me a long time :)

' Usage:
'cscript /nologo inventory.vbs /s:ip.ip.ip.ip  /u:user_name /p:password
' Multiple IP's should be comma seperated... IP's could also be dns-name(s) 
'-------------NO SPACES WHEN SPECIFYING MULTIPLE IP'S!!!-------------
' if no parametes are given local host and all parameters are run...
'Encrypts WMI Connections
Function ConvertSize(Size)
Do While InStr(Size,",") 'Remove commas from size
    CommaLocate = InStr(Size,",")
    Size = Mid(Size,1,CommaLocate - 1) & _
        Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
Loop
Suffix = " Bytes"
If Size >= 1024 Then suffix = " KB"
If Size >= 1048576 Then suffix = " MB"
If Size >= 1073741824 Then suffix = " GB"
If Size >= 1099511627776 Then suffix = " TB"
Select Case Suffix
    Case " KB" Size = Round(Size / 1024, 1)
    Case " MB" Size = Round(Size / 1048576, 1)
    Case " GB" Size = Round(Size / 1073741824, 1)
    Case " TB" Size = Round(Size / 1099511627776, 1)
End Select
ConvertSize = Size & Suffix
End Function
 
Const WbemAuthenticationLevelPktPrivacy = 6 
On Error Resume Next
If Wscript.Arguments.Named("u") <> "" And Wscript.Arguments.Named("p") <> "" Then
    boolAlternate = True
    strUser = WScript.Arguments.Named("u")
    strPassword = WScript.Arguments.Named("p")
    WScript.Echo "Using alternate credentials..."
Else
        boolAlternate = False
        WScript.Echo "Using current credentials..."
End If
If WScript.Arguments.Named("s") = "" Then
        arrComputers = Array("trim(.)")
Else
    arrComputers = Split(WScript.Arguments.Named("s"), ",")
End If
total = 0
Dim x
 
For Each strComputer In arrComputers
    If boolAlternate = True Then
        strNamespace = "root\cimv2"
        Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
        Set objWMIService = objwbemLocator.ConnectServer(strComputer, strNamespace, strUser, strPassword)
        objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
    Else
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where Description = 'Local Fixed Disk'",,48)
	If Err = 0 Then
		For Each objItem in colItems
			WScript.Echo "Caption: " & objItem.Caption
			WScript.Echo "Size: " & objItem.Size
			Wscript.Echo "Size: " & ConvertSize(objItem.Size)
			Wscript.Echo "Serial: " & objItem.VolumeSerialNumber
			ary=Array(objItem.Size)
			For Each x In ary  
			'WScript.Echo x  
			total = total + x
Next
		Next
	End If
	
	Err.Clear
Next
 
Wscript.echo "total: " & ConvertSize(total)
 
Which yeilds:
C:\scripto>cscript test.vbs /s:10.1.1.120,10.1.1.60,10.1.1.52 /u:administrator /p:"*********"
Using alternate credentials...
Caption: C:
Size: 32201936896
Size: 30 GB
Serial: E85B1B6A
Caption: C:
Size: 79990812672
Size: 74.5 GB
Serial: B40E65D6
Caption: C:
Size: 39997927424
Size: 37.3 GB
Serial: BEBC616A
Caption: D:
Size: 39998976000
Size: 37.3 GB
Serial: B6395BFA
total: 179 GB

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:

Select allOpen in new window

 

by: purplepomegranitePosted on 2009-10-20 at 06:06:25ID: 25613609

Sorry, I was in VB mode... in VBScript you shouldn't specify types, so the line that you removed should in fact be:

dim lTotalSize

Try the below modification - it is the better way of type-casting the size - it was concatenating the sizes because it was dealing with them as a string (in VB it wouldn't have due to the type definition for lTotalSize, but of course we can't do this in VBScript).

Not sure what you mean by the space in the host names though... Do you mean you'd rather use a space instead of a comma?  I assumed you would be using a comma followed by a space?  The below code won't work without the comma, but should remove any extra spaces.

' Usage:
'cscript /nologo inventory.vbs /s:ip.ip.ip.ip  /u:user_name /p:password
' Multiple IP's should be comma seperated... IP's could also be dns-name(s)
' if no parametes are given local host and all parameters are run...
'Encrypts WMI Connections
Function ConvertSize(Size)
        Do While InStr(Size,",") 'Remove commas from size
                CommaLocate = InStr(Size,",")
                Size = Mid(Size,1,CommaLocate - 1) & _
                Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
        Loop
        Suffix = " Bytes"
        If Size >= 1024 Then suffix = " KB"
        If Size >= 1048576 Then suffix = " MB"
        If Size >= 1073741824 Then suffix = " GB"
        If Size >= 1099511627776 Then suffix = " TB"
        Select Case Suffix
                Case " KB" Size = Round(Size / 1024, 1)
                Case " MB" Size = Round(Size / 1048576, 1)
                Case " GB" Size = Round(Size / 1073741824, 1)
                Case " TB" Size = Round(Size / 1099511627776, 1)
        End Select
        ConvertSize = Size & Suffix
End Function
 
Const WbemAuthenticationLevelPktPrivacy = 6 
On Error Resume Next
If Wscript.Arguments.Named("u") <> "" And Wscript.Arguments.Named("p") <> "" Then
    boolAlternate = True
    strUser = WScript.Arguments.Named("u")
    strPassword = WScript.Arguments.Named("p")
    WScript.Echo "Using alternate credentials..."
Else
        boolAlternate = False
        WScript.Echo "Using current credentials..."
End If
If WScript.Arguments.Named("s") = "" Then
        arrComputers = Array(".")
Else
        arrComputers = Split(WScript.Arguments.Named("s"), ",")
End If
 
dim lTotalSize as long
For Each strComputer In arrComputers
        strComputer = Trim(strComputer)
        If boolAlternate = True Then
                strNamespace = "root\cimv2"
                Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
                Set objWMIService = objwbemLocator.ConnectServer(strComputer, strNamespace, strUser, strPassword)
                objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
        Else
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where Description = 'Local Fixed Disk'",,48)
        For Each objItem in colItems
                WScript.Echo "Caption: " & objItem.Caption
                WScript.Echo "Size_1: " & objItem.Size
                Wscript.Echo "Size: " & ConvertSize(objItem.Size)
                Wscript.Echo "Serial: " & objItem.VolumeSerialNumber
                lTotalSize = lTotalSize + objItem.Size
        Next
Next
WScript.Echo "Total size = " & CStr(lTotalSize)

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:

Select allOpen in new window

 

by: purplepomegranitePosted on 2009-10-20 at 06:06:53ID: 25613614

Sorry, forgot to amend the lTotalSize line... corrected below.

' Usage:
'cscript /nologo inventory.vbs /s:ip.ip.ip.ip  /u:user_name /p:password
' Multiple IP's should be comma seperated... IP's could also be dns-name(s)
' if no parametes are given local host and all parameters are run...
'Encrypts WMI Connections
Function ConvertSize(Size)
        Do While InStr(Size,",") 'Remove commas from size
                CommaLocate = InStr(Size,",")
                Size = Mid(Size,1,CommaLocate - 1) & _
                Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
        Loop
        Suffix = " Bytes"
        If Size >= 1024 Then suffix = " KB"
        If Size >= 1048576 Then suffix = " MB"
        If Size >= 1073741824 Then suffix = " GB"
        If Size >= 1099511627776 Then suffix = " TB"
        Select Case Suffix
                Case " KB" Size = Round(Size / 1024, 1)
                Case " MB" Size = Round(Size / 1048576, 1)
                Case " GB" Size = Round(Size / 1073741824, 1)
                Case " TB" Size = Round(Size / 1099511627776, 1)
        End Select
        ConvertSize = Size & Suffix
End Function
 
Const WbemAuthenticationLevelPktPrivacy = 6 
On Error Resume Next
If Wscript.Arguments.Named("u") <> "" And Wscript.Arguments.Named("p") <> "" Then
    boolAlternate = True
    strUser = WScript.Arguments.Named("u")
    strPassword = WScript.Arguments.Named("p")
    WScript.Echo "Using alternate credentials..."
Else
        boolAlternate = False
        WScript.Echo "Using current credentials..."
End If
If WScript.Arguments.Named("s") = "" Then
        arrComputers = Array(".")
Else
        arrComputers = Split(WScript.Arguments.Named("s"), ",")
End If
 
dim lTotalSize
For Each strComputer In arrComputers
        strComputer = Trim(strComputer)
        If boolAlternate = True Then
                strNamespace = "root\cimv2"
                Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
                Set objWMIService = objwbemLocator.ConnectServer(strComputer, strNamespace, strUser, strPassword)
                objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
        Else
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where Description = 'Local Fixed Disk'",,48)
        For Each objItem in colItems
                WScript.Echo "Caption: " & objItem.Caption
                WScript.Echo "Size_1: " & objItem.Size
                Wscript.Echo "Size: " & ConvertSize(objItem.Size)
                Wscript.Echo "Serial: " & objItem.VolumeSerialNumber
                lTotalSize = lTotalSize + CLng(objItem.Size)
        Next
Next
WScript.Echo "Total size = " & CStr(lTotalSize)

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:

Select allOpen in new window

 

by: richrumblePosted on 2009-10-20 at 06:27:58ID: 25613840

The when using 3 host's, 10.1.1.120, 10.1.1.60, 10.1.1.52 with spaces after the comma's it will only connect to "two" of them, but it's actually just repeating the first host twice, I've verified using a packet dump that it only connects to one, one time, but repeats the values...
Caption: C:
Size_1: 32201936896
Size: 30 GB
Serial: E85B1B6A
Caption: C:
Size_1: 32201936896
Size: 30 GB
Serial: E85B1B6A
Total size =

The total size is also not outputting anything right now. With no spaces the script connects to all 3 hosts, but the size still doesn't add up, I tried adding some echo's in to see what I could see, lTotalSize doesn't seem to populate. My code works for the addiition however I have tried to use trim (l and rtrim) in various places in the "S" arg but it too doesn't allow comma + spaces.
-rich

 

by: purplepomegranitePosted on 2009-10-20 at 06:36:21ID: 25613927

Ah... you've just hit the nail on the head, as it were.  I've just realised why the argument isn't parsing properly.  If you add spaces around the commas, then the space means that the next part is another argument (i.e. the s parameter is finished).

To include spaces, you'd need to quote the parameter, e.g.
/s:"1.2.3.4, 1.2.3.5, 1.2.3.6"

That should work.

For the size issue, try the amended code below.  I hadn't initialised lTotalSize, which means VB may be handling it strangely.  If it doesn't work, let me know and I'll try and run some tests (sorry, haven't actually been able to test anything I have posted so far).

' Usage:
'cscript /nologo inventory.vbs /s:ip.ip.ip.ip  /u:user_name /p:password
' Multiple IP's should be comma seperated... IP's could also be dns-name(s)
' if no parametes are given local host and all parameters are run...
'Encrypts WMI Connections
Function ConvertSize(Size)
        Do While InStr(Size,",") 'Remove commas from size
                CommaLocate = InStr(Size,",")
                Size = Mid(Size,1,CommaLocate - 1) & _
                Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate)
        Loop
        Suffix = " Bytes"
        If Size >= 1024 Then suffix = " KB"
        If Size >= 1048576 Then suffix = " MB"
        If Size >= 1073741824 Then suffix = " GB"
        If Size >= 1099511627776 Then suffix = " TB"
        Select Case Suffix
                Case " KB" Size = Round(Size / 1024, 1)
                Case " MB" Size = Round(Size / 1048576, 1)
                Case " GB" Size = Round(Size / 1073741824, 1)
                Case " TB" Size = Round(Size / 1099511627776, 1)
        End Select
        ConvertSize = Size & Suffix
End Function
 
Const WbemAuthenticationLevelPktPrivacy = 6 
On Error Resume Next
If Wscript.Arguments.Named("u") <> "" And Wscript.Arguments.Named("p") <> "" Then
    boolAlternate = True
    strUser = WScript.Arguments.Named("u")
    strPassword = WScript.Arguments.Named("p")
    WScript.Echo "Using alternate credentials..."
Else
        boolAlternate = False
        WScript.Echo "Using current credentials..."
End If
If WScript.Arguments.Named("s") = "" Then
        arrComputers = Array(".")
Else
        arrComputers = Split(WScript.Arguments.Named("s"), ",")
End If
 
dim lTotalSize
lTotalSize = 0
For Each strComputer In arrComputers
        strComputer = Trim(strComputer)
        If boolAlternate = True Then
                strNamespace = "root\cimv2"
                Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
                Set objWMIService = objwbemLocator.ConnectServer(strComputer, strNamespace, strUser, strPassword)
                objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
        Else
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        End If
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where Description = 'Local Fixed Disk'",,48)
        For Each objItem in colItems
                WScript.Echo "Caption: " & objItem.Caption
                WScript.Echo "Size_1: " & objItem.Size
                Wscript.Echo "Size: " & ConvertSize(objItem.Size)
                Wscript.Echo "Serial: " & objItem.VolumeSerialNumber
                lTotalSize = lTotalSize + CLng(objItem.Size)
        Next
Next
WScript.Echo "Total size = " & CStr(lTotalSize)

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:

Select allOpen in new window

 

by: richrumblePosted on 2009-10-20 at 07:02:07ID: 25614199

Still nothing, I added:
lTotalSize = lTotalSize + CLng(objItem.Size)
Wscript.Echo lTotalSize
and it outputs 0 for each host and to total.
If my addition function seems to work ok, I'm just not sure it's proper. I'd really like to add more error correction to the code, but since there isn't much to do on the input side, and not using spaces isn't that big a deal, I might just settle for my code unless it will have significant draw backs with larger pools of hosts?

total = 0
Dim x
...
For Each x In ary  
total = total + x
...
Wscript.echo "total: " & total

 

by: purplepomegranitePosted on 2009-10-20 at 07:21:39ID: 25614424

The other (and possibly better) option for listing the hosts would be to read them from a text file - then you could just pass the filename as the /s parameter.  Normal format would be one host per line in the text file.  It would be quite easy to modify the script this way, and also means that you won't hit any command-line limits (i.e. if you have a large number of hosts, you probably won't fit them all in before the commandline reaches its character limit).

I've checked the size problem - it was due to the size being returned as a string, with commas (as it is a WMI uint64).  I've modified the code below to deal correctly with this.

' Usage:
'cscript /nologo inventory.vbs /s:ip.ip.ip.ip  /u:user_name /p:password
' Multiple IP's should be comma seperated... IP's could also be dns-name(s)
' if no parametes are given local host and all parameters are run...
'Encrypts WMI Connections
Function ConvertSize(Size)
 
	Size=Replace(Size, ",", "") ' Remove commas
	Suffix = " Bytes"
	If Size >= 1024 Then suffix = " KB"
	If Size >= 1048576 Then suffix = " MB"
	If Size >= 1073741824 Then suffix = " GB"
	If Size >= 1099511627776 Then suffix = " TB"
	Select Case Suffix
	Case " KB" Size = Round(Size / 1024, 1)
	Case " MB" Size = Round(Size / 1048576, 1)
	Case " GB" Size = Round(Size / 1073741824, 1)
	Case " TB" Size = Round(Size / 1099511627776, 1)
	End Select
	ConvertSize = Size & Suffix
End Function
 
Const WbemAuthenticationLevelPktPrivacy = 6 
On Error Resume Next
If Wscript.Arguments.Named("u") <> "" And Wscript.Arguments.Named("p") <> "" Then
	boolAlternate = True
	strUser = WScript.Arguments.Named("u")
	strPassword = WScript.Arguments.Named("p")
	WScript.Echo "Using alternate credentials..."
Else
	boolAlternate = False
	WScript.Echo "Using current credentials..."
End If
If WScript.Arguments.Named("s") = "" Then
	arrComputers = Array(".")
Else
	arrComputers = Split(WScript.Arguments.Named("s"), ",")
End If
 
dim lTotalSize
For Each strComputer In arrComputers
	lTotalSize = 0
	strComputer = Trim(strComputer)
	If boolAlternate = True Then
		strNamespace = "root\cimv2"
		Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
		Set objWMIService = objwbemLocator.ConnectServer(strComputer, strNamespace, strUser, strPassword)
		objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
	Else
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	End If
	Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where Description = 'Local Fixed Disk'",,48)
	For Each objItem in colItems
		WScript.Echo "Caption: " & objItem.Caption
		WScript.Echo "Size_1: " & objItem.Size
		Wscript.Echo "Size: " & ConvertSize(objItem.Size)
		Wscript.Echo "Serial: " & objItem.VolumeSerialNumber
		lSize = CDbl(Replace(objItem.Size, ",", ""))
		lTotalSize = lTotalSize + lSize
	Next
Next
WScript.Echo "Total size = " & CStr(lTotalSize) & " (" & ConvertSize(lTotalSize) & ")"

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:

Select allOpen in new window

 

by: richrumblePosted on 2009-10-20 at 13:33:04ID: 31643038

Works now!

 

by: richrumblePosted on 2009-10-20 at 13:34:03ID: 25618262

That works, thanks! I'm not all that hung up on the comma separated values so this works well thanks again!
-rich

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...