Skip to content

Instantly share code, notes, and snippets.

@farOverNinethousand
Created January 7, 2023 16:51
Show Gist options
  • Save farOverNinethousand/48818a7a7921da02e53ec0892caaf349 to your computer and use it in GitHub Desktop.
Save farOverNinethousand/48818a7a7921da02e53ec0892caaf349 to your computer and use it in GitHub Desktop.
Mounts a pre defined Windows network share with given credentials. Can be used as a workaround for Windows machines which fail to permanently store network share login credentials.
@echo off
REM Script that can be used as a workaround when Windows fails to store login credentials of added share connections.
REM Version 1.1
REM Use UTF-8 encoding in Command Prompt (important to support all kinds of passwords via pw.txt)
REM Thx to: https://stackoverflow.com/questions/11962172/echo-utf-8-characters-in-windows-batch
chcp 65001 >NUL
REM http://steve-jansen.github.io/guides/windows-batch-scripting/part-2-variables.html
SETLOCAL ENABLEEXTENSIONS
SETLOCAL enableDelayedExpansion
REM Set username for share
SET "user=YourNetworkShareUsername"
SET "pwfile=pw.txt"
REM Set path to share
SET "sharepath=\\server\Folder"
SET "driveletter=Z:"
SET /A unmount_on_success=0
REM Errorhandling: Check for missing password file
if not exist %pwfile% (
color 04
echo Warnung: %pwfile% existiert nicht^^!^^!
echo Erstelle eine Datei mit dem Namen %pwfile% imselben Ordner, in dem sich dieses Script befindet, trage dein Passwort in die erste Zeile ein und speichere diese.
echo Dieses Fenster schliesst sich in ein paar Sekunden...
ping -n 10 localhost >NUL
exit
)
<"%pwfile%" set /p password=
REM Check for empty string stolen from: https://www.tutorialspoint.com/batch_script/batch_script_empty_string.htm
echo Passwort aus %pwfile% = "!password!"
if "!password!" == "" (
color 04
echo Passwort darf nicht leer sein, ist aber leer^^!
echo Dieses Fenster schliesst sich in ein paar Sekunden...
ping -n 5 localhost >NUL
exit
)
if exist %driveletter% (
echo Netzlaufwerk %driveletter% existiert bereits, entferne es...
net use %driveletter% /DELETE>nul
echo Warte ein paar Sekunden...
ping -n 3 localhost >NUL
)
REM Check for failure of removing network share
IF %ERRORLEVEL% NEQ 0 (
color 04
echo Fehler beim unmounten von %driveletter%^^!
echo Dieses Fenster schliesst sich in ein paar Sekunden...
ping -n 5 localhost >NUL
exit
)
echo Mounte Netzwerkfreigabe %sharepath% als Laufwerksbuchstabe %driveletter% ...
net use /persistent:yes %driveletter% %sharepath% /user:"%user%" "!password!"
REM Check for failure of mounting network drive
IF %ERRORLEVEL% NEQ 0 (
color 04
echo Uff da ist etwas schiefgelaufen^^! Pruefe deine Zugangsdaten.
echo Dieses Fenster schliesst sich in ein paar Sekunden...
ping -n 5 localhost >NUL
exit
)
REM Change CMD color to signal the user that everything is alright.
color 0a
echo Sieht gut aus :)
if %unmount_on_success% EQU 1 (
echo Unmounte Netzlaufwerk %driveletter% wieder...
net use %driveletter% /DELETE>nul
IF %ERRORLEVEL% EQU 0 echo %driveletter% Erfolgreich unmounted
)
echo Dieses Fenster schliesst sich in ein paar Sekunden...
ping -n 5 localhost >NUL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment