Using Task Scheduler to send an email if drive not present

I want to use Task Scheduler to send an email if a drive is.

At the moment I can send an email from the task scheduler and can make tasks no problem.

What I need help with is possibly a .bat file to see if the drive is mounted or not.

I can check if the device is mounted using.

:FindDrive
if exist F:\nul goto Mounted

Open in new window


This pops up a GUI saying the drive isn't present.

What I want it to do is fire off the 'send an email' trigger

If the drive is present I do not want to send an email.

Kind Regards

James
LVL 1
Optima SystemsNetwork EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
That command alone does not pop up a GUI if the drive isn't present. You're doing something after this command that creates this error.
The test for the NUL device was only required in real DOS, not in the NT based shell.
So something like this should work:
@echo off
REM ...
:FindDrive
if exist F:\ goto Mounted
REM *** F: not found; send the email here ...

goto :eof
:Mounted
REM *** F: found, everything is fine

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Optima SystemsNetwork EngineerAuthor Commented:
Hi Thank you for your quick response!

How does one send an email from within a .bat file?

Regards

James
0
oBdACommented:
Oh, sorry; misunderstood your "At the moment I can send an email from the task scheduler", I thought you already had this handled.
You can send emails from the command line using blat.exe: www.blat.net
@echo off
setlocal
set Drive=F:
set EmailServer=smtp.domain.com
set EmailFrom=from@domain.com
set EmailTo=to@domain.com
set EmailSubject=%~nx0: Drive %Drive% not mounted!

REM ...
:FindDrive
if exist %Drive%\ goto Mounted
REM *** Drive not found; send the email here ...
REM *** Add the following if authentication is required before an email can be sent: -u <Logon User> -pw "<Logon Password>"
REM *** %~dp0 will expand the script's drive and path, including a trailing backslash.
%~dp0blat.exe -server %EmailServer% -f %EmailFrom% -t %EmailTo% -s "%EmailSubject%"
goto :eof

:Mounted
REM *** F: found, everything is fine

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Optima SystemsNetwork EngineerAuthor Commented:
Hi Thanks again for your help

I am getting a pop up.

Pop up
It also won't seem to send an email - can blat.exe send using  onsite exchange?

REM @echo off
setlocal
set Drive=F:
set EmailServer=SBSERVER2011
set EmailFrom=james@optima-systems.co.uk
set EmailTo=james@optima-systems.co.uk
set EmailSubject=%~nx0: Please enter the backup tape

REM ...
:FindDrive
if exist %Drive%\ goto Mounted
REM *** Drive not found; send the email here ...
REM *** Add the following if authentication is required before an email can be sent: -u <Logon User> -pw "<Logon Password>"
REM *** %~dp0 will expand the script's drive and path, including a trailing backslash.
%~dp0blat.exe -server %EmailServer% -f %EmailFrom% -t %EmailTo% -s "%EmailSubject%"
goto :eof

PAUSE

:Mounted
REM *** F: found, everything is fine

PAUSE

Open in new window


Here is the modified script I am using.

Regards

James
0
oBdACommented:
The error message is not produced by the script. How did you start the script to produce this message?
Please open a command prompt, "cd" into the folder where you saved the script, and run it.
What's the output?
blat.exe can send to Exchange, provided Exchange isn't configured to use secure connections only.
0
Optima SystemsNetwork EngineerAuthor Commented:
213231231.PNG
Still same issue unfortunately.

I'm able to send emails without authentication, maybe its not sending because of the error?
0
oBdACommented:
The Subject helped.
Please enter the following into a command prompt, once with the F: drive available and once when it's not available, and post the content of the output file (DriveF.txt on your desktop, or change the path to whatever you want) in a code block:
wmic.exe logicaldisk where "Caption='F:'" get Access,Caption,Description,DeviceID,DriveType,FreeSpace,MediaType,Name,Size,Status,StatusInfo>>"C:\Users\OptimaSys\Desktop\DriveF.txt"

Open in new window

0
Optima SystemsNetwork EngineerAuthor Commented:
Ejected
Access  Caption  Description     DeviceID  DriveType  FreeSpace  MediaType  Name  Size  Status  StatusInfo  
        F:       Removable Disk  F:        2                                F:                              

Mounted
Access  Caption  Description     DeviceID  DriveType  FreeSpace    MediaType  Name  Size          Status  StatusInfo  
0       F:       Removable Disk  F:        2          21096292352             F:    500096991232                      


Kind Regards

James
0
oBdACommented:
Try this:
@echo off
setlocal
set Drive=C:
set EmailServer=SBSERVER2011
set EmailFrom=james@optima-systems.co.uk
set EmailTo=james@optima-systems.co.uk
set EmailSubject=%~nx0: Please enter the backup tape


:FindDrive
for %%a in (Access Caption Description) do set %%a=
for /f "delims=" %%a in ('wmic.exe LogicalDisk WHERE "Caption='%Drive%'" GET Access^,Caption^,Description^,FreeSpace /value ^| find.exe "="') do (
	for /f "tokens=1,2 delims==" %%p in ("%%a") do (
		set %%p=%%q
	)
)
if defined Access goto Mounted

REM *** Drive not found; send the email here ...
echo ERROR: Drive '%Drive%'not found.
REM *** Add the following if authentication is required before an email can be sent: -u <Logon User> -pw "<Logon Password>"
REM *** %~dp0 will expand the script's drive and path, including a trailing backslash.
"%~dp0blat.exe" -server %EmailServer% -f %EmailFrom% -t %EmailTo% -s "%EmailSubject%"
PAUSE
goto :eof

:Mounted
REM *** F: found, everything is fine
echo Found drive '%Drive%', '%Description%'; free space is %FreeSpace% bytes.
PAUSE

Open in new window

0
Optima SystemsNetwork EngineerAuthor Commented:
Good morning,

Thanks for your help so far.

I'm still having issues unfortunately.

Do i need to do anything to set BLAT up?

blat.PNG
0
oBdACommented:
Sorry, no, no need to set anything up; my bad, forgot the body. While I was at it, it will send you which drives were found.
@echo off
setlocal
set Drive=F:
set EmailServer=smtp
set EmailFrom=backup@acme.com
set EmailTo=John.doe@acme.com
set EmailSubject=%~nx0: Please enter the backup tape

set MailBody=%Temp%\mail.tmp
:FindDrive
for %%a in (Access Caption Description) do set %%a=
for /f "delims=" %%a in ('wmic.exe LogicalDisk WHERE "Caption='%Drive%'" GET Access^,Caption^,Description^,FreeSpace /value ^| find.exe "="') do (
	for /f "tokens=1,2 delims==" %%p in ("%%a") do (
		set %%p=%%q
	)
)
if defined Access goto Mounted

REM *** Drive not found; send the email here ...
echo ERROR: Drive '%Drive%'not found.
REM *** Add the following if authentication is required before an email can be sent: -u <Logon User> -pw "<Logon Password>"
REM *** %~dp0 will expand the script's drive and path, including a trailing backslash.
 >"%MailBody%"  echo.
>>"%MailBody%"  echo Backup drive '%Drive%' not found.
>>"%MailBody%"  echo Currently available drives:
>>"%MailBody%"  echo.
wmic.exe /append:"%MailBody%" LogicalDisk GET Caption,Description,FreeSpace
"%~dp0blat.exe" "%MailBody%" -server %EmailServer% -f %EmailFrom% -t %EmailTo% -s "%EmailSubject%"
del "%MailBody%"
PAUSE
goto :eof

:Mounted
REM *** F: found, everything is fine
echo Found drive '%Drive%', '%Description%'; free space is %FreeSpace% bytes.
PAUSE

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.