+ Post New Thread
Results 1 to 13 of 13
Windows 7 Thread, Using batch file to copy files to protected folders in Technical; Hi there, I have a batch file that I want to run as post-imaging task (with our Kace K2000 imaging ...
  1. #1

    Join Date
    Nov 2009
    Posts
    63
    Thank Post
    14
    Thanked 2 Times in 2 Posts
    Rep Power
    10

    Using batch file to copy files to protected folders

    Hi there,

    I have a batch file that I want to run as post-imaging task (with our Kace K2000 imaging system) that copies a file from a network share into a folder in C:\Program Files. However, on Windows 7 I get an error when this is run unless I right click and choose "Run as administrator." Obviously I can't right click to do this if the script is running automatically in the Kace imaging environment. In the script, is there a way I can specify "Run as administrator" as a command?

    Thanks!

  2. #2
    detjo's Avatar
    Join Date
    Feb 2008
    Posts
    355
    Thank Post
    13
    Thanked 47 Times in 39 Posts
    Rep Power
    31
    Would GPO prefs/Comp config/Windows settings/Files work for you ?

  3. Thanks to detjo from:

    MarsRed (29th September 2012)

  4. #3

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,631
    Thank Post
    890
    Thanked 1,314 Times in 798 Posts
    Blog Entries
    1
    Rep Power
    441
    Does Kace not run the script as administrator to prevent these issues? You running the script would force you to right click but I would have thought Kace would run as admin by default.
    You do have another option of using either a start up script (which runs as SYSTEM) or using GPP to check/copy the file as well.

  5. Thanks to ZeroHour from:

    MarsRed (29th September 2012)

  6. #4

    Join Date
    Nov 2009
    Posts
    63
    Thank Post
    14
    Thanked 2 Times in 2 Posts
    Rep Power
    10
    Quote Originally Posted by detjo View Post
    Would GPO prefs/Comp config/Windows settings/Files work for you ?
    Hmm...I don't see Files listed there. I'm on a 2003 domain, would that make a difference? What exactly would Files let me do?

  7. #5

    Join Date
    Nov 2009
    Posts
    63
    Thank Post
    14
    Thanked 2 Times in 2 Posts
    Rep Power
    10
    Quote Originally Posted by ZeroHour View Post
    Does Kace not run the script as administrator to prevent these issues? You running the script would force you to right click but I would have thought Kace would run as admin by default.
    You do have another option of using either a start up script (which runs as SYSTEM) or using GPP to check/copy the file as well.
    Don't quote me, but I think Kace runs post installation tasks as whichever user logs into the machine. It sounds like the recommended route is using Group Policy, though I was hoping to contain this process (just copying one application preference file) during imaging. I have no trouble with it on the XP machines!

    Thanks all for your help.

  8. #6
    detjo's Avatar
    Join Date
    Feb 2008
    Posts
    355
    Thank Post
    13
    Thanked 47 Times in 39 Posts
    Rep Power
    31
    Quote Originally Posted by MarsRed View Post
    Hmm...I don't see Files listed there. I'm on a 2003 domain, would that make a difference? What exactly would Files let me do?
    It would copy the files you specify, from source to destination - and Server 2003 would make all the difference.

    It was introduced in server 2008 but can be added to 2003. You should def look into it
    MS have an overview paper on it here ... and this one might also be of interest

  9. Thanks to detjo from:

    MarsRed (29th September 2012)

  10. #7

    Join Date
    Nov 2009
    Posts
    63
    Thank Post
    14
    Thanked 2 Times in 2 Posts
    Rep Power
    10
    Quote Originally Posted by detjo View Post
    It would copy the files you specify, from source to destination - and Server 2003 would make all the difference.

    It was introduced in server 2008 but can be added to 2003. You should def look into it
    MS have an overview paper on it here ... and this one might also be of interest
    Thanks for the links...good to know about and certainly something I'll keep in mind as we (hopefully) move to 2008.

    Am still interested in whether there's a way to run the batch file as administrator. Any chance there's a command that will do this?

  11. #8

    ZeroHour's Avatar
    Join Date
    Dec 2005
    Location
    Edinburgh, Scotland
    Posts
    5,631
    Thank Post
    890
    Thanked 1,314 Times in 798 Posts
    Blog Entries
    1
    Rep Power
    441
    One trick I used to use was a custom AutoIT exe, autoIT can be preloaded with elevated details and then run the command for you Function RunAs
    Not sure about batches but it may be possible in vbs.

  12. #9

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,236
    Thank Post
    1,057
    Thanked 1,067 Times in 624 Posts
    Rep Power
    740
    Quote Originally Posted by ZeroHour View Post
    One trick I used to use was a custom AutoIT exe, autoIT can be preloaded with elevated details and then run the command for you Function RunAs
    Not sure about batches but it may be possible in vbs.
    You can call the batch file in an AutoIT script using the RunAs - example:

    RunAs("userid", "domain", "password", 0, @COMSpec & " /c " & "d:\a folder\script\nameofbatch.cmd")

  13. #10
    detjo's Avatar
    Join Date
    Feb 2008
    Posts
    355
    Thank Post
    13
    Thanked 47 Times in 39 Posts
    Rep Power
    31
    Something from the technet site. I'll paste this here as it's useful for future reference (and just in case it ever get lost over at MS!)
    -----------------------------------------------------------------------------------------------------------------------------

    use a utility such as RunAs any time you need to run a program as an Administrator. And so, seeing as how everyone always does whatever Microsoft tells you to do, you type the following command at the command prompt and try using the RunAs utility to run the script C:\Scripts\Test.vbs:

    runas /profile /user:fabrikam\kenmyer "C:\Scripts\Test.vbs"

    And hereís what happens when you press ENTER:

    Attempting to start C:\Scripts\Test.vbs as user "fabrikam\kenmyer" ...
    RUNAS ERROR: Unable to run - C:\Scripts\Test.vbs
    193: C:\Scripts\Test.vbs is not a valid Win32 application.

    Itís at this point that people typically do one of two things: either they give up, or they start debugging the script, trying to figure out why their code isnít valid. But hereís the deal: the error message says nothing about the script or the script code being invalid, it simply states that Test.vbs is not a valid Win32 application. Thatís because RunAs is expecting to run an executable file. Test.vbs is obviously not an executable file, so the command fails. Most likely your code is just fine; the problem is that youíre trying to run a script rather than a .exe file.

    So if RunAs accepts only executable files then obviously we canít run a script using RunAs, right? Well, not necessarily. Although Test.vbs might not be an executable file, the two Windows script hosts - Cscript.exe and Wscript.exe - are executable files. The secret to running a script under RunAs is to call one of the two script hosts, passing along the name of the script as a command-line argument. For example, this RunAs command will fire up Cscript.exe and then run the script C:\Scripts\Test.vbs:

    runas /profile /user:fabrikam\kenmyer "cscript.exe C:\Scripts\Test.vbs"

    You can see how easy that is. You simply call the RunAs command along with the following parameters:

    /profile, which causes the appropriate user profile to be loaded. This is optional, but makes it more likely RunAs will be able to do what it needs to do.

    /user:fabrikam\kenmyer, which is the user account (in the form domain\user_name) under which the process is to run.

    ďcscript.exe C:\Scripts\Test\.vbsĒ, which is the process you want RunAs to execute. Note that the entire command must be enclosed in double quote marks, and should be typed the same way you would type the command from the command prompt.

    That, by the way, is the second problem people encounter when trying to run a script under RunAs. As long as your scripts are in the folder C:\Scripts you wonít have any problems; thatís because C:\Scripts has no spaces in the path name. But suppose your scripts are in C:\Documents and Settings\kenmyer\Scripts. If you were at the command prompt and you wanted to run a script in this folder you would need to surround the path in double quotes; thatís because the path includes blank spaces. In other words:

    cscript.exe "C:\Documents and Settings\kenmyer\Scripts\Test.vbs"

    Logically enough, then, you might then expect the RunAs equivalent to look something like this:

    runas /profile /user:fabrikam\kenmyer "cscript.exe "C:\Documents and Settings\kenmyer\Scripts\Test.vbs""

    Unfortunately, thatís not the case: the double quotes nested inside double quotes simply causes RunAs to give up and display the usage screen. Tossing double quotes inside double quotes wonít work. Nor will it work to try double double quotes or triple double quotes or any other variation of quotes inside of quotes. Like a three-year-old child (or many thirty-three-year-old adults) RunAs wants things to be exactly right or it wonít even try to help you.

    Fortunately, itís fairly easy to make RunAs happy. All you need to do is use the \ character to ďescapeĒ any double quote marks that must to be embedded within the process path. This command will work:

    runas /profile /user:fabrikam\kenmyer "cscript.exe \"C:\Documents and Settings\kenmyer\Scripts\test.vbs"\"

    Notice how we put our path together. We start out with a double quote mark and then the name of the script host:

    "cscript.exe
    At this point we need to insert the path, which includes blank spaces (and thus needs to be surrounded by double quotes). To do that we simply put a \ immediately before the first double quote and another \ immediately after the second double quote:

    "cscript.exe \"C:\Documents and Settings\kenmyer\Scripts\test.vbs"\

    All we have to do then is tack on the ending double quote and weíre done:

    "cscript.exe \"C:\Documents and Settings\kenmyer\Scripts\test.vbs"\"

    Itís simple, but it works. Just remember to bracket long file paths and any other item requiring double quotes and youíll be home free.

  14. #11

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,236
    Thank Post
    1,057
    Thanked 1,067 Times in 624 Posts
    Rep Power
    740
    .....and that is why I script in AutoIT and PS.

  15. #12

    plexer's Avatar
    Join Date
    Dec 2005
    Location
    Norfolk
    Posts
    13,278
    Thank Post
    615
    Thanked 1,567 Times in 1,407 Posts
    Rep Power
    412
    Can you do the same with a bat running it with cmd.exe ?

    Ben

  16. #13


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    8,716
    Thank Post
    220
    Thanked 2,616 Times in 1,927 Posts
    Rep Power
    777
    Not sure if these links will help?


    Quote Originally Posted by MarsRed View Post
    Don't quote me, but I think Kace runs post installation tasks as whichever user logs into the machine.
    I think you are correct regarding post installation tasks, but from what I have read online, scripts and managed installations are run under the local system account. Perhaps you simply need to choose a different method to deploy the files on your Kace box?

    As a last resort, it might also be worth speaking to Kace tech support or posting your question on the IT Ninja website.

SHARE:
+ Post New Thread

Similar Threads

  1. Software to copy files by extension
    By cookie_monster in forum Windows
    Replies: 6
    Last Post: 29th June 2009, 12:57 PM
  2. How to copy files from two domains
    By timbo343 in forum Scripts
    Replies: 5
    Last Post: 18th February 2008, 09:43 PM
  3. Failed to copy files from CD
    By Dos_Box in forum Learning Network Manager
    Replies: 13
    Last Post: 12th April 2007, 10:42 AM
  4. Replies: 19
    Last Post: 20th June 2006, 03:53 PM
  5. script to copy files to machines
    By browolf in forum Scripts
    Replies: 1
    Last Post: 28th November 2005, 10:28 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •