Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows 2000 resource kit (logmeminfo.vbs)

Status
Not open for further replies.

mis25

MIS
Jul 16, 2001
11
0
0
US
I am trying to create a script that will let me know what kind of physical memory users have on their system. Would anyone be kind enough to post the logmeminfo.vbs file that is on the windows 2000 resource kit. You can just put the info in the reply post. Thanks, in advance.
 
Here you go. If this does not help email me and I will send it to you.:
'********************************************************************
'*
'* File: LogMemInfo.Vbs
'* Created: March 1999
'* Version: 1.0
'*
'* Main Function: Obtains the logical memory configuration of a machine.
'*
'* LogMemInfo.vbs [/S <server>] [/U <username>] [/W <password>]
'* [/O <outputfile>]
'*
'* Copyright (C) 1999 Microsoft Corporation
'*
'********************************************************************

OPTION EXPLICIT

'Define constants
CONST CONST_ERROR = 0
CONST CONST_WSCRIPT = 1
CONST CONST_CSCRIPT = 2
CONST CONST_SHOW_USAGE = 3
CONST CONST_PROCEED = 4

'Declare variables
Dim intOpMode, i
Dim strServer, strUserName, strPassword, strOutputFile

'Make sure the host is csript, if not then abort
VerifyHostIsCscript()

'Parse the command line
intOpMode = intParseCmdLine(strServer , _
strUserName , _
strPassword , _
strOutputFile )


Select Case intOpMode

Case CONST_SHOW_USAGE
Call ShowUsage()

Case CONST_PROCEED
Call LogMemInfo(strServer , _
strOutputFile , _
strUserName , _
strPassword )

Case CONST_ERROR
'Do Nothing

Case Else 'Default -- should never happen
Call Wscript.Echo(&quot;Error occurred in passing parameters.&quot;)

End Select

'********************************************************************
'* End of Script
'********************************************************************

'********************************************************************
'*
'* Sub LogMemInfo()
'*
'* Purpose: Obtains the logical memory configuration of a machine.
'*
'* Input: strServer a machine name
'* strOutputFile an output file name
'* strUserName the current user's name
'* strPassword the current user's password
'*
'* Output: Results are either printed on screen or saved in strOutputFile.
'*
'********************************************************************
Private Sub LogMemInfo(strServer, strOutputFile, strUserName, strPassword)



ON ERROR RESUME NEXT

Dim objFileSystem, objOutputFile, objService, objMem, objWshNet
Dim strQuery, strMessage, strCat

'Open a text file for output if the file is requested
If Not IsEmpty(strOutputFile) Then
If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
Call Wscript.Echo (&quot;Could not open an output file.&quot;)
Exit Sub
End If
End If

'Establish a connection with the server.
If blnConnect(&quot;root\cimv2&quot; , _
strUserName , _
strPassword , _
strServer , _
objService ) Then
Call Wscript.Echo(&quot;&quot;)
Call Wscript.Echo(&quot;Please check the server name, &quot; _
& &quot;credentials and WBEM Core.&quot;)
Exit Sub
End If

'Get the logical memory configuration
Set objMem = objService.Get(&quot;Win32_LogicalMemoryConfiguration=&quot;&quot;&quot; _
& &quot;LogicalMemoryConfiguration&quot;&quot;&quot;)
If Err.Number Then
Wscript.Echo &quot;Error 0x&quot; & CStr(Hex(Err.Number)) & _
&quot; occurred getting the memory configuration.&quot;
If Err.Description <> &quot;&quot; Then
Wscript.Echo &quot;Error description: &quot; & Err.Description & &quot;.&quot;
End If
Err.Clear
Exit Sub
End If

Call WriteLine(&quot;Memory Configuration (Kb)&quot;, objOutputFile)

strCat = &quot;Machine&quot;
strCat = strPackString(strCat, 20, 1, 1)
strMessage = strMessage + strCat

strCat = &quot;Tot Physical&quot;
strCat = strPackString(strCat, 15, 1, 1)
strMessage = strMessage + strCat

strCat = &quot;Tot Virtual&quot;
strCat = strPackString(strCat, 15, 1, 1)
strMessage = strMessage + strCat

strCat = &quot;Pagefile Space&quot;
strCat = strPackString(strCat, 15, 1, 1)
strMessage = strMessage + strCat

strCat = &quot;Avail Virt&quot;
strCat = strPackString(strCat, 15, 1, 1)
strMessage = strMessage + strCat
Call WriteLine(strMessage, objOutputFile)

strMessage = Empty

If IsEmpty(strServer) Then
Set objWshNet = CreateObject(&quot;Wscript.Network&quot;)
strServer = objWshNet.ComputerName
End If

strMessage = strMessage + strPackString _
(strServer, 20, 1, 1)

strMessage = strMessage + strPackString _
(strInsertCommas(objMem.TotalPhysicalMemory), 15, 1, 1)

strMessage = strMessage + strPackString _
(strInsertCommas(objMem.TotalVirtualMemory), 15, 1, 1)

strMessage = strMessage + strPackString _
(strInsertCommas(objMem.TotalPagefileSpace), 15, 1, 1)

strMessage = strMessage + strPackString _
(strInsertCommas(objMem.AvailableVirtualMemory), 15, 1, 1)

Call WriteLine(strMessage, objOutputFile)

If IsObject(objOutputFile) Then
objOutputFile.Close
Call Wscript.Echo (&quot;Results are saved in file &quot; & strOutputFile & &quot;.&quot;)
End If
End Sub

'********************************************************************
'*
'* Function intParseCmdLine()
'*
'* Purpose: Parses the command line.
'* Input:
'*
'* Output: strServer a remote server (&quot;&quot; = local server&quot;)
'* strUserName the current user's name
'* strPassword the current user's password
'* strOutputFile an output file name
'*
'********************************************************************
Private Function intParseCmdLine( ByRef strServer, _
ByRef strUserName, _
ByRef strPassword, _
ByRef strOutputFile )


ON ERROR RESUME NEXT

Dim strFlag
Dim intState, intArgIter
Dim objFileSystem

If Wscript.Arguments.Count > 0 Then
strFlag = Wscript.arguments.Item(0)
End If

If IsEmpty(strFlag) Then 'No arguments have been received
intParseCmdLine = CONST_PROCEED
Exit Function
End If

'Check if the user is asking for help or is just confused
If (strFlag=&quot;help&quot;) OR (strFlag=&quot;/h&quot;) OR (strFlag=&quot;\h&quot;) OR (strFlag=&quot;-h&quot;) _
OR (strFlag = &quot;\?&quot;) OR (strFlag = &quot;/?&quot;) OR (strFlag = &quot;?&quot;) _
OR (strFlag=&quot;h&quot;) Then
intParseCmdLine = CONST_SHOW_USAGE
Exit Function
End If

'Retrieve the command line and set appropriate variables
intArgIter = 0
Do While intArgIter <= Wscript.arguments.Count - 1
Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)

Case &quot;/s&quot;
If Not blnGetArg(&quot;Server&quot;, strServer, intArgIter) Then
intParseCmdLine = CONST_ERROR
Exit Function
End If
intArgIter = intArgIter + 1

Case &quot;/o&quot;
If Not blnGetArg(&quot;Output File&quot;, strOutputFile, intArgIter) Then
intParseCmdLine = CONST_ERROR
Exit Function
End If
intArgIter = intArgIter + 1

Case &quot;/u&quot;
If Not blnGetArg(&quot;User Name&quot;, strUserName, intArgIter) Then
intParseCmdLine = CONST_ERROR
Exit Function
End If
intArgIter = intArgIter + 1

Case &quot;/w&quot;
If Not blnGetArg(&quot;User Password&quot;, strPassword, intArgIter) Then
intParseCmdLine = CONST_ERROR
Exit Function
End If
intArgIter = intArgIter + 1

Case Else 'We shouldn't get here
Call Wscript.Echo(&quot;Invalid or misplaced parameter: &quot; _
& Wscript.arguments.Item(intArgIter) & vbCRLF _
& &quot;Please check the input and try again,&quot; & vbCRLF _
& &quot;or invoke with '/?' for help with the syntax.&quot;)
Wscript.Quit

End Select

Loop '** intArgIter <= Wscript.arguments.Count - 1

If IsEmpty(intParseCmdLine) Then _
intParseCmdLine = CONST_PROCEED

End Function

'********************************************************************
'*
'* Sub ShowUsage()
'*
'* Purpose: Shows the correct usage to the user.
'*
'* Input: None
'*
'* Output: Help messages are displayed on screen.
'*
'********************************************************************
Private Sub ShowUsage()

Wscript.Echo &quot;&quot;
Wscript.Echo &quot;Obtains the logical memory configuration of a machine.&quot;
Wscript.Echo &quot;&quot;
Wscript.Echo &quot;SYNTAX:&quot;
Wscript.Echo &quot; LogMemInfo.vbs [/S <server>] [/U <username>]&quot; _
& &quot; [/W <password>]&quot;
Wscript.Echo &quot; [/O <outputfile>]&quot;
Wscript.Echo &quot;&quot;
Wscript.Echo &quot;PARAMETER SPECIFIERS:&quot;
Wscript.Echo &quot; server A machine name.&quot;
Wscript.Echo &quot; username The current user's name.&quot;
Wscript.Echo &quot; password Password of the current user.&quot;
Wscript.Echo &quot; outputfile The output file name.&quot;
Wscript.Echo &quot;&quot;
Wscript.Echo &quot;EXAMPLE:&quot;
Wscript.Echo &quot;1. cscript LogMemInfo.vbs&quot;
Wscript.Echo &quot; Obtains the logical memory configuration for &quot; _
& &quot;the current machine.&quot;
Wscript.Echo &quot;2. cscript LogMemInfo.vbs /S MyMachine2&quot;
Wscript.Echo &quot; Obtains the logical memory configuration for &quot; _
& &quot;the machine MyMachine2.&quot;

End Sub

'********************************************************************
'* General Routines
'********************************************************************

'********************************************************************
'*
'* Function strInsertCommas()
'*
'* Purpose: Puts commas into an interger to make it more readable.
'*
'* Input: intValue an integer
'*
'* Output: strInsertCommas A String
'*
'********************************************************************
Function strInsertCommas(intValue)

Dim IntPlace

strInsertCommas = &quot;&quot;
intPlace = 0
For I = len(intValue) to 1 Step - 1
strInsertCommas = Mid(intValue,I,1) & strInsertCommas
intPlace = intPlace + 1
If intPlace = 3 then
strInsertCommas = &quot;,&quot; & strInsertCommas
intPlace = 0
End If
Next
If Left(strInsertCommas,1) = &quot;,&quot; then
strInsertCommas = Right(strInsertCommas, len(strInsertCommas) -1)
End If

End Function

'********************************************************************
'*
'* Function strPackString()
'*
'* Purpose: Attaches spaces to a string to increase the length to intWidth.
'*
'* Input: strString a string
'* intWidth the intended length of the string
'* blnAfter Should spaces be added after the string?
'* blnTruncate specifies whether to truncate the string or not if
'* the string length is longer than intWidth
'*
'* Output: strPackString is returned as the packed string.
'*
'********************************************************************
Private Function strPackString( ByVal strString, _
ByVal intWidth, _
ByVal blnAfter, _
ByVal blnTruncate)

ON ERROR RESUME NEXT

intWidth = CInt(intWidth)
blnAfter = CBool(blnAfter)
blnTruncate = CBool(blnTruncate)

If Err.Number Then
Call Wscript.Echo (&quot;Argument type is incorrect!&quot;)
Err.Clear
Wscript.Quit
End If

If IsNull(strString) Then
strPackString = &quot;null&quot; & Space(intWidth-4)
Exit Function
End If

strString = CStr(strString)
If Err.Number Then
Call Wscript.Echo (&quot;Argument type is incorrect!&quot;)
Err.Clear
Wscript.Quit
End If

If intWidth > Len(strString) Then
If blnAfter Then
strPackString = strString & Space(intWidth-Len(strString))
Else
strPackString = Space(intWidth-Len(strString)) & strString & &quot; &quot;
End If
Else
If blnTruncate Then
strPackString = Left(strString, intWidth-1) & &quot; &quot;
Else
strPackString = strString & &quot; &quot;
End If
End If

End Function

'********************************************************************
'*
'* Function blnGetArg()
'*
'* Purpose: Helper to intParseCmdLine()
'*
'* Usage:
'*
'* Case &quot;/s&quot;
'* blnGetArg (&quot;server name&quot;, strServer, intArgIter)
'*
'********************************************************************
Private Function blnGetArg ( ByVal StrVarName, _
ByRef strVar, _
ByRef intArgIter)

blnGetArg = False 'failure, changed to True upon successful completion

If Len(Wscript.Arguments(intArgIter)) > 2 then
If Mid(Wscript.Arguments(intArgIter),3,1) = &quot;:&quot; then
If Len(Wscript.Arguments(intArgIter)) > 3 then
strVar = Right(Wscript.Arguments(intArgIter), _
Len(Wscript.Arguments(intArgIter)) - 3)
blnGetArg = True
Exit Function
Else
intArgIter = intArgIter + 1
If intArgIter > (Wscript.Arguments.Count - 1) Then
Call Wscript.Echo( &quot;Invalid &quot; & StrVarName & &quot;.&quot;)
Call Wscript.Echo( &quot;Please check the input and try again.&quot;)
Exit Function
End If

strVar = Wscript.Arguments.Item(intArgIter)
If Err.Number Then
Call Wscript.Echo( &quot;Invalid &quot; & StrVarName & &quot;.&quot;)
Call Wscript.Echo( &quot;Please check the input and try again.&quot;)
Exit Function
End If

If InStr(strVar, &quot;/&quot;) Then
Call Wscript.Echo( &quot;Invalid &quot; & StrVarName)
Call Wscript.Echo( &quot;Please check the input and try again.&quot;)
Exit Function
End If

blnGetArg = True 'success
End If
Else
strVar = Right(Wscript.Arguments(intArgIter), _
Len(Wscript.Arguments(intArgIter)) - 2)
blnGetArg = True 'success
Exit Function
End If
Else
intArgIter = intArgIter + 1
If intArgIter > (Wscript.Arguments.Count - 1) Then
Call Wscript.Echo( &quot;Invalid &quot; & StrVarName & &quot;.&quot;)
Call Wscript.Echo( &quot;Please check the input and try again.&quot;)
Exit Function
End If

strVar = Wscript.Arguments.Item(intArgIter)
If Err.Number Then
Call Wscript.Echo( &quot;Invalid &quot; & StrVarName & &quot;.&quot;)
Call Wscript.Echo( &quot;Please check the input and try again.&quot;)
Exit Function
End If

If InStr(strVar, &quot;/&quot;) Then
Call Wscript.Echo( &quot;Invalid &quot; & StrVarName)
Call Wscript.Echo( &quot;Please check the input and try again.&quot;)
Exit Function
End If
blnGetArg = True 'success
End If
End Function

'********************************************************************
'*
'* Function blnConnect()
'*
'* Purpose: Connects to machine strServer.
'*
'* Input: strServer a machine name
'* strNameSpace a namespace
'* strUserName name of the current user
'* strPassword password of the current user
'*
'* Output: objService is returned as a service object.
'* strServer is set to local host if left unspecified
'*
'********************************************************************
Private Function blnConnect(ByVal strNameSpace, _
ByVal strUserName, _
ByVal strPassword, _
ByRef strServer, _
ByRef objService)

ON ERROR RESUME NEXT

Dim objLocator, objWshNet

blnConnect = False 'There is no error.

'Create Locator object to connect to remote CIM object manager
Set objLocator = CreateObject(&quot;WbemScripting.SWbemLocator&quot;)
If Err.Number then
Call Wscript.Echo( &quot;Error 0x&quot; & CStr(Hex(Err.Number)) & _
&quot; occurred in creating a locator object.&quot; )
If Err.Description <> &quot;&quot; Then
Call Wscript.Echo( &quot;Error description: &quot; & Err.Description & &quot;.&quot; )
End If
Err.Clear
blnConnect = True 'An error occurred
Exit Function
End If

'Connect to the namespace which is either local or remote
Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
strUserName, strPassword)
ObjService.Security_.impersonationlevel = 3
If Err.Number then
Call Wscript.Echo( &quot;Error 0x&quot; & CStr(Hex(Err.Number)) & _
&quot; occurred in connecting to server &quot; _
& strServer & &quot;.&quot;)
If Err.Description <> &quot;&quot; Then
Call Wscript.Echo( &quot;Error description: &quot; & Err.Description & &quot;.&quot; )
End If
Err.Clear
blnConnect = True 'An error occurred
End If

'Get the current server's name if left unspecified
If IsEmpty(strServer) Then
Set objWshNet = CreateObject(&quot;Wscript.Network&quot;)
strServer = objWshNet.ComputerName
End If

End Function

'********************************************************************
'*
'* Sub VerifyHostIsCscript()
'*
'* Purpose: Determines which program is used to run this script.
'*
'* Input: None
'*
'* Output: If host is not cscript, then an error message is printed
'* and the script is aborted.
'*
'********************************************************************
Sub VerifyHostIsCscript()

ON ERROR RESUME NEXT

Dim strFullName, strCommand, i, j, intStatus

strFullName = WScript.FullName

If Err.Number then
Call Wscript.Echo( &quot;Error 0x&quot; & CStr(Hex(Err.Number)) & &quot; occurred.&quot; )
If Err.Description <> &quot;&quot; Then
Call Wscript.Echo( &quot;Error description: &quot; & Err.Description & &quot;.&quot; )
End If
intStatus = CONST_ERROR
End If

i = InStr(1, strFullName, &quot;.exe&quot;, 1)
If i = 0 Then
intStatus = CONST_ERROR
Else
j = InStrRev(strFullName, &quot;\&quot;, i, 1)
If j = 0 Then
intStatus = CONST_ERROR
Else
strCommand = Mid(strFullName, j+1, i-j-1)
Select Case LCase(strCommand)
Case &quot;cscript&quot;
intStatus = CONST_CSCRIPT
Case &quot;wscript&quot;
intStatus = CONST_WSCRIPT
Case Else 'should never happen
Call Wscript.Echo( &quot;An unexpected program was used to &quot; _
& &quot;run this script.&quot; )
Call Wscript.Echo( &quot;Only CScript.Exe or WScript.Exe can &quot; _
& &quot;be used to run this script.&quot; )
intStatus = CONST_ERROR
End Select
End If
End If

If intStatus <> CONST_CSCRIPT Then
Call WScript.Echo( &quot;Please run this script using CScript.&quot; & vbCRLF & _
&quot;This can be achieved by&quot; & vbCRLF & _
&quot;1. Using &quot;&quot;CScript LogMemInfo.vbs arguments&quot;&quot; for Windows 95/98 or&quot; _
& vbCRLF & &quot;2. Changing the default Windows Scripting Host &quot; _
& &quot;setting to CScript&quot; & vbCRLF & &quot; using &quot;&quot;CScript &quot; _
& &quot;//H:CScript //S&quot;&quot; and running the script using&quot; & vbCRLF & _
&quot; &quot;&quot;LogMemInfo.vbs arguments&quot;&quot; for Windows NT/2000.&quot; )
WScript.Quit
End If

End Sub

'********************************************************************
'*
'* Sub WriteLine()
'* Purpose: Writes a text line either to a file or on screen.
'* Input: strMessage the string to print
'* objFile an output file object
'* Output: strMessage is either displayed on screen or written to a file.
'*
'********************************************************************
Sub WriteLine(ByVal strMessage, ByVal objFile)

On Error Resume Next
If IsObject(objFile) then 'objFile should be a file object
objFile.WriteLine strMessage
Else
Call Wscript.Echo( strMessage )
End If

End Sub

'********************************************************************
'*
'* Function blnErrorOccurred()
'*
'* Purpose: Reports error with a string saying what the error occurred in.
'*
'* Input: strIn string saying what the error occurred in.
'*
'* Output: displayed on screen
'*
'********************************************************************
Private Function blnErrorOccurred (ByVal strIn)

If Err.Number Then
Call Wscript.Echo( &quot;Error 0x&quot; & CStr(Hex(Err.Number)) & &quot;: &quot; & strIn)
If Err.Description <> &quot;&quot; Then
Call Wscript.Echo( &quot;Error description: &quot; & Err.Description)
End If
Err.Clear
blnErrorOccurred = True
Else
blnErrorOccurred = False
End If

End Function

'********************************************************************
'*
'* Function blnOpenFile
'*
'* Purpose: Opens a file.
'*
'* Input: strFileName A string with the name of the file.
'*
'* Output: Sets objOpenFile to a FileSystemObject and setis it to
'* Nothing upon Failure.
'*
'********************************************************************
Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)

ON ERROR RESUME NEXT

Dim objFileSystem

Set objFileSystem = Nothing

If IsEmpty(strFileName) OR strFileName = &quot;&quot; Then
blnOpenFile = False
Set objOpenFile = Nothing
Exit Function
End If

'Create a file object
Set objFileSystem = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If blnErrorOccurred(&quot;Could not create filesystem object.&quot;) Then
blnOpenFile = False
Set objOpenFile = Nothing
Exit Function
End If

'Open the file for output
Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
If blnErrorOccurred(&quot;Could not open&quot;) Then
blnOpenFile = False
Set objOpenFile = Nothing
Exit Function
End If
blnOpenFile = True

End Function

'********************************************************************
'* *
'* End of File *
'* *
'********************************************************************
James Collins
Field Service Engineer
A+, MCP

email: butchrecon@skyenet.net

Please let us (Tek-tips members) know if the solutions we provide are helpful to you. Not only do they help you but they may help others.
 
Butchrecon,

Thanks for the script, i think it will help me out.
 
No problem. James Collins
Field Service Engineer
A+, MCP

email: butchrecon@skyenet.net

Please let us (Tek-tips members) know if the solutions we provide are helpful to you. Not only do they help you but they may help others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top