powershell:运行不带扩展名的可执行文件

问题描述 投票:0回答:1

我有一个 powershell 脚本,需要运行许多可执行文件。

可执行文件没有扩展名,并且无法重命名。

它们的格式都是

<something>_test

当我尝试使用 powershell 运行它们时:

& $test_bin | Tee-Object -FilePath $log_output_file

失败并出现错误

Cannot run a document in the middle of a pipeline

我尝试设置 PATHEXT (

$env:PATHEXT="$env:PATHEXT;_test"
) 但这没有帮助。我猜它只支持实际的扩展。

有没有办法告诉Windows某个特定文件是可执行文件,或者告诉powershell执行一个文件,即使它认为它是一个文档?

windows powershell executable
1个回答
0
投票

示例:

# Define a function which executes any file
function Start-ProcessEx ($filePath)
{
    $psi = [System.Diagnostics.ProcessStartInfo]::new()
    $psi.FileName = $filePath
    $psi.UseShellExecute = $false # This is the key to run non-executable file
    return [System.Diagnostics.Process]::Start($psi)
}

Start-ProcessEx -filePath 'C:\Program Files\Speedtest\Speedtest.randomextension'
© www.soinside.com 2019 - 2024. All rights reserved.