1

I can right click on the DEC16.bat file and it will run. I am having trouble including it in a script to run from a flash drive. The PowerShell script essentially copies over a bunch of install files onto a client's computer.

Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS H:\> $script = "\\xxxhsfmsl03\adap\Database\Install\AugKA\DEC16.bat"
PS H:\>
PS H:\> Start-Process powershell -Credential “xxx\xxxvis_desktop” -ArgumentList '-noprofile -command &{Start-Process $script -verb runas}'
Start-Process : This command cannot be run due to the error: The directory name is invalid.
At line:1 char:1
+ Start-Process powershell -Credential “xxx\xxxvis_desktop” -ArgumentList '-noprof ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

PS H:\> $script
\\xxxhsfmsl03\adap\Database\Install\AugKA\DEC16.bat
PS H:\>

(I have inserted "xxx"'s to protect the innocent)

mklement0
  • 382,024
  • 64
  • 607
  • 775
JustJohn
  • 1,362
  • 2
  • 22
  • 44
  • 1
    ArgumentList is quoted with single quotes. Therefore $script wont expand. Use double quotes – Matt Feb 11 '17 at 02:41
  • 1
    If you got this code from [here](http://stackoverflow.com/questions/15305696/running-a-bat-file-as-admin-from-powershell) I understand. I see you left a comment there yesterday. I also left one pointing out the error. – Matt Feb 11 '17 at 02:43

1 Answers1

0

Try the following:

Start-Process powershell `
   -WorkingDirectory (Split-Path $script) `
   -Credential xxx\xxxvis_desktop `
   -ArgumentList '-noprofile', '-command', "
     & { start-process powershell -ArgumentList '-File', '$script' -Verb RunAs }
     "
  • Your primary problem was most likely that the target user - xxx\xxxvis_desktop - lacked permission to access what happened to be the current directory at the time of invocation.

    • Setting the working directory explicitly to a directory the target user is allowed to access should fix that problem - -WorkingDirectory (Split-Path $script) sets the working dir. to the dir. in which the target script is located.
  • Your secondary problem - as pointed out in a comment on the question by Matt - is that the command string you passed to Start-Process was enclosed in '...' (single quotes), causing the embedded $script variable reference not to be expanded (interpolated).

    • Using "..." (double quotes) fixes that problem; note, however, that the command line to pass to the powershell executable is split into individual arguments passed via -ArgumentList - the (literal, single-quoted) options, followed by the (interpolated, double-quoted) command string, which is the preferable way to pass arguments, because it is more robust.

    • Note, how the $string reference inside the command string is enclosed in embedded '...' so as to ensure that when the invoked powershell instance parses the command string, the value of $string is recognized as a single argument (although this happens not to be necessary for the value at hand, \\xxxhsfmsl03\adap\Database\Install\AugKA\DEC16.bat).

      • If there's a chance that the value of $script has embedded ' instances, you must use the following (double the ' instances to escape them):
        $($script -replace "'", "''")
  • The final problem is that you cannot use Start-Process directly on a script - as in the outer call, you need to call powershell and pass it the script filename as an argument.


Additional notes:

  • The & { ... } wrapper around the start-process call in the command string shouldn't be necessary.

  • Generally, you could use a single Start-Process call with -Verb RunAs to elevate the run of $script, but, unfortunately, -Verb RunAs and -Credential cannot be combined, so that means:

    • If the current user is an administrative account, the elevated session will invariably run as that user - you'll just get a yes/no prompt to confirm elevation.
    • Otherwise, the credentials will be prompted for, but you cannot pre-populate the username in that dialog.

    • Start-Process -Verb RunAs powershell -ArgumentList '-noprofile', '-File', $script

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    It works. And you are correct, I cannot pre-populate the username by putting "-Credential myUserNameHere" ::: and thank you very much! – JustJohn Feb 21 '17 at 19:23