powershell - Start-Process powershell 找不到指定的

标签 powershell windows-10 powershell-5.0 runas start-process

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);

我收到您描述的错误:

enter image description here

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

关于powershell - Start-Process powershell 找不到指定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51504073/

相关文章:

c++ - Windows 7/10 中的 Winlogon 屏幕捕获

powershell - 如何在PowerShell中将输出重定向到内存而不是文件?

powershell - 如何导入路径中包含空格的CSV文件?

windows - "downloads"文件夹在哪里

powershell - Net.WebClient.DownloadFile从批处理文件下载Google云端硬盘文件无法识别某些字符

ruby-on-rails - 找不到 libxml2 的包配置

mysql - PowerShell - 保存路径中的无效字符

PowerShell 5 中从未处理过的 C# 事件

powershell,如何监控目录并将文件移动到另一个文件夹

windows-10 - 停止 Chrome “restore previous session” 功能