System Administration-Some simplest scripts for daily tasks

[tweetmeme source=”mrnitishkumar” only_single=”false”]

Its been really a while when I last wrote over System administration, my office job. Might be the reason that you don’t find it enough interesting to write on, if things are not changing much. But let me say it..if we think the small things around us are boring and complicated, then its because we are not thinking about them.

Technology is not a panacea, technology is not some magic, but sure could be magical, if we apply it for solving our cause in intuitive ways. As a popular brand says Its what we do with it.

I am listing some of the simplest troubling tasks on System administration and easiest understandable solutions on them via scripts. Note that all the solutions are just raw and sure better implementations could be found, but its just to push your imagination and then share the same with whole world.

Task 1: Monitoring network connection of some server or some link on hourly/30mins basis and log it too.
Task 2: Taking system state backup of servers on Daily/ Weekly basis.
Task 3: Taking event logs backup of servers on Daily/ Weekly basis.

Task 1: Monitoring network connection of some server or some link on hourly/30mins basis and log it too.
Such scenarios are most common. You will asked to monitor some link on regular basis and obvious way to do the same is ping the same server. But in messy environments like sys admin usually get, its not possible for someone to sit in front of screen and MS DOS windows do not allow to check history of long back. What if one wanted to know that in whole day, when the link went down or might be some alert right then?

Yes there are softwares for doing the same, but why can’t you yourself think a simple solution? Check the script below..

echo off
set Today=%date:~6,4%%date:~0,2%%date:~3,2%
set Period=%time:~6,4%%time:~0,2%%time:~3,2%
mkdir \\\share\%Today%
sc.exe config "Messenger" start= auto
net start Messenger
echo The time at which the check was done: %time% >>\\\share\%Today%\log.txt
Ping -n 2 | find "Reply" >>\\\share\%Today%\log.txt
if errorlevel 1 net send failed %time%

Your PC IP here:
Monitored IP here:
Share location: \\\share

You just need to save this as a batch file and then put it in Windows scheduled tasks to run in each 10 mins or 20 mins to take the status. It will create date wise folders in any given share location (each new day, a new folder with name like ddmmyyyy) and also will pop up a message on your system ( via net send in case of failure. Logs will be created in a text file named log.txt, one in each of the date folder accordingly.

Note: you need to change the date/time format of system as DDMMYYYY otherwise, it will create the folders with weird names.

Task 2: Taking system state backup of servers on Daily/ Weekly basis

Yeah you will say that system state backup method doesn’t work most of the time in expected way, but that’s the case due to un-equal hardware configurations most of the time. Best practices always ask you for taking system state backup time to time.

In real world, we usually miss or ignore the task, which were never productive for us, so better scheduling the same via Windows scheduled tasks and your custom script.

REM – Create Date and Time strings
set Today=%date:~6,4%%date:~0,2%%date:~3,2%
net use X: /delete
net use x: \\\share
mkdir x:\%Today%
ntbackup backup systemstate /f x:\%Today%\AD.bkf
cd C:\Program Files\Windows Resource Kits\Tools
compress -Z x:\%Today%\AD.bkf x:\%Today%\AD.ZIP
del x:\%Today%\AD.bkf

Share location: \\\share

Like the last one, you just need to save the above as a batch file and put it in Windows scheduled task to run on weekly or daily basis (as your situations permit). Obviously the share location must be accessible\writeable and the script has to be placed in scheduled task of the same server, you want to backup. There should be sufficient disk space in the share to accommodate your needs. One day backup may be around 1-2 Gbs and after compressing (which the script does in last three lines) that will go sufficiently lesser. For using later on, you will be needed to expand the backup from compressed file via expand command in similar way.

1. you need to change the date/time format of system as DDMMYYYY
2. You need to install Windows Resource Toolkit in the same server for the compress tool.

Task 3: Taking event logs backup of servers on Daily/ Weekly basis

Well going through event logs might be irritating sometimes as Windows many times just skip to provide the info, we might be looking for. But as a compliance thing for Audit reviews or for security reasons, you always wishes to keep the logs for the longest period possible. So, what about backing up them on daily basis and removing them from the server?

The below is the VB script that I got from some forum.

Dim DestServer
‘ Put in the UNC path for where you want the logs to be stored
DestServer = \\\share\

‘Create the Time variables
sDate=Right("0" & Month(Date),2) _
& "-" & Right("0" & Day(Date),2) _
& "-" & Right(Year(Date),2)

sTime = DatePart("h", Now) & DatePart("n", Now)

set oFSO = CreateObject("Scripting.FileSystemObject")

‘If correct folder doesn’t exist, make it
if Not oFSO.FolderExists(DestServer & sDate) then
   set oFolder = oFSO.CreateFolder(DestServer & sDate )
end if

‘Gets the log files for this machine
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
        & strComputer & "\root\cimv2")

Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile")

‘This section goes out and gets the hostname this is run on for us.

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

For Each objItem in colItems
  strHOSTNAME = objItem.Name

‘Now archive the logs and clear them
if oFSO.FolderExists(DestServer & sDate) then
  For Each objLogfile in colLogFiles
    strBackupLog = objLogFile.BackupEventLog _
        (DestServer & sDate & "\"  & strHOSTNAME & "_" & objLogFile.LogFileName & "_" & sDate & "_" & sTime & ".evt")
end if

Share location: \\\share

This time, its not a batch file like last two tasks and you have to save the above text as a .vbs file and then same routine.. put in Windows scheduled tasks. Note that security logs might be huge like some 800 mb – 1GB, so slow down the servers up to almost non-responsive state for a few mins sometime. I have not found any work around over the same yet, but till now, its safe to run the same in a time, where the servers are not in full production.

Note: you need to change the date/time format of system as DDMMYYYY.

Just thought to post the same scripts here, so that other might be benefitted and also to bring awareness that Windows scripts are not something to be completely ignored or to be thought as geeky stuff only. Hope others also will share few of such tricks with me.

Keep tuned in and drop a few words please.

Now you could download the app for this blog on your Nokia (Symbian5) via  this link or widget via this link.

Digg This

One thought on “System Administration-Some simplest scripts for daily tasks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.