简体   繁体   中英

Powershell error when using loaded assembly

I'm having a problem with using zip compression in a Powershell script. The code snippet in question is:

$zipfile = $targetFile
$file   = 'Script.ps1'

$stream = New-Object IO.FileStream($zipfile, [IO.FileMode]::Open)
$mode   = [System.IO.Compression.ZipArchiveMode]::Update
$zip    = New-Object IO.Compression.ZipArchive($stream, $mode)

($zip.Entries | ? { $file -contains $_.Name }) | % { $_.Delete() }

#   Add a newer Script.ps1 file with the new Comment Based Help template.
$newFile = "$PSScriptRoot\$file"
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$newFile,"Script.ps1","optimal")

#   Clean up.
$zip.Dispose()
$stream.Close()
$stream.Dispose()

The code attempts to delete a file from the archive and then add a newer version of the same file. When I run the script, I receive the following:

[ERROR] Unable to find type [System.IO.Compression.ZipArchiveMode]. Make sure that the [ERROR] assembly that contains this type is loaded. [ERROR] At C:\\xxxxx\\xxxxx\\xxxxx\\PowerShellIDEInstallers\\PowerShel [ERROR] lIDEInstallers\\VSInstallCBH.ps1:141 char:2 [ERROR] + $mode = [System.IO.Compression.ZipArchiveMode]::Update [ERROR] +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [ERROR] + CategoryInfo : InvalidOperation: (System.IO.Compression.ZipArch [ERROR] iveMode:TypeName) [], RuntimeException [ERROR] + FullyQualifiedErrorId : TypeNotFound [ERROR]

However, if I run it again, it will work correctly. I found a few posts ( this and this ) that spoke of similar problems. I'm currently using:

Add-Type -AssemblyName System.IO.Compression.FileSystem

At the top of the script. I also found this post which looked promising, but, did not work. I should also add that the problem occurs in the ISE, Visual Studio, and the command prompt. The code will work if I run it a second time in any of the environments.

I'm baffled and at a loss. Can anyone tell me why this is happening?

You're close. You need to load one more assembly in this case. Use:

Add-Type -AssemblyName System.IO.Compression

Dave's answer was a partial clue to the resolution. Changing the Add-Type command as he suggested made things better. However, the code still failed because the change he suggested now caused the command:

[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$newFile,"Script.ps1","optimal")

to fail.

I was able to correct that and fix the problem once I found:

To use the extension methods, you must reference the System.IO.Compression.FileSystem assembly in your project.

With Dave's suggestion, I simply added the following to correct my problem:

Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem

The code now works correctly the first time.

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