Remote Pair Shared Drive

We’ve had an issue where users in our company would request access to a shared drive, but they weren’t at their computer when our IT department was available to log in and map it. To solve this, I made a script that will create a Batch file in a specified users’ Startup Folder. After the Batch file runs, it deletes itself.

remotePairNetworkDrive.bat

cls
@pushd %~dp0
@echo off
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
color 0A
:top
cls
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
color 0A
echo What computer (name or IP) do you want to run this on?
set /p address=
ping -n 1 "%address%" | findstr /r /c:"[0-9] *ms"
if %errorlevel% == 0 (
    GOTO:one ) else (
    echo [41mUnable to find %address%. Device may be offline. Try again?[0m
    pause
    GOTO:top
    )
:one
cls
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
color 0A
echo What computer (name or IP) do you want to run this on? %address%
echo What user do you want this installed for?
set /p user=
echo What is the address for the drive you'd like to pair?
set /p drive=
echo What letter would you like to map the drive to? Leave blank if any.
set /p letter=
if not defined letter (
    GOTO any
) ELSE (
    goto specified
)
:any
set command=net use * "%drive%" /YES
set /a r = %random%
echo @echo off > "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%r%.bat"
echo %command% >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%r%.bat"
TYPE "deleteScript.txt" >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%r%.bat"
GOTO two
:specified
set command=net use "%letter%:" /YES
GOTO two
echo @echo off > "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%address%.bat"
echo %command% >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%address%.bat"
TYPE "deleteScript.txt" >> "\\%address%\c$\Users\%user%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\pairNetworkDrive%letter%.bat"
:two
popd
cls
:three
ECHO ============================
ECHO REM MAPPING TOOL
ECHO ============================
ECHO ____________________________
echo %drive% has been mapped on %address% for %user%.
echo Please have %user% restart %address% to see changes.
pause

:: Love, CJ Pollock

IMPORTANT: You need to use this next bit to make sure the target computer’s startup script deletes itself after execution. Simply name it deleteScript.txt and put it in the same directory as the previous Batch file:

deleteScript.txt

(goto) 2>nul & del "%~f0"

Download: remotePairNetworkDrive.zip
(right-click and Save Link As)

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Connecting to %s