Start-Process powershell 找不到指定的

Start-Process powershell cannot find specified

Edit5:Adam 的代码可以工作,除非路径中有空格。该解决方案位于 Powershell Opening File Path with Spaces

Edit4:通过路径测试进一步简化。同样的错误。

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

当我调用“powershell .\script.ps1 admin”时的输出是:

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:没关系。以前的解决方案停止工作。脚本是:

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

调用“powershell .\script.ps1 admin”时的错误是:

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

当我现在对脚本路径进行硬编码时,它甚至无法工作,即使删除了“-Verb runAs”。

Edit2:这就解决了,我这两天就是接受不了自己的回答。希望我记得这样做,以防其他人提出这个问题。

Edit1:我的脚本现在是:

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

失败并出现以下错误。但是,如果我硬编码脚本路径并将脚本编写为:

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

成功了。我也试过 ""$myInvocation.MyCommand.Definition"" 无济于事。

原文: 我有一个 powershell 脚本,至少在 Windows 7 中提升了用户,然后 运行 脚本的其余部分。然而,在 Windows 10 中,它给了我:

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"

该脚本存在于此路径中,因为它实际上试图调用自身。我在这里不知所措。

我 运行 在 Windows 10 (v10.0.15063 Build 15063) 上安装 Powershell v5.1.15063.1155。如果我 运行 以下内容:

$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

您可以尝试将此作为解决方法,因为它对我有用。

对于您的问题,我认为您创建的 ProcessStartInfo 对象有问题 ($newProcess)。当找不到作为参数提供的可执行文件名称时,我看到了该错误。例如,如果我 运行 以下内容:

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

我收到您描述的错误:

您确定 Powershell 在您的路径中 (where.exe powershell)?我知道它的范围。