Skip to content

Instantly share code, notes, and snippets.

@adamz01h
Created April 19, 2022 15:05
Show Gist options
  • Select an option

  • Save adamz01h/ed7ce54e8d8ee74561d8012b54b5830a to your computer and use it in GitHub Desktop.

Select an option

Save adamz01h/ed7ce54e8d8ee74561d8012b54b5830a to your computer and use it in GitHub Desktop.
Backup a windows machine to a nas.
::::::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights V2
::::::::::::::::::::::::::::::::::::::::::::
@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:init
setlocal DisableDelayedExpansion
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B
:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
REM Run shell as admin (example) - put here code as you like
REM set name=backupclient
REM set password=backupclient
ECHO Account information must match the user on the NAS.
ECHO Shared Account Username:
set /p name=
ECHO Shared Account Password:
set /p password=
ECHO Backup Network Path:
set /p backuppath=
net user %name% %password% /add
net localgroup "Backup Operators" %name% /add
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /f /v "%name%" /t REG_DWORD /d 0
ECHO Added %name% to hidden UserList
wbadmin enable backup -addtarget:%backuppath% -schedule:20:00 -include:C: -allCritical -allowDeleteOldBackups -user:%name% -password:%password% -quiet
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment