在zip中运行exe而不提取

时间:2018-03-05 01:27:27

标签: powershell zip

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

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

我试过

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

但是我收到了错误

  

...无法识别为cmdlet,函数,脚本文件或可运行程序的名称。

这会打开exe:

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

但我无法传递参数。

1 个答案:

答案 0 :(得分: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或更高版本。

相关问题