Send email from the command line

Posted by Steve Wiseman on November 17, 2009 with 1 Comments

Two weeks ago, I wrote about backing up a remote windows machine using Remote Desktop.

One thing lacking from this solution is email notification. How do we know if the backup job started? Or finished?

There is a free and open source tool that can be used to solve this problem, it is called blat. It consists of an executable, and a DLL.

You can find it at http://www.blat.net/

Send Mail Command Line

Download it, and extract the files to a place where your scripts run. In my case, I put it in c:\backups

To start, you will need an SMTP server that you have access to. That is probably the most difficult part of getting it to work – which means it is very easy to use.

If I just want to send a simple email that tells me a batch script has started, I could call it like this:

blat.exe -f batch@intelliadmin.com -to support@intelliadmin.com -server 192.168.1.1 -subject "Batch file started" -body "The very important batch script has started"

You have five required options to get your email to send:

-f [FROM] (This is the email address that the message will come from)
-to [TO] (This is the email address the message will be sent to)
-subject [SUBJECT] (Subject of the email. Uses quotes if you have spaces)
-body [BODY] (Body of email. Use quotes if you have spaces)
-server [SERVER] (IP Address or host name of the server)

That is for a simple message. Blat is filled with powerful options. For example, I could run a robocopy in my script, and redirect the output to a text file like this:

robocopy c:\backup \\server\c\backup >> output.txt

Then, when I send my notification I could attach the output text file to my email like this:

blat.exe -f batch@intelliadmin.com -to support@intelliadmin.com -server 192.168.1.1 -subject "Batch file started" -body "The very important batch script has completed" -attach c:\backup\output.txt

Once the command runs, the output will show up as an attachment in my inbox:

Send email cmd prompt

Like I said, it is loaded with options. Check it out, and when you do just type blat.exe -help to get the entire list of command line parameters.

Original Article from www.networksteve.com

Filed Under: BAT Files, Tools, Utility

Date and time stamp in your batch files

Posted by Steve Wiseman on November 15, 2009 with 1 Comments

There has been some activity recently in an old 2007 post I wrote about creating a date and time stamp in your batch files.

Well, the funny part was that I said ‘Date and Time’ in the subject, but I only talked about pulling out the current date.

Sue asked “What if I need both date, and time?”

Good question. What if you want to create a file that has the current date and time for each execution of the script?

In my old article I was creating zip files, using an imaginary command line based zip program. So in my article here I am going to make our goal to zip up a folder and name it:

YYYYMMDDHHMMSS_DATA.zip

If it were run today, our script should create a file by this name:

20091111143900_DATA.zip

To accomplish this, you need to use substring batch codes. If you want more detail on how these work, I explain it in the original article:

Create a date and time stamp in your batch files

To get the current time we use the %TIME% environment variable, and %DATE% for the date.

Using the substring batch codes from my old article, this is how we would pull out the year, month and day:

%date:~-4,4%%date:~-10,2%%date:~-7,2%

How about the time? That can be a little more difficult since the numbers returned from the time are not always the same length. For example, if it is 9 o’clock, it will say 9:00 – not 09:00. This will cause trouble when using the value for our filename.

Before we deal with that space, let me show you the substring codes to pull out the time:

Milliseconds: %time:~-2,2%
Seconds: %time:~-5,2%
Minutes: %time:~-8,2%
Hours: %time:~-11,2%

So naturally, if we wanted HHMMSS we could take those values above and put them side by side:

%time:~-11,2%%time:~-8,2%%time:~-5,2%

But like I said before, the hours can give you trouble because of the space. I came up with this code to pull off the space:

SET HOUR=%time:~-11,2%
Call :TRIM %HOUR%
GOTO :EOF
:TRIM
Set HOUR=%*
:EOF
REM You would use your trimmed hour right here
@echo %HOUR%

So, pulling it all together, how would we get an environment variable filled with a good date and time stamp filename?

Here is the code:
REM Get the hour first and put in an environment var

SET HOUR=%time:~-11,2%
Call :TRIM %HOUR%
GOTO :EOF
:TRIM
Set HOUR=%*
:EOF

REM Create our timestamp filename variable
SET DATESTMP=%date:~-4,4%%date:~-10,2%%date:~-7,2%%HOUR%
SET FILENAME=%DATESTMP%%time:~-8,2%%time:~-5,2%_DATA.ZIP

REM This is just an example call using our new filename
REM -=The real PKZIP program probably uses a different command line syntax
pkzip.exe c:\ImportantFolder\*.* c:\ZIPFILES\%FILENAME%

Using the code above, you can easily generate new files using the date and time in your batch script.

Original Article from www.networksteve.com

Free Tool – Suspend from the command line

Posted by Steve Wiseman on September 22, 2009 with 5 Comments

I have a system task schedule that wakes up my system at night, runs a few backup processes…and then shuts the computer down.

What I really wanted it to do was to suspend, or hibernate. That way I can get back into my computer quick when I come back in the morning.

After searching around, I found that this little VB Script command can be used to suspend a computer:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "RunDll32.exe powrprof.dll,SetSuspendState"

Unfortunately this does not work on all versions of Windows. For my task, I wanted something I could use on anything from Windows 2000, to the 64 bit version of Windows 7.

After many fruitless searches on Google, I had the IntelliAdmin team build one for me.

It is called System Suspend, and you can download it from here: SystemSuspend.exe

If you run the program with no options, it will immediately suspend the computer. There are two optional arguments you can use when calling it:

/Hibernate:[TRUE|FALSE] – Defaults to false. If you set Hibernate to true the computer will totally power off and save the state to disk. If Hibernate is set to false, then the system will go into a low power state that will allow you to resume quickly

/Force:[TRUE|FALSE]- Applications are notified of the suspend/hibernate by default. If you set the force option to true, then it will skip this step and perform the action immediately

We have tested it on Windows 2000, XP, 2003, Vista, Windows 7, and 2008 – including 64 bit versions…and it works great on all of them.

Filed Under: BAT Files, Tools, Utility, Windows

Write a script to find and delete a file

Posted by Steve Wiseman on June 17, 2009 with 3 Comments

I distributed a test version of Remote Control 4.0 across our network last week, and since it was a special debug version, it created a large number of files named debuglog.dat.

I wanted an easy way to find and delete these using a batch file. I researched and was able to take parts of other scripts to build what I needed.

The first step was to identify all of the local drives on the system. I know that this is available in the registry key HKEY_LOCAL_MACHINE\System\MountedDrives

Using the reg command, I can get this by calling:

reg query HKLM\SYSTEM\MountedDevices

And using the find command I can pull out only the devices with dos drive letters:

reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”

Still, this is not enough for our task. Here is what we get when calling the script:

Find and Delete

Lots of binary data we do not need

We can take this output, and feed it through the “for” command, and call a function for each drive letter:

for /f “tokens=1″ %%x in (’reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”‘) do echo %%x

Now, we get a list of the drives, minus the binary data. We can use that to call a function that will parse out the \DosDevices\

for /f %34tokens=1%34 %%x in (’reg query HKLM\SYSTEM\MountedDevices^%7cfind /i %34\DosDevices\%34′) do call :D eleteFileSub %%x
goto endScript :D eleteFileSub
set LocalDrive=%1
set LocalDrive=%LocalDrive:~-2%
echo %Drive%
goto endScript
:endScript

With that script, we get a complete list of local drives. Hmm…starting to look good:

Local Drive Letters

Now. What next? Well, we want to search each local drive for our file, and then delete it. For safety reasons, I am only going to echo the file names – I want you to think real hard before doing this..since the wrong move and you could wipe out important files.

Again, the ‘for’ command is a great way to do this:

for /f “tokens=1 delims=*” %%z in (’dir “LocalDrive%\SomeFile.txt” /s /b’) do echo del “%%z”

Replace SomeFile.txt with the file you want to remove.

This will pull out the filename in a full directory search of the specified drive and echo it. If you want to actually delete the file you will need to remove the “echo”:

for /f “tokens=1 delims=*” %%z in (’dir “LocalDrive%\SomeFile.txt” /s /b’) do echo del “%%z”

Now, putting the whole thing together:

@echo off
for /f “tokens=1″ %%x in (’reg query HKLM\SYSTEM\MountedDevices^|find /i “\DosDevices\”‘) do call :deleteSub %%x
goto endSub
:deleteSUb
set LocalDrive=%1
set LocalDrive=%LocalDrive:~-2%
for /f “tokens=1 delims=*” %%z in (’dir “%Drive%\Some File.txt” /s /b’) do echo del “%%z”
goto endSub
:endSub

Text version here if you have copy-paste issues

Make sure you do lots of testing before you pull the trigger and remove the “echo” statement. I wouldn’t want you to blow away the wrong files :)