Page 1 of 1

Intro to Admin rights, and Hybrid Scripting.

Posted: October 12th, 2015, 5:01 am
by ApatheticEuphoria
If for some reason you need your batch file to run as administrator:

Code: Select all

@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

<YOUR BATCH SCRIPT HERE>


To create a temporary .VBS file within a Batch Script:
(Add a colon ":" to the beginning of each line of code for the VBS script. Then use Findstr command to search for code and create temp VBS file with said code) [Here i use a VERY simple example where the VBS does nothing more than press a key,and type some text]

Code: Select all

@ echo off
:Set wshshell = WScript.CreateObject("WScript.Shell")
:wshshell.SendKeys "{CAPSLOCK}"             
:wshshell.SendKeys "This is an example."
:WScript.Sleep 5000 '// Sleep for 5 seconds
FindStr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs


Save your file as Anything.bat , Change file type to all files and your ready to go.

Enjoy!! :mrgreen:

==========More useful Batch Examples============

How to open a website using Batch:

Code: Select all

@ echo off
start http://google.com


How to loop code in a Batch File:

Code: Select all

: loop
[Insert Code Here]
goto loop


Drift King wrote:A lot of people are scripting for jarvis. So, this is something that may interest the batch scripters.
this program will give you access to some more options for your batch script.

download: https://copy.com/Zw4C7ti6iuel/Bat2EXE%20setup.exe

features:
1. create standalone exe files from your bat file
2. make it silent (So that the CMD window doesn't pop up every time you execute it.)
3. Give icon and version information.