Solved

Change this script to run on a list of machines in a txt file.

Posted on 2010-08-24
41
608 Views
Last Modified: 2012-08-13
Hi,

Change this script to run on a list of machines in a txt file.
this script check ram. if different then emails. ts a login script. but i want to run it as a scheduled task. So need to run on a txt file with machine names and email if change found. Also need to use the logged in user outlook to send email.

regards
sharath
On error resume next

 

'Get Windows folder location

Set oShell  = CreateObject( "WScript.Shell" )

windir      = oShell.ExpandEnvironmentStrings("%windir%")

 

'Set some other variables

Set fso     = CreateObject("Scripting.FileSystemObject")

strComputer = oShell.ExpandEnvironmentStrings("%computername%")

strFile     = windir & "\temp\ram.log"

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objShell = CreateObject("Wscript.Shell")

objName = objFSO.GetTempName

objTempFile = objName

 

'Run command line to get system information

objShell.Run "cmd /c systeminfo > " & windir & "\Temp\" & objTempFile, 0, True

Set objTextFile = objFSO.OpenTextFile(windir & "\Temp\" & objTempFile, 1)

strMemory = "0"

Do While objTextFile.AtEndOfStream <> True

	strText = objTextFile.ReadLine

	'Look for the line that has the RAM amount

	If Instr(strText, "Total Physical Memory:") > 0 Then

		strMemory = trim(replace(strText,"Total Physical Memory:",""))

		Exit Do

	End If

Loop

objTextFile.Close

 

'Delete the temp file

Set filesys = CreateObject("Scripting.FileSystemObject") 

filesys.DeleteFile windir & "\Temp\" & objTempFile

 

'Create the log file if it does not exist

if NOT fso.FileExists(strFile) Then

	Set objFile = fso.CreateTextFile(strFile, True)

	objFile.WriteLine(strMemory)

	objFile.Close

end if

 

'Read the file

Set objFile = fso.OpenTextFile(strFile, 1)

strContents = replace(objFile.ReadAll,vbCRLF,"")

objFile.Close

 

'Set the log file to the current amount of RAM

Set objFile  = fso.OpenTextFile(strFile, 2, True)

objFile.WriteLine(strMemory)

objFile.Close

 

'Compare the file to what was discovered in the first part of the script

if strContents <> strMemory then

	'There is a change - email an alert

	SendAlert strContents,strMemory,strComputer

end if

 

Sub SendAlert(OriginalRAM,CurrentRAM,sComputer)

	On Error Resume Next

 

	Dim objMessage

	Set objMessage = CreateObject("CDO.Message")

	with objMessage

		.From = "Ram_Notification@plc.com" 

		.To = "sharath@plc.com"

		.Subject = "The machine " & sComputer & " has had a change in RAM"

		.TextBody = "Please confirm that the computer '" & sComputer & "' has had a change in RAM." & vbCRLF & "Last known RAM value: " & OriginalRAM & vbCRLF & "Current RAM value: " & CurrentRAM

		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "exchange name"

		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

		.Configuration.Fields.Update

		.Send

	end with 

	Set objMessage = Nothing

End Sub

Open in new window

0
Comment
Question by:bsharath
  • 22
  • 16
  • 2
41 Comments
 
LVL 5

Expert Comment

by:MaxSoullard
Comment Utility
try this:
On error resume Next

strTxtFile = "TEXT FILE PATH"

Const ForReading = 1
'Reads the TXT file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strTxtFile, ForReading)
strAllComputers = objFile.ReadAll
objFile.Close

arrComputers = Split(strAllComputers,VbCrLf)

For Each strComputer In arrComputers
	 
	'Get Windows folder location
	Set oShell  = CreateObject( "WScript.Shell" )
	windir      = oShell.ExpandEnvironmentStrings("%windir%")
	 
	'Set some other variables
	Set fso     = CreateObject("Scripting.FileSystemObject")
	strComputer = oShell.ExpandEnvironmentStrings("%computername%")
	strFile     = windir & "\temp\ram.log"
	 
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("Wscript.Shell")
	objName = objFSO.GetTempName
	objTempFile = objName
	 
	'Run command line to get system information
	objShell.Run "cmd /c systeminfo > " & windir & "\Temp\" & objTempFile, 0, True
	Set objTextFile = objFSO.OpenTextFile(windir & "\Temp\" & objTempFile, 1)
	strMemory = "0"
	Do While objTextFile.AtEndOfStream <> True
		strText = objTextFile.ReadLine
		'Look for the line that has the RAM amount
		If Instr(strText, "Total Physical Memory:") > 0 Then
			strMemory = trim(replace(strText,"Total Physical Memory:",""))
			Exit Do
		End If
	Loop
	objTextFile.Close
	 
	'Delete the temp file
	Set filesys = CreateObject("Scripting.FileSystemObject") 
	filesys.DeleteFile windir & "\Temp\" & objTempFile
	 
	'Create the log file if it does not exist
	if NOT fso.FileExists(strFile) Then
		Set objFile = fso.CreateTextFile(strFile, True)
		objFile.WriteLine(strMemory)
		objFile.Close
	end if
	 
	'Read the file
	Set objFile = fso.OpenTextFile(strFile, 1)
	strContents = replace(objFile.ReadAll,vbCRLF,"")
	objFile.Close
	 
	'Set the log file to the current amount of RAM
	Set objFile  = fso.OpenTextFile(strFile, 2, True)
	objFile.WriteLine(strMemory)
	objFile.Close
	 
	'Compare the file to what was discovered in the first part of the script
	if strContents <> strMemory then
		'There is a change - email an alert
		SendAlert strContents,strMemory,strComputer
	end If
Next 

Sub SendAlert(OriginalRAM,CurrentRAM,sComputer)
	On Error Resume Next
 
	Dim objMessage
	Set objMessage = CreateObject("CDO.Message")
	with objMessage
		.From = "Ram_Notification@plc.com" 
		.To = "sharath@plc.com"
		.Subject = "The machine " & sComputer & " has had a change in RAM"
		.TextBody = "Please confirm that the computer '" & sComputer & "' has had a change in RAM." & vbCRLF & "Last known RAM value: " & OriginalRAM & vbCRLF & "Current RAM value: " & CurrentRAM
		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "exchange name"
		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
		.Configuration.Fields.Update
		.Send
	end with 
	Set objMessage = Nothing
End Sub

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
check it.
const MACHINES_LIST_FILE = "c:\temp\machines.txt"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		 
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objShell = CreateObject("Wscript.Shell")
		objName = objFSO.GetTempName
		objTempFile = objName
		 
		'Run command line to get system information
		objShell.Run "cmd /c systeminfo > " & windir & "\Temp\" & strComputer & "_" & objTempFile, 0, True
		filePath = windir & "\Temp\" & strComputer & "_" & objTempFile
		
		Set objTextFile = objFSO.OpenTextFile(filePath, 1)
		strMemory = "0"
		Do While objTextFile.AtEndOfStream <> True
			strText = objTextFile.ReadLine
			'Look for the line that has the RAM amount
			If Instr(strText, "Total Physical Memory:") > 0 Then
				strMemory = trim(replace(strText,"Total Physical Memory:",""))
				Exit Do
			End If
		Loop
		objTextFile.Close
		 
		'Delete the temp file
		Set filesys = CreateObject("Scripting.FileSystemObject") 
		filesys.DeleteFile windir & "\Temp\" & strComputer & "_" & objTempFile
		 
		'Create the log file if it does not exist
		if NOT fso.FileExists(strFile) Then
			Set objFile = fso.CreateTextFile(strFile, True)
			objFile.WriteLine(strMemory)
			objFile.Close
		end if
		 
		'Read the file
		Set objFile = fso.OpenTextFile(strFile, 1)
		strContents = replace(objFile.ReadAll,vbCRLF,"")
		objFile.Close
		 
		'Set the log file to the current amount of RAM
		Set objFile  = fso.OpenTextFile(strFile, 2, True)
		objFile.WriteLine(strMemory)
		objFile.Close
		 
		'Compare the file to what was discovered in the first part of the script
		if strContents <> strMemory then
			'There is a change - email an alert
				message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF

		end if
	end if
next

NotifyByEmail message

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get this
Sedgwick
---------------------------
Windows Script Host
---------------------------
Script:      D:\Ram email.vbs
Line:      28
Char:      3
Error:      File not found
Code:      800A0035
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------

MaxSoullard i dont want to use the exchange name rather use the outlook to email
0
 
LVL 5

Expert Comment

by:MaxSoullard
Comment Utility
Try this one it uses outlook
On error resume Next

strTxtFile = "C:\Documents and Settings\soullm01\Desktop\New Text Document.txt"

Const ForReading = 1
'Reads the TXT file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strTxtFile, ForReading)
strAllComputers = objFile.ReadAll
objFile.Close

arrComputers = Split(strAllComputers,VbCrLf)

For Each strComputer In arrComputers
	 
	'Get Windows folder location
	Set oShell  = CreateObject( "WScript.Shell" )
	windir      = oShell.ExpandEnvironmentStrings("%windir%")
	 
	'Set some other variables
	Set fso     = CreateObject("Scripting.FileSystemObject")
	strComputer = oShell.ExpandEnvironmentStrings("%computername%")
	strFile     = windir & "\temp\ram.log"
	 
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("Wscript.Shell")
	objName = objFSO.GetTempName
	objTempFile = objName
	 
	'Run command line to get system information
	objShell.Run "cmd /c systeminfo > " & windir & "\Temp\" & objTempFile, 0, True
	Set objTextFile = objFSO.OpenTextFile(windir & "\Temp\" & objTempFile, 1)
	strMemory = "0"
	Do While objTextFile.AtEndOfStream <> True
		strText = objTextFile.ReadLine
		'Look for the line that has the RAM amount
		If Instr(strText, "Total Physical Memory:") > 0 Then
			strMemory = trim(replace(strText,"Total Physical Memory:",""))
			Exit Do
		End If
	Loop
	objTextFile.Close
	 
	'Delete the temp file
	Set filesys = CreateObject("Scripting.FileSystemObject") 
	filesys.DeleteFile windir & "\Temp\" & objTempFile
	 
	'Create the log file if it does not exist
	if NOT fso.FileExists(strFile) Then
		Set objFile = fso.CreateTextFile(strFile, True)
		objFile.WriteLine(strMemory)
		objFile.Close
	end if
	 
	'Read the file
	Set objFile = fso.OpenTextFile(strFile, 1)
	strContents = replace(objFile.ReadAll,vbCRLF,"")
	objFile.Close
	 
	'Set the log file to the current amount of RAM
	Set objFile  = fso.OpenTextFile(strFile, 2, True)
	objFile.WriteLine(strMemory)
	objFile.Close
	 
	'Compare the file to what was discovered in the first part of the script
	if strContents <> strMemory then
		'There is a change - email an alert
		SendAlert strContents,strMemory,strComputer
	end If
Next 

Sub SendAlert(OriginalRAM,CurrentRAM,strComputer)
	'On Error Resume Next


	Set MyApp = CreateObject("Outlook.Application")
	Set MyItem = MyApp.CreateItem(0) 'olMailItem
	With MyItem
	    .To = "max.soullard@bbc.co.uk"
	    .Subject = "The machine " & strComputer & " has had a change in RAM"
	    .ReadReceiptRequested = False
	    .HTMLBody = "Please confirm that the computer '" & strComputer & "' has had a change in RAM." & vbCRLF & "Last known RAM value: " & OriginalRAM & vbCRLF & "Current RAM value: " & CurrentRAM
	
	End With
	MyItem.send
End Sub

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
check it pls
const MACHINES_LIST_FILE = "c:\temp\machines.txt"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		 
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objShell = CreateObject("Wscript.Shell")
		objName = objFSO.GetTempName
		objTempFile = objName
		 
		'Run command line to get system information		
		filePath = windir & "\Temp\" & strComputer & "_" & objTempFile
		objShell.Run "cmd /c md " & windir & "\Temp\", 0, True
		objShell.Run "cmd /c systeminfo > " & filePath, 0, True
		
		if objFSO.FileExists(filePath) = false then
			wscript.echo "systeminfo log file could not be found: " & filePath
			exit for
		end if
		
		Set objTextFile = objFSO.OpenTextFile(filePath, 1)
		strMemory = "0"
		Do While objTextFile.AtEndOfStream <> True
			strText = objTextFile.ReadLine
			'Look for the line that has the RAM amount
			If Instr(strText, "Total Physical Memory:") > 0 Then
				strMemory = trim(replace(strText,"Total Physical Memory:",""))
				Exit Do
			End If
		Loop
		objTextFile.Close
		 
		'Delete the temp file
		Set filesys = CreateObject("Scripting.FileSystemObject") 
		filesys.DeleteFile windir & "\Temp\" & strComputer & "_" & objTempFile
		 
		'Create the log file if it does not exist
		if NOT fso.FileExists(strFile) Then
			Set objFile = fso.CreateTextFile(strFile, True)
			objFile.WriteLine(strMemory)
			objFile.Close
		end if
		 
		'Read the file
		Set objFile = fso.OpenTextFile(strFile, 1)
		strContents = replace(objFile.ReadAll,vbCRLF,"")
		objFile.Close
		 
		'Set the log file to the current amount of RAM
		Set objFile  = fso.OpenTextFile(strFile, 2, True)
		objFile.WriteLine(strMemory)
		objFile.Close
		 
		'Compare the file to what was discovered in the first part of the script
		if strContents <> strMemory then
			'There is a change - email an alert
				message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF

		end if
	end if
next

NotifyByEmail message

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
sedgwick
Can i have a log on the status
So i am sure its not stuck some where on the machine i run it from
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
what do u mean?

you can open the log file anytime during the running and see which servers already were processed...
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Where does the log store?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
sorry i mixed with another script i posted for you.
here's the script with a log file (see LOG_FILE)
const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		if objFSO.FileExists(LOG_FILE) then
			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)
		else
			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		end if
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		 
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objShell = CreateObject("Wscript.Shell")
		objName = objFSO.GetTempName
		objTempFile = objName
		 
		'Run command line to get system information		
		filePath = windir & "\Temp\" & strComputer & "_" & objTempFile
		objShell.Run "cmd /c md " & windir & "\Temp\", 0, True
		objShell.Run "cmd /c systeminfo > " & filePath, 0, True
		
		if objFSO.FileExists(filePath) = false then
			wscript.echo "systeminfo log file could not be found: " & filePath
			exit for
		end if
		
		Set objTextFile = objFSO.OpenTextFile(filePath, 1)
		strMemory = "0"
		Do While objTextFile.AtEndOfStream <> True
			strText = objTextFile.ReadLine
			'Look for the line that has the RAM amount
			If Instr(strText, "Total Physical Memory:") > 0 Then
				strMemory = trim(replace(strText,"Total Physical Memory:",""))
				Exit Do
			End If
		Loop
		objTextFile.Close
		 
		'Delete the temp file
		Set filesys = CreateObject("Scripting.FileSystemObject") 
		filesys.DeleteFile windir & "\Temp\" & strComputer & "_" & objTempFile
		 
		'Create the log file if it does not exist
		if NOT fso.FileExists(strFile) Then
			Set objFile = fso.CreateTextFile(strFile, True)
			objFile.WriteLine(strMemory)
			objFile.Close
		end if
		 
		'Read the file
		Set objFile = fso.OpenTextFile(strFile, 1)
		strContents = replace(objFile.ReadAll,vbCRLF,"")
		objFile.Close
		 
		'Set the log file to the current amount of RAM
		Set objFile  = fso.OpenTextFile(strFile, 2, True)
		objFile.WriteLine(strMemory)
		objFile.Close
		 
		'Compare the file to what was discovered in the first part of the script
		if strContents <> strMemory then
			'There is a change - email an alert
				message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF

			objResFile.Write message
			objResFile.Close
		end if
	end if
next

NotifyByEmail message

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get this

---------------------------
Windows Script Host
---------------------------
Script:      D:\Ram email.vbs
Line:      13
Char:      4
Error:      Permission denied
Code:      800A0046
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
fixed


const MACHINES_LIST_FILE = "c:\temp\machines.txt"

const LOG_FILE = "c:\temp\ram_process.log"



set objFSO = createobject("scripting.filesystemobject")

set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)

arrMachines = Split(objFile.ReadAll, vbNewLine)

objFile.Close



for each strComputer in arrMachines

	if Trim(strComputer) <> "" then

		

		if objFSO.FileExists(LOG_FILE) then

			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)

		else

			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)

		end if

		

		'Get Windows folder location

		Set oShell  = CreateObject( "WScript.Shell" )

		windir      = oShell.ExpandEnvironmentStrings("%windir%")

		 

		'Set some other variables

		Set fso     = CreateObject("Scripting.FileSystemObject")

		strFile     = windir & "\temp\" & strComputer & "_ram.log"

		 

		Set objFSO = CreateObject("Scripting.FileSystemObject")

		Set objShell = CreateObject("Wscript.Shell")

		objName = objFSO.GetTempName

		objTempFile = objName

		 

		'Run command line to get system information		

		filePath = windir & "\Temp\" & strComputer & "_" & objTempFile

		objShell.Run "cmd /c md " & windir & "\Temp\", 0, True

		objShell.Run "cmd /c systeminfo > " & filePath, 0, True

		

		if objFSO.FileExists(filePath) = false then

			wscript.echo "systeminfo log file could not be found: " & filePath

			objResFile.Close

			exit for

		end if

		

		Set objTextFile = objFSO.OpenTextFile(filePath, 1)

		strMemory = "0"

		Do While objTextFile.AtEndOfStream <> True

			strText = objTextFile.ReadLine

			'Look for the line that has the RAM amount

			If Instr(strText, "Total Physical Memory:") > 0 Then

				strMemory = trim(replace(strText,"Total Physical Memory:",""))

				Exit Do

			End If

		Loop

		objTextFile.Close

		 

		'Delete the temp file

		Set filesys = CreateObject("Scripting.FileSystemObject") 

		filesys.DeleteFile windir & "\Temp\" & strComputer & "_" & objTempFile

		 

		'Create the log file if it does not exist

		if NOT fso.FileExists(strFile) Then

			Set objFile = fso.CreateTextFile(strFile, True)

			objFile.WriteLine(strMemory)

			objFile.Close

		end if

		 

		'Read the file

		Set objFile = fso.OpenTextFile(strFile, 1)

		strContents = replace(objFile.ReadAll,vbCRLF,"")

		objFile.Close

		 

		'Set the log file to the current amount of RAM

		Set objFile  = fso.OpenTextFile(strFile, 2, True)

		objFile.WriteLine(strMemory)

		objFile.Close

		 

		'Compare the file to what was discovered in the first part of the script

		if strContents <> strMemory then

			'There is a change - email an alert

				message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF



			objResFile.Write message

		end if

		

		objResFile.Close

	end if

next



NotifyByEmail message



wscript.echo "Done"

 

sub NotifyByEmail(strBody)

	Dim ToAddress

	Dim MessageSubject

	Dim MessageBody

	Dim MessageAttachment

	dim myRecipient,olMailItem



	Dim ol, ns, newMail



	ToAddress = "xxx.yyy@zzz.com"

	MessageSubject = "Change in RAM notification email"

	MessageBody = strBody



	Set ol = WScript.CreateObject("Outlook.Application")

	Set ns = ol.getNamespace("MAPI")

	ns.logon "","",true,false

	Set newMail = ol.CreateItem(olMailItem)

	newMail.Subject = "RAM Notification Email"

	newMail.Body = MessageBody & vbCrLf



	' validate the recipient, just in case...

	Set myRecipient = ns.CreateRecipient(ToAddress)

	myRecipient.Resolve

	If Not myRecipient.Resolved Then

	   MsgBox "unknown recipient"

	Else

	   newMail.Recipients.Add(myRecipient)

	   newMail.Send

	End If



	Set ol = Nothing



end sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
It breaks in the middle when i get a message as this

---------------------------
Windows Script Host
---------------------------
systeminfo log file could not be found: C:\WINDOWS\Temp\ty00260_radF5EC5.tmp
---------------------------
OK  
---------------------------
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
first of all can u check if the file really exists or not?


short background on this issue.

line 34 -> runs a utility call 'systeminfo' which return system information (RAM among others)
you can open console window and type 'systeminfo' to see the result.
the result of 'systeminfo' is logged to this file: C:\WINDOWS\Temp\ty00260_radF5EC5.tmp

line 36 -> tries to open this file to get the RAM information, if file could not be found the script breaks.

now, the only scenario in which the file is not exists is if the utility didn't write the log.
i modified the script to try 2 different methods to run this utility.
please test it and post if the error occurs.
if it does, i'll find another way to get the RAM info.




const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		if objFSO.FileExists(LOG_FILE) then
			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)
		else
			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		end if
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		 
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		Set objShell = CreateObject("Wscript.Shell")
		objName = objFSO.GetTempName
		objTempFile = objName
		 
		'Run command line to get system information		
		filePath = windir & "\Temp\" & strComputer & "_" & objTempFile
		objShell.Run "cmd /c systeminfo > " & filePath, 0, True
		
		closeFile = false
		if objFSO.FileExists(filePath) = false then
			Set objExecObject = objShell.Exec("cmd /c systeminfo")
			Set objTextFile = objExecObject.StdOut
		else
			Set objTextFile = objFSO.OpenTextFile(filePath, 1)
			closeFile = true
		end if
		
		strMemory = "0"
		Do While objTextFile.AtEndOfStream <> True
			strText = objTextFile.ReadLine
			'Look for the line that has the RAM amount
			If Instr(strText, "Total Physical Memory:") > 0 Then
				strMemory = trim(replace(strText,"Total Physical Memory:",""))
				Exit Do
			End If
		Loop
		
		if closeFile = true then
			objTextFile.Close

			'Delete the temp file
			Set filesys = CreateObject("Scripting.FileSystemObject") 
			filesys.DeleteFile filePath
		end if
		 		 
		'Create the log file if it does not exist
		if NOT fso.FileExists(strFile) Then
			Set objFile = fso.CreateTextFile(strFile, True)
			objFile.WriteLine(strMemory)
			objFile.Close
		end if
		 
		'Read the file
		Set objFile = fso.OpenTextFile(strFile, 1)
		strContents = replace(objFile.ReadAll,vbCRLF,"")
		objFile.Close
		 
		'Set the log file to the current amount of RAM
		Set objFile  = fso.OpenTextFile(strFile, 2, True)
		objFile.WriteLine(strMemory)
		objFile.Close
		 
		'Compare the file to what was discovered in the first part of the script
		if strContents <> strMemory then
			'There is a change - email an alert
				message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF

			objResFile.Write message
		end if
		
		objResFile.Close
	end if
next

NotifyByEmail message

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
check this script, i used WMI to get the RAM infor for each server.
if this one works fine, better use this one.


const MACHINES_LIST_FILE = "c:\temp\machines.txt"

const LOG_FILE = "c:\temp\ram_process.log"



set objFSO = createobject("scripting.filesystemobject")

set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)

arrMachines = Split(objFile.ReadAll, vbNewLine)

objFile.Close



for each strComputer in arrMachines

	if Trim(strComputer) <> "" then

		

		if objFSO.FileExists(LOG_FILE) then

			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)

		else

			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)

		end if

		

		'Get Windows folder location

		Set oShell  = CreateObject( "WScript.Shell" )

		windir      = oShell.ExpandEnvironmentStrings("%windir%")

		 

		'Set some other variables

		Set fso     = CreateObject("Scripting.FileSystemObject")

		strFile     = windir & "\temp\" & strComputer & "_ram.log"

		 

		Set objWMIService = GetObject("winmgmts:" _

		& "{impersonationLevel=impersonate}!\\" _

		& strComputer & "\root\cimv2")

		Set colComputer = objWMIService.ExecQuery _

		("Select TotalPhysicalMemory from Win32_ComputerSystem")

		 

		For Each objComputer in colComputer

			strMemory = objComputer.TotalPhysicalMemory

			exit for

		Next



		firstRun = false

		'Create the log file if it does not exist

		if NOT fso.FileExists(strFile) Then

			Set objFile = fso.CreateTextFile(strFile, True)

			objFile.WriteLine(strMemory)

			objFile.Close

			firstRun = true

		end if

		 

		'Read the file

		Set objFile = fso.OpenTextFile(strFile, 1)

		strContents = replace(objFile.ReadAll,vbCRLF,"")

		objFile.Close

		 

		'Set the log file to the current amount of RAM

		Set objFile  = fso.OpenTextFile(strFile, 2, True)

		objFile.WriteLine(strMemory)

		objFile.Close

		 

		'Compare the file to what was discovered in the first part of the script

		if firstRun = false and strContents <> strMemory then

			'There is a change - email an alert

				message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF



			objResFile.Write message

		end if

		

		objResFile.Close

	end if

next



if message <> "" then

	NotifyByEmail message

end if



wscript.echo "Done"

 

sub NotifyByEmail(strBody)

	Dim ToAddress

	Dim MessageSubject

	Dim MessageBody

	Dim MessageAttachment

	dim myRecipient,olMailItem



	Dim ol, ns, newMail



	ToAddress = "xxx.yyy@zzz.com"

	MessageSubject = "Change in RAM notification email"

	MessageBody = strBody



	Set ol = WScript.CreateObject("Outlook.Application")

	Set ns = ol.getNamespace("MAPI")

	ns.logon "","",true,false

	Set newMail = ol.CreateItem(olMailItem)

	newMail.Subject = "RAM Notification Email"

	newMail.Body = MessageBody & vbCrLf



	' validate the recipient, just in case...

	Set myRecipient = ns.CreateRecipient(ToAddress)

	myRecipient.Resolve

	If Not myRecipient.Resolved Then

	   MsgBox "unknown recipient"

	Else

	   newMail.Recipients.Add(myRecipient)

	   newMail.Send

	End If



	Set ol = Nothing



end sub

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I am trying the previous code and still does not show the
ram_process.log
Populated...
Its empty after it completes as well.
Can we have
Already done
Excluded because of permission issue
and so on
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
run the script.
if server is unavailable it will be logged, also if there's a change in RAM.
check ram_process.log for progress.
const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

dim totalPhysicalMemory
for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		if objFSO.FileExists(LOG_FILE) then
			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)
		else
			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		end if
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		
		totalPhysicalMemory = ExecuteQuery(strComputer)

		if totalPhysicalMemory = "" then
			message = message & "[" & strComputer & "]" & " is unavailable" & vbCRLF
		else

			strMemory = totalPhysicalMemory

			firstRun = false
			'Create the log file if it does not exist
			if NOT fso.FileExists(strFile) Then
				Set objFile = fso.CreateTextFile(strFile, True)
				objFile.WriteLine(strMemory)
				objFile.Close
				firstRun = true
			end if
			 
			'Read the file
			Set objFile = fso.OpenTextFile(strFile, 1)
			strContents = replace(objFile.ReadAll,vbCRLF,"")
			objFile.Close
			 
			'Set the log file to the current amount of RAM
			Set objFile  = fso.OpenTextFile(strFile, 2, True)
			objFile.WriteLine(strMemory)
			objFile.Close
			 
			'Compare the file to what was discovered in the first part of the script
			if strContents <> strMemory then
				'There is a change - email an alert
					message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF

				objResFile.Write message
			end if
		end if
		
		objResFile.Close
	end if
next

if message <> "" then
	NotifyByEmail message
end if

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

function ExecuteQuery(strComputer)
	on error resume next
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
	if Err.number = 0 then
		Set colComputer = objWMIService.ExecQuery _
		("Select TotalPhysicalMemory from Win32_ComputerSystem")
		
		For Each objComputer in colComputer
			ExecuteQuery = objComputer.TotalPhysicalMemory
			exit function
		Next
	else
		ExecuteQuery = ""
	end if
	
end function

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
forgot to log the unavailable servers.
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
here's the script with the update.
const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

dim totalPhysicalMemory
for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		if objFSO.FileExists(LOG_FILE) then
			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)
		else
			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		end if
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		
		totalPhysicalMemory = ExecuteQuery(strComputer)

		if totalPhysicalMemory = "" then
			message = message & "[" & strComputer & "]" & " is unavailable" & vbCRLF
			objResFile.Write message
		else

			strMemory = totalPhysicalMemory

			firstRun = false
			'Create the log file if it does not exist
			if NOT fso.FileExists(strFile) Then
				Set objFile = fso.CreateTextFile(strFile, True)
				objFile.WriteLine(strMemory)
				objFile.Close
				firstRun = true
			end if
			 
			'Read the file
			Set objFile = fso.OpenTextFile(strFile, 1)
			strContents = replace(objFile.ReadAll,vbCRLF,"")
			objFile.Close
			 
			'Set the log file to the current amount of RAM
			Set objFile  = fso.OpenTextFile(strFile, 2, True)
			objFile.WriteLine(strMemory)
			objFile.Close
			 
			'Compare the file to what was discovered in the first part of the script
			if strContents <> strMemory then
				'There is a change - email an alert
					message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & vbCRLF & "Current RAM value: " & strMemory & vbCRLF

				objResFile.Write message
			end if
		end if
		
		objResFile.Close
	end if
next

if message <> "" then
	NotifyByEmail message
end if

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

function ExecuteQuery(strComputer)
	on error resume next
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
	if Err.number = 0 then
		Set colComputer = objWMIService.ExecQuery _
		("Select TotalPhysicalMemory from Win32_ComputerSystem")
		
		For Each objComputer in colComputer
			ExecuteQuery = objComputer.TotalPhysicalMemory
			exit function
		Next
	else
		ExecuteQuery = ""
	end if
	
end function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I get a final report as this. I get the machines that were not accessable because they were offline. Can I get just the differnces machines

[PC8]
Last known RAM value: 3,062 MB
Current RAM value: 526454784
[PC9]
Last known RAM value: 3,062 MB
Current RAM value: 1063325696
[PC10]
Last known RAM value: 3,062 MB
Current RAM value: 1063325696

I will need to get the report in MB or GB like 4 GB and so on
One more thing is I need 1 mail for each differnce not one overall email
That will make it easy for me to forward to others
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
that should do:
const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

dim totalPhysicalMemory
for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		if objFSO.FileExists(LOG_FILE) then
			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)
		else
			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		end if
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		
		totalPhysicalMemory = ExecuteQuery(strComputer)

		if totalPhysicalMemory = "" then
			objResFile.Write "[" & strComputer & "]" & " is unavailable" & vbCRLF
		else

			strMemory = Round(totalPhysicalMemory / 1024 / 1024 / 1024, 2)

			firstRun = false
			'Create the log file if it does not exist
			if NOT fso.FileExists(strFile) Then
				Set objFile = fso.CreateTextFile(strFile, True)
				objFile.WriteLine(strMemory)
				objFile.Close
				firstRun = true
			end if
			 
			'Read the file
			Set objFile = fso.OpenTextFile(strFile, 1)
			strContents = replace(objFile.ReadAll,vbCRLF,"")
			objFile.Close
			 
			'Set the log file to the current amount of RAM
			Set objFile  = fso.OpenTextFile(strFile, 2, True)
			objFile.WriteLine(strMemory)
			objFile.Close
			 
			'Compare the file to what was discovered in the first part of the script
			if Trim(strContents) <> Trim(strMemory) then
				'There is a change - email an alert
					message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & " GB" & vbCRLF & "Current RAM value: " & strMemory & " GB" & vbCRLF & "@"

				objResFile.Write message
			end if
		end if
		
		objResFile.Close
	end if
next

for each msg in Split(message, "@")
	if Trim(msg) <> "" then
		NotifyByEmail message
	end if
next

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

function ExecuteQuery(strComputer)
	on error resume next
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
	if Err.number = 0 then
		Set colComputer = objWMIService.ExecQuery _
		("Select TotalPhysicalMemory from Win32_ComputerSystem")
		
		For Each objComputer in colComputer
			ExecuteQuery = objComputer.TotalPhysicalMemory
			exit function
		Next
	else
		ExecuteQuery = ""
	end if
	
end function

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Author Comment

by:bsharath
Comment Utility
I still get something like this

@[IOP05]
Last known RAM value: 1064808448 GB
Current RAM value: 0.99 GB
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
u need to delete your previous temp files from c:\windows\temp before running the updated script
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
You mean from the server or all the clients ?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
from the machine you run the script...
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Some thing wrong. it finshes and emails
BHut repeating same machine name many times. Not for 1. Say i have 100 machijnes i get report for 2000

Machine: [W00002]
W00002 is unavailable
Machine: [W00006]
W00002 is unavailable
Machine: [W00016]
W00002 is unavailable
Machine: [W00020]
W00002 is unavailable
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
are you sure you use the latest script?
cause if machine is unavailable it won't add it to the email.
only if a change in RAM detected.

const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

dim totalPhysicalMemory
for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		if objFSO.FileExists(LOG_FILE) then
			set objResFile = objFSO.OpenTextFile(LOG_FILE, 8)
		else
			set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		end if
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		
		totalPhysicalMemory = ExecuteQuery(strComputer)

		if totalPhysicalMemory = "" then
			objResFile.Write "[" & strComputer & "]" & " is unavailable" & vbCRLF
		else

			strMemory = Round(totalPhysicalMemory / 1024 / 1024 / 1024, 2)

			firstRun = false
			'Create the log file if it does not exist
			if NOT fso.FileExists(strFile) Then
				Set objFile = fso.CreateTextFile(strFile, True)
				objFile.WriteLine(strMemory)
				objFile.Close
				firstRun = true
			end if
			 
			'Read the file
			Set objFile = fso.OpenTextFile(strFile, 1)
			strContents = replace(objFile.ReadAll,vbCRLF,"")
			objFile.Close
			 
			'Set the log file to the current amount of RAM
			Set objFile  = fso.OpenTextFile(strFile, 2, True)
			objFile.WriteLine(strMemory)
			objFile.Close
			 
			'Compare the file to what was discovered in the first part of the script
			if Trim(strContents) <> Trim(strMemory) then
				'There is a change - email an alert
					message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & " GB" & vbCRLF & "Current RAM value: " & strMemory & " GB" & vbCRLF & "@"

				objResFile.Write message
			end if
		end if
		
		objResFile.Close
	end if
next

for each msg in Split(message, "@")
	if Trim(msg) <> "" then
		NotifyByEmail Reaplce(msg, "@", "")
	end if
next

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

function ExecuteQuery(strComputer)
	on error resume next
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
	if Err.number = 0 then
		Set colComputer = objWMIService.ExecQuery _
		("Select TotalPhysicalMemory from Win32_ComputerSystem")
		
		For Each objComputer in colComputer
			ExecuteQuery = objComputer.TotalPhysicalMemory
			exit function
		Next
	else
		ExecuteQuery = ""
	end if
	
end function

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
I've updated the script to overwrite previous log file (not appending), and have separate email about each machine RAM change.
const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

dim totalPhysicalMemory
for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		
		totalPhysicalMemory = ExecuteQuery(strComputer)

		if totalPhysicalMemory = "" then
			objResFile.Write "[" & strComputer & "]" & " is unavailable" & vbCRLF
		else

			strMemory = Round(totalPhysicalMemory / 1024 / 1024 / 1024, 2)

			firstRun = false
			'Create the log file if it does not exist
			if NOT fso.FileExists(strFile) Then
				Set objFile = fso.CreateTextFile(strFile, True)
				objFile.WriteLine(strMemory)
				objFile.Close
				firstRun = true
			end if
			 
			'Read the file
			Set objFile = fso.OpenTextFile(strFile, 1)
			strContents = replace(objFile.ReadAll,vbCRLF,"")
			objFile.Close
			 
			'Set the log file to the current amount of RAM
			Set objFile  = fso.OpenTextFile(strFile, 2, True)
			objFile.WriteLine(strMemory)
			objFile.Close
			 
			'Compare the file to what was discovered in the first part of the script
			if Trim(strContents) <> Trim(strMemory) then
				'There is a change - email an alert
					message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & " GB" & vbCRLF & "Current RAM value: " & strMemory & " GB" & vbCRLF & "@"

				objResFile.Write message
			end if
		end if
		
		objResFile.Close
	end if
next

for each msg in Split(message, "@")
	if Trim(msg) <> "" then
		NotifyByEmail Reaplce(msg, "@", "")
	end if
next

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx.yyy@zzz.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

function ExecuteQuery(strComputer)
	on error resume next
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
	if Err.number = 0 then
		Set colComputer = objWMIService.ExecQuery _
		("Select TotalPhysicalMemory from Win32_ComputerSystem")
		
		For Each objComputer in colComputer
			ExecuteQuery = objComputer.TotalPhysicalMemory
			exit function
		Next
	else
		ExecuteQuery = ""
	end if
	
end function

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
change line 65 to:

NotifyByEmail Replace(msg, "@", "")

i had a typo on the word "Replace", sorry about that.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I ran the script after i changed a remote machines ram.log file but no email was sent.
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
the machine slog files are formatted this way: <machine>_ram.log. and located under c:\windows\temp
now, if you wanna test the script, change the value in one of the machines log files and run the script.
you should receive an email with the difference.

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I cannot find the file
machinename_ram.log in any machine i checked a few and no file is found.
The location i run the script i get the log created but nothing is written into it. I mean the vbs from the server... No data within the log is created that tells me which all machines its success and which are failures
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
the c:\temp\ram.log is empty?
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
No folder as temp in C drive
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
don't u have c:\temp\ram_process.log ?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
this file should be created no matter what, and reflect the script procedures.
the actual RAM values are stored under c:\windows\temp in each machine log file.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
I cleared 1 machines all files within temp and ran the script on just 1 computer name in the txt file and it completed but no file was created.
The log that i have also did not show any success or failure.
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
after you delete all machines log from c:\windows\temp, the first time u run the script, the ram_process.log is empty.
also if no change in RAM was detected to any of the machines, the ram_process.log is empty.
only if change was detected is a RAM machine, the ram_process.log will show it, and as a consequence an email will be sent.
0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Ok
Can we have a log that records all success and failures like
Create
Already exists
Different
So i know its checking each time i run and its creating if missing and if it already exists
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
check the log, if RAM wasn't changed, it logs as well
const MACHINES_LIST_FILE = "c:\temp\machines.txt"
const LOG_FILE = "c:\temp\ram_process.log"

set objFSO = createobject("scripting.filesystemobject")
set objFile = objFSO.OpenTextFile(MACHINES_LIST_FILE, 1)
arrMachines = Split(objFile.ReadAll, vbNewLine)
objFile.Close

set objResFile = objFSO.CreateTextFile(LOG_FILE, 2)
		
dim totalPhysicalMemory
for each strComputer in arrMachines
	if Trim(strComputer) <> "" then
		
		'Get Windows folder location
		Set oShell  = CreateObject( "WScript.Shell" )
		windir      = oShell.ExpandEnvironmentStrings("%windir%")
		 
		'Set some other variables
		Set fso     = CreateObject("Scripting.FileSystemObject")
		strFile     = windir & "\temp\" & strComputer & "_ram.log"
		
		totalPhysicalMemory = ExecuteQuery(strComputer)

		if totalPhysicalMemory = "" then
			objResFile.Write "[" & strComputer & "]" & " is unavailable" & vbCRLF
		else

			strMemory = Round(totalPhysicalMemory / 1024 / 1024 / 1024, 2)

			firstRun = false
			'Create the log file if it does not exist
			if NOT fso.FileExists(strFile) Then
				Set objFile = fso.CreateTextFile(strFile, True)
				objFile.WriteLine(strMemory)
				objFile.Close
				firstRun = true
			end if
			 
			'Read the file
			Set objFile = fso.OpenTextFile(strFile, 1)
			strContents = replace(objFile.ReadAll,vbCRLF,"")
			objFile.Close
			 
			'Set the log file to the current amount of RAM
			Set objFile  = fso.OpenTextFile(strFile, 2, True)
			objFile.WriteLine(strMemory)
			objFile.Close
			 
			'Compare the file to what was discovered in the first part of the script
			if Trim(strContents) <> Trim(strMemory) then
				'There is a change - email an alert
					message = message & "[" & strComputer & "]" & vbCRLF & "Last known RAM value: " & strContents & " GB" & vbCRLF & "Current RAM value: " & strMemory & " GB" & vbCRLF & "@"

				objResFile.Write message
			else
				objResFile.WriteLine "[" & strComputer & "]" & vbCRLF & "RAM wasn't changed"
			end if
		end if
	end if
next

objResFile.Close

for each msg in Split(message, "@")
	if Trim(msg) <> "" then
		NotifyByEmail Replace(msg, "@", "")
	end if
next

wscript.echo "Done"
 
sub NotifyByEmail(strBody)
	Dim ToAddress
	Dim MessageSubject
	Dim MessageBody
	Dim MessageAttachment
	dim myRecipient,olMailItem

	Dim ol, ns, newMail

	ToAddress = "xxx@yyy.com"
	MessageSubject = "Change in RAM notification email"
	MessageBody = strBody

	Set ol = WScript.CreateObject("Outlook.Application")
	Set ns = ol.getNamespace("MAPI")
	ns.logon "","",true,false
	Set newMail = ol.CreateItem(olMailItem)
	newMail.Subject = "RAM Notification Email"
	newMail.Body = MessageBody & vbCrLf

	' validate the recipient, just in case...
	Set myRecipient = ns.CreateRecipient(ToAddress)
	myRecipient.Resolve
	If Not myRecipient.Resolved Then
	   MsgBox "unknown recipient"
	Else
	   newMail.Recipients.Add(myRecipient)
	   newMail.Send
	End If

	Set ol = Nothing

end sub

function ExecuteQuery(strComputer)
	on error resume next
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" _
		& strComputer & "\root\cimv2")
	if Err.number = 0 then
		Set colComputer = objWMIService.ExecQuery _
		("Select TotalPhysicalMemory from Win32_ComputerSystem")
		
		For Each objComputer in colComputer
			ExecuteQuery = objComputer.TotalPhysicalMemory
			exit function
		Next
	else
		ExecuteQuery = ""
	end if
	
end function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
Comment Utility
Still a little confused with this post
Can i have a log that shows as checked the machine log available and its same
with different details that shows Could not create log,Log available,Could not contact machine,Permission error and so on...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now