Solved

How to run a batch file with its contents being run with Administrator rights

Posted on 2014-07-31
28
201 Views
Last Modified: 2014-08-01
To automatically report the technical specification of older PCs and laptops, I'm using Astra32 software in batch mode, loaded on a memory stick. This runs the program and outputs a CSV file to the memory stick. So just plug in the stick, wait for the contents to list, and click on the batch file name. Job done.

This worked fine on XP machines, but now the older PCs are increasingly having Vista and even Win7 loaded, the operation has become much longer as I'm having to manually update the properties of the Astra EXE file to tick the "run as administrator" box under the Advanced tab each time, and then right clicking the batch file itself to select the "run as administrator" option, and then answering the UCA permission too. It works, but pretty time-consuming when there's a pile to do.

I've tried automating it again by changing the batch file to contain: runas /noprofile /user:\machinename\administrator "astra32.exe", but this trips up on either not knowing the machinename or the administrator password, or because the password is a blank.

So any ideas on making this simple again please?
0
Comment
Question by:pauldownham
  • 14
  • 12
  • +1
28 Comments
 
LVL 14

Expert Comment

by:Don Thomson
ID: 40232437
On the "astra32.exe"  Under Properties - Compatibility - Check always run as Administrator
Then Search for Cmd.exe = and do the same (cmd.exe runs the batch program)

Right Mouse Click the Batch file and see if the checkbox under Compatibility is greyed out - if it is not - check it
If the Batch file is being run from a shortcut - then check the checkbox under advanced
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40232482
Compatibility options do not stay in place when you apply them to a Removable Drive.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40232504
You can create a VBscript, this may work

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", ",", "", "runas", 1
0
 

Author Comment

by:pauldownham
ID: 40232538
DTH: Thanks, but as Joshua says, these ticks are lost when the memory stick is moved on to the next PC. Also, CMD.EXE is located on each PC, so again this would need changing every time.

Joshua: Thanks, but how is this implemented? Just lines in a batch file?  Also, there are some parameters after the "astra32.exe" to format the report, where would these go?
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 40232555
When you run a batch file as administrator, ALL things it starts run as administrator.  I don't know why you're telling it to run the program itself as administrator.

Also don't know where you're running these... on clients?  On a domain? On a corporate netwrok...

I collect system data all the time using System Information for Windows - single executable plus license file.  I use PSEXEC and run it from a server remotely on all systems as an admin, dumping the output to HTML or CSV files on my local system.  I can collect all systems in my client network from one system in about 1 minute per machine (just have to remember to run it from the server where there are no share connection limits).  (It's run from a small batch file too that does a couple of other things).
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40232571
A vbscript is an entirely different file ending in vbs.

It would look similar to attached
runasadmin.vbs
0
 

Author Comment

by:pauldownham
ID: 40232714
Lee: Thanks. Well, it didn't for me ... when I started the batch file under "run as administrator" the astra program stopped at startup with a rights message. These are standalone laptops mostly. I'm stuck with astra because the next stage is that the memory stick report is selectively chopped by a bespoke database application, so any other format of report would involve a massive amount of knock-on work.

Joshua: Thanks, I'll try that out tomorrow.
0
 

Author Comment

by:pauldownham
ID: 40233614
Joshua: Progress!  The VB idea seems to work, but not when the necessary parameters are inserted. The parameters are: "/rc filename /q"
These inform the program to output a report called filename as a CSV file, and to run silently.
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40234057
Good!

If there are multiple parameters with spaces then you have to use a single quote around double quotes like so

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", '"/rc filename /q"', "", "runas", 1

Open in new window


If that doesnt work you may try a "-" instead of "/"

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", '"-rc filename -q"', "", "runas", 1

Open in new window

0
 

Author Comment

by:pauldownham
ID: 40234108
Thanks Joshua, in fact it worked with the quotes around the other way, double on the outside and single on the inside. So subject to a bit of testing I think we are there!

Final (cheeky) follow up .... the filename is specified as e:\folder\results.csv, because in most cases the memory stick is allocated as the E drive - but is there a VB way of saying thisdrive\folder\results.csv so that it always saves on the stick regardless of allocation?

Thanks again, and points on the way.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40234135
Try this

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", "'/rc' strDrive & '\folder\results.csv' '/q'", "", "runas", 1

Open in new window

0
 

Author Comment

by:pauldownham
ID: 40234236
Hi, I notice you've left the quotes around the original way that didn't work on the parameter section, so I swapped them over again, but now the script reports an end of statement error.


objShell.ShellExecute "astra32.exe", '"/rc" strDrive&"\folder\results.csv" "/q'", "", "runas", 1

I guess its the syntax of all those quotes!
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40234254
i updated the quotes in my post after a few minutes of my original post, maybe the edit will work?
0
 

Author Comment

by:pauldownham
ID: 40234307
OK, I've got the updated version now.
So close!  It's now running Astra but tripping up with a use of parameters message. I'm assuming the string generated by the bit in double quotes has still got a syntax issue?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:pauldownham
ID: 40234515
I've been playing ...

First the parameters needed a space before the forward slash, but it still didn't work. So, I added a message box to check that an expansion of the strDrive to include the whole string was correct, which it was, but when substituted back in the command line I still get the parameters being questioned by Astra.

 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
strDrive = "  /rc " & strDrive & "\folder\filename /q"
MsgBox(strDrive)

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", "strDrive", "", "runas", 1

If I put the literal contents of the expanded strDrive variable in its place (with single quotes around) it works. So, now just seems to be a syntax error in using strDrive?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40234538
It was happening because you were calling strDrive from within itself. Try this

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
strEntirePath = "  /rc " & strDrive & "\folder\filename /q"

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", "strEntirePath", "", "runas", 1

Open in new window

0
 

Author Comment

by:pauldownham
ID: 40234563
Unfortunately, that made no difference. I changed my message box to strEntirePath, and that bit was fine.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40234566
Try it without quotes around strEntirePath

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
strEntirePath = "  /rc " & strDrive & "\folder\filename /q"
MsgBox(strEntirePath)

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", strEntirePath, "", "runas", 1

Open in new window

0
 

Author Comment

by:pauldownham
ID: 40234587
Doesn't run at all then!

This is probably taking too much of your time ... the essential bit of running as an Administrator has been solved, so I'm feeling guilty trying to get this drive bit sussed too!
0
 

Author Closing Comment

by:pauldownham
ID: 40234604
Problem solved and then some further help too - many thanks!
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40234626
Try this one. Ive got some time. I would like to know if it works too!

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
strEntirePath = ""/rc " & strDrive & "\folder\filename /q""
MsgBox(strEntirePath)

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", strEntirePath, "", "runas", 1

Open in new window

0
 

Author Comment

by:pauldownham
ID: 40234701
Well the double double quotes caused a statement error, and lots of mixing single and doubles got into a mess, so I tried adding chr(34) at each end of the original and that produced the correct result in the MsgBox, ie. all the parameters in place and a double quote each end. But still no luck when trying the command line with / without / in brackets / in single quotes etc. Always Astra bringing up the parameter error message.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40234825
This is my last idea to try lol. It uses concatenation instead

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
strMyPath = "/rc " & strDrive
strMyPath = strMyPath & "\folder\filename /q"

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", strMyPath, "", "runas", 1

Open in new window

0
 

Author Comment

by:pauldownham
ID: 40235062
Sorry, just the same. Doesn't run without quotes around strMyPath, and brings up the Astra parameters error message with quotes.  My MsgBox check shows the parameters line correctly constructed though - as before with the other methods.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40235073
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strPath = objShell.CurrentDirectory
strDrive = objFSO.GetDriveName(strPath)
strMyPath = "/rc " & strDrive
strMyPath = strMyPath & "\folder\filename /q"

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe",'strMyPath',"","runas", 1

Open in new window

0
 

Author Comment

by:pauldownham
ID: 40235094
New Idea!

The drive allocations are only ever going to be something like C to G, so how about a short series of five IF/THEN lines, where if the drive letter is found to be say a "D" then just run a command line containing the D version of fully type literal parameters?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40235100
I think if you use that idea and there is another drive it will write to that drive or try to write to a CD-ROM before it even gets to the drive letter of your USB drive.
0
 

Author Comment

by:pauldownham
ID: 40235295
Well, it has worked so far during tests, and for the very few cases where the drive is not going to be C to G, I'm happy to intervene manually. So, I'm calling that done! Cheers.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now