I can't get this to output how I want

S

Steven Platt

What is wrong with this thing? Maybe I am too used to PHP.

Const EXAMPLE_BYTES = 32210195968
Const BYTES_IN_MEG = 1048576
Const MEGS_IN_GIG = 1024

WScript.Echo EXAMPLE_BYTES & " is " & bytesToGigs( EXAMPLE_BYTES ) & " in
gigs"

Function bytesToGigs( someNumber )
WScript.Echo (someNumber / BYTES_IN_MEG) / MEGS_IN_GIG
End Function

I want the output to be something like:
32210195968 is 29.9980826377869 in gigs

Right now I get output like:
29.9980826377869
32210195968 is in gigs

Weird, huh? Thanks for the replies, guys.

-Steven-
 
T

Torgeir Bakken \(MVP\)

Steven said:
What is wrong with this thing? Maybe I am too used to PHP.

Const EXAMPLE_BYTES = 32210195968
Const BYTES_IN_MEG = 1048576
Const MEGS_IN_GIG = 1024

WScript.Echo EXAMPLE_BYTES & " is " & bytesToGigs( EXAMPLE_BYTES ) & " in
gigs"

Function bytesToGigs( someNumber )
WScript.Echo (someNumber / BYTES_IN_MEG) / MEGS_IN_GIG
End Function

I want the output to be something like:
32210195968 is 29.9980826377869 in gigs

Right now I get output like:
29.9980826377869
32210195968 is in gigs

Weird, huh? Thanks for the replies, guys.
Hi,

You need to assign the value to the function name inside the
function to make the function return anything:

Function bytesToGigs( someNumber )
bytesToGigs = (someNumber / BYTES_IN_MEG) / MEGS_IN_GIG
End Function
 
M

Matthias Jarling

Hi Steven,

Steven Platt said:
What is wrong with this thing? Maybe I am too used to PHP.

Const EXAMPLE_BYTES = 32210195968
Const BYTES_IN_MEG = 1048576
Const MEGS_IN_GIG = 1024

WScript.Echo EXAMPLE_BYTES & " is " & bytesToGigs( EXAMPLE_BYTES ) & " in
gigs"

Function bytesToGigs( someNumber )
WScript.Echo (someNumber / BYTES_IN_MEG) / MEGS_IN_GIG
End Function

Your function does not return a value but displays a message itself; that's
why u get two message boxes.

Function bytesToGigs( someNumber )
bytesToGigs = someNumber / BYTES_IN_MEG / MEGS_IN_GIG
End Function
 
S

Steven Platt

Thanks for the reply, fellas. Now I have another problem. I can't seem to
run this script on remote computers. I can run it locally just fine and it
issues the output correctly. When I try to run it on a remote computer
sometimes I get:

C:\Documents and Settings\user\My
Documents\scripts\Inventory_mapped_drives.vbs(11, 2) Microsoft VBScript
runtime error: The remote server machine does not exist or is unavailable:
'GetObject'

But I ping the computer in question and it replies. Then, other times I
just get the first line and it doesn't output the DriveType=4 information.
Here is my code:

Const BYTES_IN_MEG = 1048576
Const MEGS_IN_GIG = 1024

arrComputers = Array("SALLY") '. works but remote computers don't :S
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo
"================================================================="
WScript.Echo "Information about Network Connections on: " & strComputer
WScript.Echo
"================================================================="

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk
WHERE DriveType=4")

For Each objItem In colItems
WScript.Echo "Description: " & objItem.Description
WScript.Echo "DeviceID: " & objItem.DeviceID
WScript.Echo "ProviderName: " & objItem.ProviderName
WScript.Echo "FileSystem: " & objItem.FileSystem
WScript.Echo "FreeSpace: " & BytesToGigs( objItem.FreeSpace ) & "G"
WScript.Echo
Next
Next

Function BytesToGigs( sizeInBytes )
BytesToGigs = Round( ((sizeInBytes / BYTES_IN_MEG) / MEGS_IN_GIG), 2 )
End Function

Thanks for the help.

-Steven-
 
S

sbconway

Try using this connection string instead:

Const BYTES_IN_MEG = 1048576
Const MEGS_IN_GIG = 1024
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
UserName = ""
Password = ""


arrComputers = Array("sally") '. works but remote computers don't :S
For Each strComputer In arrComputers
WScript.Echo
"=============================­==============================­======"

WScript.Echo "Information about Network Connections on: " &
strComputer
WScript.Echo
"=============================­==============================­======"


Set objWMIService =
SWBemlocator.ConnectServer(strComputer,"\root\CIMV2",UserName,Password)
Set colItems = objWMIService.ExecQuery("Select * from
Win32_LogicalDisk WHERE DriveType=4",,48)

For Each objItem In colItems
WScript.Echo "Description: " & objItem.Description
WScript.Echo "DeviceID: " & objItem.DeviceID
WScript.Echo "ProviderName: " & objItem.ProviderName
WScript.Echo "FileSystem: " & objItem.FileSystem
WScript.Echo "FreeSpace: " & BytesToGigs( objItem.FreeSpace ) & "G"
WScript.Echo
Next
Next


Function BytesToGigs( sizeInBytes )
BytesToGigs = Round( ((sizeInBytes / BYTES_IN_MEG) / MEGS_IN_GIG), 2 )

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top