Solved

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

Posted on 2010-08-24
41
615 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 22
  • 16
  • 2
41 Comments
 
LVL 5

Expert Comment

by:MaxSoullard
ID: 33511622
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
ID: 33511887
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
ID: 33512268
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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 5

Expert Comment

by:MaxSoullard
ID: 33512397
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
ID: 33518489
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
ID: 33518583
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
ID: 33518596
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
ID: 33518607
Where does the log store?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33518616
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
ID: 33518644
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
ID: 33518660
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
ID: 33518832
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
ID: 33518919
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
ID: 33518959
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
ID: 33519161
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
ID: 33520232
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
ID: 33520282
forgot to log the unavailable servers.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33520286
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
ID: 33539611
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
ID: 33552029
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
 
LVL 11

Author Comment

by:bsharath
ID: 33552099
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
ID: 33552101
u need to delete your previous temp files from c:\windows\temp before running the updated script
0
 
LVL 11

Author Comment

by:bsharath
ID: 33552114
You mean from the server or all the clients ?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33552123
from the machine you run the script...
0
 
LVL 11

Author Comment

by:bsharath
ID: 33557264
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
ID: 33557309
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
ID: 33557319
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
ID: 33557336
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
ID: 33603643
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
ID: 33605870
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
ID: 33605903
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
ID: 33605918
the c:\temp\ram.log is empty?
0
 
LVL 11

Author Comment

by:bsharath
ID: 33605925
No folder as temp in C drive
0
 
LVL 42

Expert Comment

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

Expert Comment

by:sedgwick
ID: 33606066
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
ID: 33606126
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
ID: 33606233
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
ID: 33606249
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
ID: 33656899
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
ID: 33710568
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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