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

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?
pauldownhamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Don ThomsonCommented:
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
Joshua GrantomSenior Systems AdministratorCommented:
Compatibility options do not stay in place when you apply them to a Removable Drive.
0
Joshua GrantomSenior Systems AdministratorCommented:
You can create a VBscript, this may work

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "astra32.exe", ",", "", "runas", 1
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

pauldownhamAuthor Commented:
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
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
Joshua GrantomSenior Systems AdministratorCommented:
A vbscript is an entirely different file ending in vbs.

It would look similar to attached
runasadmin.vbs
0
pauldownhamAuthor Commented:
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
pauldownhamAuthor Commented:
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
Joshua GrantomSenior Systems AdministratorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pauldownhamAuthor Commented:
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
Joshua GrantomSenior Systems AdministratorCommented:
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
pauldownhamAuthor Commented:
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
Joshua GrantomSenior Systems AdministratorCommented:
i updated the quotes in my post after a few minutes of my original post, maybe the edit will work?
0
pauldownhamAuthor Commented:
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
pauldownhamAuthor Commented:
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
Joshua GrantomSenior Systems AdministratorCommented:
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
pauldownhamAuthor Commented:
Unfortunately, that made no difference. I changed my message box to strEntirePath, and that bit was fine.
0
Joshua GrantomSenior Systems AdministratorCommented:
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
pauldownhamAuthor Commented:
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
pauldownhamAuthor Commented:
Problem solved and then some further help too - many thanks!
0
Joshua GrantomSenior Systems AdministratorCommented:
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
pauldownhamAuthor Commented:
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
Joshua GrantomSenior Systems AdministratorCommented:
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
pauldownhamAuthor Commented:
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
Joshua GrantomSenior Systems AdministratorCommented:
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
pauldownhamAuthor Commented:
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
Joshua GrantomSenior Systems AdministratorCommented:
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
pauldownhamAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.