Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm writing my first batch file and have two basic questions. I'm using Windows7 and wondering if there is an easy way to run something as an administrator? At the moment I just type cmd into the search bar and hit ctrl + shift + enter. I can right-click on the file and choose to run it as admin, but I was wondering if I could make this part of my batchfile. The second piece of my question is how to pass arguments to a program that I am opening. At the moment I am using the following code:

ECHO OFF

ECHO This will export a shapefile from my postgresql database

PAUSE   

::START "pgsql2shp export" "C:\Program Files (x86)\PostgreSQL\9.1\bin\pgsql2shp.exe" [-f 'D:\test.shp' -h localhost -u postgres -p 5434 spatial_data 'SELECT * FROM public.energy']
::START "pgsql2shp export" "C:\Program Files (x86)\PostgreSQL\9.1\bin\" "-f 'D:\test.shp' -h localhost -u postgres -p 5434 spatial_data 'SELECT * FROM public.energy'" "pgsql2shp.exe"

START "pgsql2shp export" "C:\Program Files (x86)\PostgreSQL\9.1\bin\pgsql2shp.exe" "-f 'D:\test.shp' -h localhost -u postgres -p 5434 spatial_data 'SELECT * FROM public.energy'"

PAUSE

ECHO shpfile complete       

PAUSE                       :: requires user input to end

I have run this file by pasting the following into the command line, after cd-ing to the following dir: C:\Program Files (x86)\PostgreSQL\9.1\bin

>pgsql2shp.exe -f D:\test.shp -h localhost -u postgres -p 5434 spatial_data "SELECT
* FROM public.energy"

My batch file code gives an error on a new terminal, but it disappears before I can read it, and I don't know what is going wrong.

share|improve this question
    
Starting cmd from Run runs it with administrator privileges. As simple as Win + R, then typing cmd. Also, instead of double-clicking the batch file, you could run it from within the command prompt so that you can see the output. It won't disappear. –  VenkatH Feb 5 '12 at 17:49
    
That doesn't seem to be the case for me. If I use the approach described in my question, Administrator appears in the title bar and I have to approve the UAC pop-up. –  celenius Feb 5 '12 at 17:53
2  
@VenkatH Your statement is inaccurate for default installs of Vista & 7. In those systems, you must explicitly select "Run as Administrator" in order to run a program with Admin-level permissions. –  Iszi Feb 5 '12 at 18:58
add comment

1 Answer

The runas command is what provides this ability for you. It will allow you to run any program AS someone else:

runas /noprofile /user:mymachine\administrator cmd

As you can see from the above quote, you simply specify the appropriate user from the list there, and boom, u run as that user.

The native issue w/ this method is the fact that the password will either a) need to be entered manually (if user isn't an admin user), or b) the password will be exposed on the runas command.

There are other utilities out there:

  • elevate is one such. You can download it here: WinAbility Elevate. This program basically lets you run any command as an elevated user even from a non admin command prompt.

To answer your second question; when passing information to the script, you have to use the proper command argument syntax:

Z:\Users\Phillip>copy con go.cmd
@echo off
echo/Passed First Parameter:  %1
echo/Passed Second Parameter:  %2
echo/
echo/Pased Third Formatted Parameter:  /d:%3
echo/
^Z
        1 file(s) copied.

Z:\Users\Phillip>go yes no ok
Passed First Parameter:  yes
Passed Second Parameter:  no

Pased Third Formatted Parameter:  /d:ok

The %1, %2, %3 syntax refers to the actual parameters passed to the program, this can include file names, programs, etc, as long as the information drops into the window as a valid file name or text entry.

The same command syntax above still applies to passing command line parameters with spaces in them as well:

Z:\Users\Phillip>go yes no "asdfasdfasdf asdf as dfa sdf "
Passed First Parameter:  yes
Passed Second Parameter:  no

Pased Third Formatted Parameter:  /d:"asdfasdfasdf asdf as dfa sdf "

However, if you are dragging/dropping, you should get into the habit of actually quoting the %3 or whatever parameter yourself (always try first, some systems do pass with the quotes already embedded, some do not, so best to test first.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.