在zip中运行exe而不提取

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

我有一个包含安装程序的.zip(setup.exe和相关文件)。如何在不解压缩zip的情况下在PowerShell脚本中运行setup.exe

另外,我需要将命令行参数传递给setup.exe。

我试过了

& 'C:\myzip.zip\setup.exe'

但是我收到了一个错误

...不被识别为cmdlet,函数,脚本文件或可操作程序的名称。

这打开了exe:

explorer 'C:\myzip.zip\setup.exe'

但我不能传递参数。

powershell zip
1个回答
0
投票

你问的是不可能的。您必须解压缩zip文件才能运行可执行文件。 explorer语句仅起作用,因为Windows资源管理器在后台透明地执行提取。

你可以做的是编写一个自定义函数来封装提取,调用和清理。

function Invoke-Installer {
    Param(
        [Parameter(Mandatory=$true)]
        [ValidateScript({Test-Path -LiteralPath $_})]
        [string[]]$Path,

        [Parameter(Manatory=$false)]
        [string[]]$ArgumentList = @()
    )

    Begin {
        Add-Type -Assembly System.IO.Compression.FileSystem
    }

    Process {
        $Path | ForEach-Object {
            $zip, $exe = $_ -split '(?<=\.zip)\\+', 2

            if (-not $exe) { throw "Invalid installer path: ${_}" }

            $tempdir = Join-Path $env:TEMP [IO.File]::GetFileName($zip)
            [IO.Compression.ZipFile]::ExtractToDirectory($zip, $tempdir)

            $installer = Join-Path $tempdir $exe
            & $installer @ArgumentList

            Remove-Item $tempdir -Recurse -Force
        }
    }
}

Invoke-Installer 'C:\myzip.zip\setup.exe' 'arg1', 'arg2', ...

请注意,这需要.Net Framework v4.5或更高版本。

© www.soinside.com 2019 - 2024. All rights reserved.