Sunday, October 03, 2010

Server Scheduled Tasks Script

'Author: Scot Geer ()
'Purpose: Reads all scheduled tasks listed, compiles their last run, last result, etc.
'         & then sends it to specified recipient(s) via text email
'Date Created: 07 Oct 2008
'Instructions: Modify only the parameters in the "modify" section below
'Tips: If this script cannot send an email to the SMTP server, it will fail silently
'      & write to the Event Log. Put a bad ip address in there, then run it, then check
'      your event log (application, ERROR, WSH)
'##########################################################################################

Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objFSO, objDocument, objShell
Dim commandsArr, schTasksArr(), groupedTasksArr()
Dim myCmd, x, fullCommand, totalTaskLinesRequested
Dim reqSchTasksLinesArr, myTimeDateNow

myCmd = "cmd /c "


'                                 !!! Modify this section only !!!
'##########################################################################################
'commandsArr is an array of command line commands
'CLI Scheduled Tasks reference found here:
'http://technet.microsoft.com/en-us/library/cc772785.aspx
commandsArr = Array("schtasks /query /fo LIST /v")

'run this CLI command "schtasks /query /fo LIST /v" and pick which lines you want here:
reqSchTasksLinesArr = Array("TaskName:","Next Run Time:","Last Run Time:","Last Result:")

'Modify this section only for the 'sendEmail' function
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Dim eFrom,eTo,eSubject,eBody,eAttachment,eUsername,ePassword
Dim eSmtpServer,eSmtpPort,eSmtpUseSSL

Const eNetworkType = 1 'use '1' if script will run on machine with SMTP installed
'Const eNetworkType = 2 'use '2' if script will run on SMTP box somewhere on network

eFrom = "s@some-domain.com"
eTo = "me@some-email.com"
eSubject = "Daily Scheduled Tasks Report (Server-Name - 10.12.14.21)"
eBody = ""
'Type in full path and filename with extension here if you want an attachment
eAttachment = "%homepath%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\backup01.log"
'Note: use domain\username if going to exchange server
'Note: use username@domain.com if going to public Internet SMTP server
'Note: only use username/password if SMTP server is on separate machine
eUsername = "" 
ePassword = "" 
eSmtpServer = "Name-of-SMTP-Server"
eSmtpPort = 25
eSmtpUseSSL = False
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'##########################################################################################
'                                  !!! Modify this section only !!!


totalTaskLinesRequested = ((UBound(reqSchTasksLinesArr))+1)




'Main Code
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myTimeDateNow = FormatDateTime(Time,0) & ", " & Date

For x=0 To UBound(commandsArr)
fullCommand = myCmd & commandsArr(x)
'replace Null with fullPath if writing to file
runCommands fullCommand, Null 
Next

groupTaskResults

eBody = prettyUpResults 'builds results into body of email

sendEmail eFrom,eTo,eSubject,eBody,eAttachment,eUsername,ePassword, _
eNetworkType,eSmtpServer,eSmtpPort,eSmtpUseSSL
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





'Action: runs commands via command line, reads each line or reads all
'Dependencies: 'appendTextToFile()' (only if you want to write to file)
'              global declaration of schTasksArr to enter results
'           global declaration of totalTaskLinesRequested
'              'groupTaskResults()' 
'Parameters: 'pCommand' --> command line (ex: "schtasks /query /fo LIST /v") 
'            'pPath' --> fullPath of text file to write info to (if applicable)
'Returns: Nothing
'******************************************************************************
Sub runCommands(pCommand,pPath)

Dim objExecObject, strLine, y, w

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(pCommand)
y = 0

Do Until objExecObject.StdOut.AtEndOfStream
   strLine = objExecObject.StdOut.ReadLine
   For w=0 To UBound(reqSchTasksLinesArr)
   If InStr(strLine, reqSchTasksLinesArr(w)) Then
    ReDim Preserve schTasksArr(y)
    schTasksArr(y) = strLine
' appendTextToFile pPath,schTasksArr(y) 'write to text file
    y=y+1
   End If
Next
Loop
End Sub
'********************************************************************|


'Action: pulls 3 specific lines from the 'schTasksArr' array and groups them
'        into their own sections for easy display or printing to a file
'Dependencies: 'schTasksArr' --> lines from --> "schtasks /query /fo LIST /v"
'              global declaration of 'totalTaskLinesRequested'
'              'totalTaskLinesRequested' = # of lines wanted from schtasks cmd
'Parameters: none
'Returns: nothing returned but it does build the global groupedTasksArr array
'******************************************************************************
Function groupTaskResults

'making info into groups of 3 lines 
'we want 3 specific lines from each sch task
Dim m, w, groupedTasks, groupedTaskResults, tempPlaceHolder
w = 0
'get number of total task lines, divide by totalTaskLinesRequested 
'to get # of sections needed
groupedTasks = (UBound(schTasksArr) + 1)/totalTaskLinesRequested
'don't forget to subtract 1 (starts at zero)
For x=0 To groupedTasks - 1
'grouping each scheduled task sections
'don't forget to subtract 1 (starts at zero)
For m=0 To totalTaskLinesRequested - 1
tempPlaceHolder = tempPlaceHolder & schTasksArr(w) & vbCrLf
w = w + 1
Next
groupedTaskResults = tempPlaceHolder
tempPlaceHolder = Null 

ReDim Preserve groupedTasksArr(x)
groupedTasksArr(x) = groupedTaskResults
'Keep this echo line here for testing purposes
' WScript.Echo groupedTasksArr(x) & vbTab & "This is the grouped tasks"
Next

End Function
'*******************************************************************|


'Action: formats array results into a readable/printable form
'Dependencies: globally declared array of some kind
'Parameters: none
'Returns: 'myPrettyPrint' --> properly formatted results
'******************************************************************************
Function prettyUpResults

Dim myPrettyPrint
For x=0 To UBound(groupedTasksArr)
myPrettyPrint = myPrettyPrint & groupedTasksArr(x) _
& vbCrLf & "Current System Time" _
& String(9, ".") & myTimeDateNow _
& vbCrLf & String(40, "#") & vbCrLf & vbCrLf
Next

prettyUpResults = myPrettyPrint

End Function
'*******************************************************************|


'Action: sends email & attachment (attachment optional)
'Dependencies: global declaration of "modify" section below
'Parameters: see "modify" section below
'Returns: nothing
'******************************************************************************
Function sendEmail(pFr,pTo,pSu,pTe,pAtt,eUser,ePswd,pType,pSmtp,ePort,eSSL)

'Modify this section only for the 'sendEmail' function 
'Put this section in the global declaration section of this script
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' Dim eFrom,eTo,eSubject,eBody,eAttachment,eUsername,ePassword
' Dim eSmtpServer,eSmtpPort,eSmtpUseSSL

'Const eNetworkType = 1 'use '1' if script will run on machine with SMTP installed
'Const eNetworkType = 2 'use '2' if script will run on SMTP box somewhere on network

' eFrom = "username@domain.com"
' eTo = "recipient@domain.com"
' eSubject = "Subject Message"
' eBody = "Body of email"
'Type in full path and filename with extension here if you want an attachment
' eAttachment = "c:\temp\logs\backup01.log"
'Note: use domain\username if going to exchange server
'Note: use username@domain.com if going to public Internet SMTP server
' eUsername = "domain\username" 'don't use this line if eNetworkType=1 
' ePassword = "password"        'don't use this line if eNetworkType=1
' eSmtpServer = "serverName or IP address"
' eSmtpPort = 25
' eSmtpUseSSL = False
'
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

On Error Resume Next

Dim objEmail, extraMessages

Set objEmail = CreateObject("CDO.Message")
extraMessages = "Check the parameters in the 'email modify' section" _
& " of the script" _
& vbCrLf & "Rob I. is a Hottie!  :D"
objEmail.From = pFr
objEmail.To = pTo
objEmail.Subject = pSu
objEmail.Textbody = pTe
objEmail.AddAttachment(pAtt)
objEmail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/sendusing") = pType
objEmail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = pSmtp 
objEmail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = ePort

'Only use this section if running this script from a non-SMTP server, such as a 
'workstation. Simply remove the apostrophe's at the beginning of each line.
'##############################################################################
' objEmail.Configuration.Fields.Item _
'    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 
' objEmail.Configuration.Fields.Item _
'    ("http://schemas.microsoft.com/cdo/configuration/sendusername") = eUser
' objEmail.Configuration.Fields.Item _ 
'    ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = ePswd
' objEmail.Configuration.Fields.Item _
'    ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = eSSL
' objEmail.Configuration.Fields.Item _
'    ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
'##############################################################################

objEmail.Configuration.Fields.Update
objEmail.Send
myErrHandler(extraMessages)
End Function
'*******************************************************************|


'Action: generic error handler module that writes to the system event log
'Dependencies: global declaration of 'objShell'
'Parameters: 'pMsg' = extra message you want written to the event log
'Returns: nothing
'******************************************************************************
Sub myErrHandler(pMsg)

Dim errMessage, eventLogMessage
If Err.Number<>0 Then
errMessage = Err.Description
eventLogMessage = WScript.ScriptFullName _
& vbCrLf & vbCrLf & errMessage _
& vbCrLf & pMsg
' WScript.Echo eventLogMessage
objShell.LogEvent 1, eventLogMessage
End If
Err.Clear

End Sub
'*******************************************************************|


'Action: quits the script and prompts the user "Quitting Now"
'Dependencies: none
'Parameters: nothing
'Returns: nothing
'******************************************************************************
Sub quitScript

WScript.Echo "Quitting Now"
Wscript.Quit

End Sub
'*******************************************************************|

Monday, August 04, 2008

MySQL Double Joins


devices table
device_id (PK) | device_name

site_contacts table
device_id (FK) | contact_name | contact_email

site_address table
device_id (FK) | address

Select specific info from 2 tables without having to pull all info from all 3 tables:
################################################
SELECT contact_name, contact_email, address
FROM site_contacts INNER JOIN devices
ON site_contacts.device_id = devices.device_id
INNER JOIN site_address
ON site_address.device_id = devices.device_id
WHERE devices.device_name = 'fake_router.mrn';
################################################


LEFT (aka OUTER) JOIN:
Returns all records from the "LEFT" table (the table listed after the "FROM" clause) even the records that have no matching row in the "RIGHT" table (the table listed after the "LEFT JOIN" clause).

LEFT JOIN NOTES:
If both tables in a left join have the same column name, you can simplify your query with the following:
################################################
SELECT *
FROM devices LEFT JOIN site_address
USING (device_id);
################################################

*Note: In this case, all records from the 'devices' table would be returned even if there were no corresponding record in the 'site_address' table.


INNER (aka CROSS) JOIN:
Only returns matching row records that exist in 2 or more tables
################################################
SELECT device_name, address
FROM devices, site_address
WHERE devices.device_id = site_address.device_id;
################################################

*Note: in this case, only records from the 'devices' table that have matching records in the 'site_address' table will be returned (or vice-versa). The order of the tables listed in your query will not matter (because you are performing an INNER JOIN).


Saturday, May 05, 2007

Secure HD Erase

How to REALLY erase a hard drive by ZDNet's Robin Harris -- You may already know that “deleting” a file does nothing of the sort. But did you know that your disk drive has a built-in system for the secure erasure of data? No? Then read on. What do you mean “delete” doesn’t delete? File information is maintained in a directory so your operating system can find it. All that [...]

Sunday, February 19, 2006

Increase Icon Cache / Remember Folder View / Registry Favorites


--------------------------------------------------------------
"HKLM\SOFTWARE\Microsoft\Windows\Current Version\Explorer"
Max Cached Icons
Reg_SZ or DWORD
value can be set from 100 to 4096
-------------------------------------------------|


*** FORCE REMEMBER ICON TYPE ***
--------------------------------------------------------------
Windows Registry Editor Version 5.00


[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer]
"Max Cached Icons"=dword:000007d0
; 000007d0 = 2000 decimal
-------------------------------------------------|


*** FORCE REMEMBER FOLDER VIEWS ***
--------------------------------------------------------------
Windows Registry Editor Version 5.00


[-HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\BagMRU]
[-HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam\Bags]

[HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell]
"BagMRU Size"=dword:00001f40
; 00001f40 = 8000 decimal

[HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam]
"BagMRU Size"=dword:00001f40

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoSaveSettings"=dword:00000000

;Look up Disabled Desktop icon on M.E. PC
;http://www.kellys-korner-xp.com/xp_tweaks.htm
-------------------------------------------------|


*** REGISTRY FAVORITES ***
--------------------------------------------------------------
Windows Registry Editor Version 5.00


[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\Favorites]
"RecycleBinIcon"="My Computer\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CLSID\\{645FF040-5081-101B-9F08-00AA002F954E}\\DefaultIcon"

"HKLMRun"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"

"Time Servers"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers"

"HKLM Home"="My Computer\\HKEY_LOCAL_MACHINE"

"IE Restrictions"="My Computer\\HKEY_CURRENT_USER\\Software\\Policies\\Microsoft\\Internet Explorer"

"HKUAdminRun"="My Computer\\HKEY_USERS\\*** GUID ***\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"

"HKCU Run"="My Computer\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"

"HKLM WSH"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Script Host\\Settings"

"HKLM RunOnce"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"

"IE Proxy Settings"="My Computer\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"

"Group Policy SIDS"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\*** GUID ***"

"View Source Editor"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\\View Source Editor"

"Local NTP Server"="My Computer\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters"

"Change NIC MAC"="My Computer\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\0001"

"EnableOpLocks1"="My Computer\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\lanmanserver\\parameters"

"OpLocksEnabled2"="My Computer\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\MRxSmb\\Parameters"

"Classic Shell/Disable Quicklaunch"="My Computer\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"

"Enable DCOM"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Ole"

"ODBC.INI"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\ODBC\\ODBC.INI"

"Max Cached Icons"="My Computer\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer"

"Min Outlook to SysTray"="My Computer\\HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\10.0\\Outlook\\Preferences"
------------------------------------------------|