I am working with the IO.Compression namespace in PowerShell and am running into an issue where Methods which are available in my C# code are not available in PowerShell
C# snippet
string archfile = @"c:\temp\a1.zip";
string source = @"C:\temp\testing\logs\BatchProcess\BatchProcess_2017_08_22.log";
using (ZipArchive archive = ZipFile.Open(archfile, ZipArchiveMode.Update))
{
archive.CreateEntryFromFile(source, @"myfolder\folder2\file.log");
}
Similar start of code in PowerShell
Add-Type -assembly System.IO.Compression.FileSystem
$archfile = "c:\temp\a1.zip"
# open mode: 1: Create, 2: Update
$archive = [System.IO.Compression.ZipFile]::Open("C:\temp\2.zip", 2)
$archive.CreateEntryFromFile($source, "myfolder\folder2\file.log")
A review of members shows that the function CreateEntryFromFile() (among others) is missing. The variable is a type of ZipArchive and a new file is created.
$archive | Get-Member displays:
TypeName: System.IO.Compression.ZipArchive
Name MemberType Definition
---- ---------- ----------
CreateEntry Method System.IO.Compression.ZipArchiveEntry CreateEntry(string entryName), System.IO.Compression.ZipArchiveE...
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetEntry Method System.IO.Compression.ZipArchiveEntry GetEntry(string entryName)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Entries Property System.Collections.ObjectModel.ReadOnlyCollection[System.IO.Compression.ZipArchiveEntry] Entries {get;}
Mode Property System.IO.Compression.ZipArchiveMode Mode {get;}
Is this simply part of how PowerShell creates .NET objects where not all methods are (or can be) supported?
ZipArchive reference: https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx
CreateEntryFromFileis an extension method. Note you can also call extension methods directly because they are static[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($archive, $source, "myfolder\folder2\file.log").