Wednesday, April 13, 2016

How to check for admin rights from a Windows batch file

Simple check for administrative permissions in a batch file:

REM Check for admin rights - net session must run as admin
net session >nul 2>&1
if %ErrorLevel% NEQ 0 (
    echo You must right-click and select "RUN AS ADMINISTRATOR" to run this script.
    exit /B

Wednesday, February 17, 2016

Another HEREDOC style for Windows Batch Files

I wanted to pipe a SQL query to sqlplus in a batch file, without using a temp SQL file.

That technique involves echo'ing the SQL statements to a temp file, then call sqlplus on that file:

REM create the temp SQL file
echo SELECT USER >>temp.sql
echo FROM DUAL;  >>temp.sql

REM execute sqlplus
sqlplus -S / @temp.sql

REM cleanup!
del temp.sql

An alternative is to wrap the echo statements in parentheses "( ... )", so they execute as a block, then pipe "|" the result to sqlplus:

    echo SELECT USER
    echo FROM DUAL;
) | sqlplus -S /

Admittedly, this is not much prettier than the first example, but it's self-contained, and doesn't leave random temp files that may or may not get cleaned up.