• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

How to run a windows script file with administrator rights,

Hello, I am new to this stuff, and need to get a windows script file to run with admin priv.  
I use a command file.   .cmd to in invoke the .wsf file.
When I try to execute this, I get a permission denied.    If I right click, run as administrator -- process works.  
Is there a way I can get this to runas with admin priv.
Hope I entered this correctly.
Any help would be appreciated.

.cmd file
wscript.exe D:\lsftest\terry.wsf

DIM strInputFolder, strOutputFolder

strInputFolder = "D:\MainFrameFTPFiles\terry.txt"
strOutputFolder = "D:\Dataloads\tja.txt"

Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "D:\MainFrameFTPFiles\terry.txt" , "D:\Dataloads\tja.txt", OverwriteExisting

wscript.echo "VBS Script running"
wscript.echo "Terry.vbs"

wscript.echo "Input  Folder =" &  strInputFolder
wscript.echo "Output Folder =" &  strOutputFolder
  • 3
  • 3
4 Solutions
That's a simple file copy, there's no need for the wsf file.
A one-line batch script will do the job:
copy "D:\MainFrameFTPFiles\terry.txt" "D:\Dataloads\tja.txt"

Open in new window

As for the permissions issue: the best option is probably to create a dedicated access group for the Dataloads folder, give this group at least Change permissions on the folder, and add the user(s) running the script to this group.
There is nothing in this script that really requires elevation.
tambrosiAuthor Commented:
I should of explained further,  there is a lot more to the script than just the copy,  the script will be accessing datalogic, email
and a few other areas.  
I need to call the cmd file from a third party product to execute.  
omgangIT ManagerCommented:
You can use PsExec to execute a command in the context of another user.  Here's an example from a vbscript I've used in the past.  The command is executing a batch file on a remote machine using the supplied username (RemUser) and password

            strFullCmd = "psexec \\" & strDestComputer & " -u " & strRemUser & " -p " & strPass _
                        & " -i -d GetHelpMsg.bat " & Chr(34) & strUserName & Chr(34)

Read up on, and download, PsExec here  https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

OM Gang
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Where exactly do you get the Access denied?
If it's in the section you posted above, then changing the permissions as I suggested should work.
If it's somewhere else, the first solution is to change the respective permissions so that elevation isn't required.
If the script is started from a third-party application, then the third-party application should be run elevated.
Is the whole thing supposed to be run interactively, or as a scheduled task?
tambrosiAuthor Commented:
Thanks for the input and ideas.  This is the code that got me past the permission denied issue.  

If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , WScript.ScriptFullName & " /elevate", "", "runas", 1
End If
I need to dig into the permissions and see what is going on,  but this got me past the issue for now.   I need to red up on the  psExec  process.  

That's basically just treating the symptoms, not the cause.
Unless you're doing something that really requires administrative permissions that can not be delegated (and so far, what you've listed should be able to run without those), you should change whatever permissions are still required so that a non-admin account can run the tasks involved.
psexec won't help you here; starting psexec does require administrative permissions (because it temporarily installs a service), so in order to start it, you will already require elevation, so at this point, you can just as well continue with the script.
tambrosiAuthor Commented:
I agree,  I need to get to the root of the cause.  
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now