3

Edit5: Adam's code works unless there are spaces in the path. That solution is at Powershell Opening File Path with Spaces

Edit4: Simplified further with a test for the path. Same Error.

If ($args[0] -eq "admin")
{
    $TheScriptPath = "C:\Users\root\Desktop\script.ps1"
    Test-Path ($TheScriptPath)
    Start-Process "powershell -noexit" $TheScriptPath
}
Else { Write-Host "OK" }

Output when I call "powershell .\script.ps1 admin" is:

True

Start-Process : This command cannot be run due to the error: The system cannot find the file specified.

At C:\Users\root\Desktop\script.ps1:11 char:2

Edit3: Nevermind. Previous solution stopped working. Script is:

if ($args[0] -eq "admin)
{
    $TheScriptPath = $myInvocation.MyCommand.Definition
    Start-Process powershell -Verb runAs -Workingdirectory $PSScriptroot $TheScriptPath
    exit
}
Write-Host "Ok"

Error when I call "powershell .\script.ps1 admin" is:

Start-Process : This command cannot be run due to the error: The system cannot find the file specified.

At C:\Users\root\Desktop\script.ps1:11 char:2

It's not even working when I hard-code the script path now, even with "-Verb runAs" removed.

Edit2: This is solved, I just can't accept my own answer for two days. Hopefully I remember to do that in case someone else comes along with this question.

Edit1: My script now reads:

If ($args[0] -eq "go")
{
    $ThePath = $myInvocation.MyCommand.Definition
    Start-Process powershell -Verb runAs $ThePath
    Exit
}
Write-Host "OK"

It fails with the error below. However, if I hard-code the script path and write the script as:

If ($args[0] -eq "go")
{
    Start-Process powershell -Verb runAs C:\Users\root\Desktop\script.ps1
    Exit
}
Write-Host "OK"

It succeeds. I've also tried ""$myInvocation.MyCommand.Definition"" to no avail.

Original: I have a powershell script that, at least in Windows 7, elevated the user and then ran the rest of the script. In Windows 10, however, it's giving me:

Exception calling "start" with "1" argument(s): "The system cannot find hte file specified"

At C:\Users\root\desktop\script.ps1:15 char:2

If ($True)
{
    # We are not running "as Administrator" - so relaunch as administrator

    # Create a new process object that starts PowerShell
    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter
    $newProcess.Arguments = $myInvocation.MyCommand.Definition;

    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process
    [System.Diagnostics.Process]::Start($newProcess);

    # Exit from the current, unelevated, process
    exit
}

Write-Host "Ok"

The script exists at this path, as it actually attempting to invoke itself. I'm at a loss here.

Community
  • 1
  • 1
Logan Jones
  • 324
  • 1
  • 3
  • 10

1 Answers1

2

I'm running Powershell v5.1.15063.1155 on Windows 10 (v10.0.15063 Build 15063). If I run the following:

$context = [Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()

if (-not $context.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process powershell -Verb runAs -ArgumentList $myInvocation.MyCommand.Definition
    exit
}

Get-Date
Sleep -Seconds 4

You can try this as a workaround as it works for me.

To your question, I'd think something is wrong with the the ProcessStartInfo object you created ($newProcess). I've seen that error when the executable name supplied as a parameter can't be found. For example, if I run the following:

$newProcess = new-object System.Diagnostics.ProcessStartInfo "cocain";
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
$newProcess.Verb = "runas";
[System.Diagnostics.Process]::Start($newProcess);

I get the error you described:

enter image description here

You're sure Powershell is in your path (where.exe powershell)? I know its a reach.

Adam
  • 3,891
  • 3
  • 19
  • 42
  • Powershell is definitely in path. Also, I changed ti to use Start-Process so the only call is "Start-Process powershell -Verb runAs $PSScriptroot. It fails with the same error, but it works if I hard-code the script path. – Logan Jones Jul 24 '18 at 18:39
  • Still no go even after the change to start-process? – Adam Jul 24 '18 at 18:40
  • 1
    Already tried with the full path to powershell, and it didn't fix it. I actually have the solution posted, I just can't accept the answer for two days because that makes sense to the stackoverflow admins. – Logan Jones Jul 24 '18 at 18:58
  • I ended up going with your solution. Start process line should be start-process -FilePath powershell.exe -ArgumentList "-file \`"$($filepath.path)\`"" to work if there are spaces in the path. Thanks for the help. – Logan Jones Jul 24 '18 at 21:08