jeudi 2 février 2017

Using Reflection within Powershell Class

Good Evening, I am Testing out Powershell Classes in V5 and I am unable to use reflection within a Powershell class. Take the below example:

class PSHello{
    [void] Zip(){
        Add-Type -Assembly "System.IO.Compression.FileSystem"
        $includeBaseDirectory = $false
        $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
        [System.IO.Compression.ZipFile]::CreateFromDirectory('C:\test', 'c:\test.zip',$compressionLevel ,$includeBaseDirectory)
    }
}
$f = [PSHello]::new()
$f.Zip()

As we can see, i am loading the Assembly and then Using Reflection to Create a zip of a directory. However when this is run i recieve the error of

Unable to find type [System.IO.Compression.ZipFile].
+ CategoryInfo          : ParserError: (:) [],        ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound

Now if i run the same contents of my Zip Method outside of the class it works. So why cannot Reflection be used like this inside of a class?





Aucun commentaire:

Enregistrer un commentaire