Link to home
Start Free TrialLog in
Avatar of mascoloj
mascolojFlag for United States of America

asked on

Batch File to run with Admin priviledges

I wanted to know if there is an easy way to get a batch file to run with administrator priviledges. I have a batch file which is called by a group policy for users. It copies folders and files for them into a directory. It works great for the admin, but for users they get "unknown error". If I can get the script to run with admin priviledges then I think I can solve my issue. Any help would be great.

Mas
Avatar of Bradley Fox
Bradley Fox
Flag of United States of America image

If you assign the script as a Computer Startup Script instead of a User Logon Script in the GPO it will run with Admin rights.  This may only work for you if the script does the same thing for every user.
Avatar of mascoloj

ASKER

I don't think that will work. The script needs to run when the user logs on. Thanks for the info though.
What exactly does the script do?  Can you post it here?
Not sure if this works with scripts/batch files but have a look at this

http://www.microsoft.com/technet/prodtechnol/windows2000serv/downloads/epal.mspx
Alright, things have progressed a little since my last post. Here's what I'm trying to do, where I am and my problem. If anyone has a problem with this question changing a bit I will open a new one.

I'm converting user profiles to roaming profiles. In doing so I have excluded several folders from the profile including the Desktop, Application Data and My Documents. With the roaming profile I have also set the GPO to delete the local profile when the user logs off.

All of this creates a problem though.... Because some folders are excluded from the profile, their contents (files and folders) do not get copied to their new home as they should. So I need an ingenious way to copy the info from these folders when the user logs on.

I first made a batch file to copy what I needed but when it came to copying the desktop, and "unknown error" appeared. The error does not appear when I am logged in as administrator. The error is permissions based and I won't change the permissions to accomidate this copy. So I found a VB script to run my batch file as the administrator with the password. This caused the %username% variable which I use in the batch file to switch from being the user who is logged on to the administrator running the script. So I made another batch file to capture the username and pass it to the VB script which then passes it to the batch file which does the copying. In theory it's great and on an XP workstation it works but on Windows 2003 it craps out as soon as the batch file that does the copying starts. The screen go way to fast for me to see any errors and inserting a pause in the batch does nothing to help. Below are my scripts in the order they run seperated by lines.

I also raised the points to this question because this is starting to annoy me and I really want it fixed!

*****Username.bat*****
set user=%username%
call vb.vbs
_________________________

*****VB.vbs*****
Option explicit

    Dim oShell
    set oShell= Wscript.CreateObject("WScript.Shell")
    oShell.Run "runas /user:administrator@domain.com ""copyuser.bat %user%"""
    WScript.Sleep 100
    oShell.Sendkeys "PASSWORD~"
    Wscript.Quit
________________________

*****Copyuser.bat********
If Exist "\\sqltest\ProfileSet\%1.txt" EXIT

If Exist "c:\documents and settings\%1\local settings\Application Data\Microsoft\Outlook\outlook.pst" md "\\sqltest\Users\%1\Outlook

If Exist "c:\documents and settings\%1\local settings\Application Data\Microsoft\Outlook\outlook.pst" copy "c:\documents and settings\%1\local settings\Application Data\Microsoft\Outlook\outlook.pst" "\\sqltest\Users\%1\Outlook\" /y

If EXIST "c:\documents and settings\%1\My Documents" md "\\sqltest\Users\%1\My Documents"

If EXIST "c:\documents and settings\%1\My Documents" xcopy "c:\documents and settings\%1\My Documents" "\\sqltest\Users\%1\My Documents\" /e /i /h /y

xcopy "C:\Documents and Settings\%1\Desktop" "\\sqltest\Desktops\%1" /e /i /h /y

copy "c:\profileset.txt" "\\sqltest\ProfileSet\%1.txt"


I forgot to mention that the server that I am trying to run the scripts from are a Windows 2003 Server with SP1 installed. All scripts are being run from the System32 folder via a GPO which calls username.bat.
Change line

oShell.Run "runas /user:administrator@domain.com ""copyuser.bat %user%"""

to

oShell.Run "runas /user:administrator@domain.com ""cmd /k copyuser.bat %user%"""


This will keep the cmd window open so you can see the error
I just tried it but it didn't work.
ASKER CERTIFIED SOLUTION
Avatar of Bradley Fox
Bradley Fox
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, I put that in. What I originally got was the first bat file opening, I saw it call the vb script, the second bat would open and then shut quickly and then the first bat would close. Now I get both opening and closing quickly. The first one doesn't even wait for the second one to finish. Also, I ran the script about 10 times to try to get a glimpse of the error - what I found is that there definately is an error but other than that I can't tell what it is.

I want to thank you for helping me with this. Anything else you can think of? Is there a way to print the screen to a text file?
I placed the cmd /k piece infront of runas and the window finally stayed open. I'll keep you up on what happens. Right now it looks like the username/password is wrong.
OK, I'm a freakin idiot. The password I was using in the script was for my domain administrator account. Right now though I'm in my TEST system - the domain password is totally different. Thanks a lot for your help. You get the points!
Got one more question on securities now. I'll be posting another question on it. Feel free to help me again!