Recently have been working on a script that extracts the contents of a file and puts that contents on a server unzipped.
I’ve run into an issue where the script will bomb because it cannot find types that are clearly loaded in the script.
Here is the issue and question:
If I run this script in a new session I get the following error everytime:
At C:\temp\copycode.ps1:89 char:13
-
hidden [ZipArchiveEntry]$entry
-
~~~~~~~~~~~~~~~
Unable to find type [ZipArchiveEntry].
At C:\temp\copycode.ps1:91 char:18
-
ArchiveFile([ZipArchiveEntry]$entry) {
-
~~~~~~~~~~~~~~~
Unable to find type [ZipArchiveEntry].
At C:\temp\copycode.ps1:100 char:10
-
[ZipFileExtensions]::ExtractToFile($this.entry, $file.FullNam ...
-
~~~~~~~~~~~~~~~~~
Unable to find type [ZipFileExtensions].
+ CategoryInfo : ParserError: ( , ParseException
+ FullyQualifiedErrorId : TypeNotFound
However if I add the following in a separate file and then call this script it runs with out issue. Why won’t the using statements and the add-type work in the same script.
This way works:
param(
[Parameter(Mandatory=$true)][string]$sourceZip,
[Parameter(Mandatory=$true)][string]$destPath
)
[void][Reflection.assembly]::LoadWithPartialName(‘System.IO.Compression’)
[void][Reflection.assembly]::LoadWithPartialName(‘System.IO.Compression.FileSystem’)
add-type -assemblyname ‘System.IO.Compression’
add-type -assemblyname ‘System.IO.Compression.FileSystem’
& .\copy-code.ps1 -sourcezip $sourceZip -destpath $destPath
this doesnt
.\copy-code -sourcezip c:\temp\somezipfile.zip -destpath c:\temp\somezip
copy-code.ps1 — Full source is in gist below: