SEJPM
SEJPM

Reputation: 262

Execute batch script when mounting drive

I'm currently using a container-based encryption tool, which provides the data through virtual drives on windows.

As I'm storing a program that I want to run every time the virtual drive is mounted (always same drive letter if that helps), I'm looking for a possibility that would enable me to run a small batch script that starts the program every time the drive is mounted (and hence available).

Is there a way to do this using Windows task scheduler? Or do I have to write a program, to check if the particluar drive / script is available and execute it if so?

Upvotes: 2

Views: 3743

Answers (2)

Trashman
Trashman

Reputation: 1584

You can create an autorun.inf file in the root of the volume you're mounting.

open=program.exe

Where program.exe is the program you want to run. That's the easy part.

HOWEVER, this functionality is disabled on most newer versions of Windows. There are multiple ways to get around this.

  1. It appears the easiest way is to install the program/utility APUSB 47 by Leelusoft. I must caution to make sure to check your install options carefully as it also tries to install some suspicious browser add-ons, but the program itself scans free and clear of malware and you can stop the add-ons from installing.

    Note that you must have good physical control of your system (no one else can come in and insert something like a usb drive to execute malware from), and know the source of your autorun.inf (e.g. you created it yourself) and what it calls because this could introduce a security risk.

  2. Depending on your Windows version and settings, you can use the registry keys NoDriveTypeAutoRun or NoDriveAutoRun. I do have to warn that changing either of these from the Windows defaults does introduce security risks. If you have no security concern whatsoever, the easiest way is to simply delete both of these keys from both locations they might exist in:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    

    and

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    

    Likely there will only be one instance you have to delete, but unless your system is stand alone with no internet connections and no one but you has access to it, I HIGHLY RECOMMEND AGAINST THIS METHOD.

    As an alternative, since you know the drive letter of the device you're bringing online and assuming you have good control over direct access to this machine, you can use NoDriveAutoRun to ONLY allow autorun on the drive you're bringing online. This solution should be reasonably safe. Since I believe that's the safest option, I will present that here. You can search for specifics on the other methods if you'd like.

    To do this, first create a backup of your registry. Then, open up the registry editor, and go to this location:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    

    If the key "NoDriveAutoRun" does not exist, create one of type REG_DWORD

    Now, for the value... here you have to exercise your binary/hex knowledge. Imagine 32 bits:

    00000011111111111111111111111111
    

    The first 6 are zeros, and each of the 26 1's represent a drive Z through A from left to right. If this value is set, Windows will not allow autorun on ANY drive. You want to enable on one drive. I don't know what drive letter it is, but let's just say it's drive F you want to enable. Then, you would have the following:

    00000011111111111111111111011111
    

    Now, you can't just enter the binary, you have to convert this to hex. Hopefully You have a calculator handy. If you have Windows 7 or earlier, you can use the built-in calculator with View/Programmer to do the conversion. If you have Windows 8, I'm sorry.

    In this case, this translates to

    0x3FFFFDF
    

    And that is the value you will enter for the registry key NoDriveAutoRun

    If the key "NoDriveTypeAutoRun" exists, delete it.

    Also check HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer

    For the key "NoDriveTypeAutoRun" and delete that if it exists.

Upvotes: 2

npocmaka
npocmaka

Reputation: 57252

It is possible with the scheduler and SCHTASKS /RI ONEVENT switch (available from vista and above) - you'll need elevetated permissions.It will activate a task on a certain event , but you need to check your event log for the exact even when the drive is mounted(google search gave me this but anyway you'll need to check your events).

Upvotes: 0

Related Questions