简体   繁体   中英

Run exe in zip without extracting

I have a .zip containing an installer (setup.exe and associated files). How can I run setup.exe in a PowerShell script without extracting the zip?

Also, I need to pass command line parameters to setup.exe.

I tried

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

but I get an error

... not recognized as the name of a cmdlet, function, script file, or operable program.

This opens the exe:

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

but I cannot pass parameters.

What you're asking is not possible. You must extract the zip file in order to be able to run the executable. The explorer statement only works because the Windows Explorer does the extraction transparently in the background.

What you can do is write a custom function to encapsulate extraction, invocation, and cleanup.

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', ...

Note that this requires .Net Framework v4.5 or newer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM