I am using PowerShell 5.1 on a Windows 7 box and am trying to reference a type in a .NET assembly from inside a PowerShell class, and am getting an "Unable to find type" error.
My "main" program is FooProg.ps1, where I am creating a new instance of the FooMod class
#Requires -Version 5.1
using module .\FooMod\FooMod.psm1
[FooMod]::new() | out-null
The FooMod class is in a PowerShell module (FooMod.psm1) in the FooMod sub-folder
#Requires -Version 5.1
using assembly .\Foo.dll
using namespace Foo
class FooMod {
FooMod() {
Write-Host "Start - Assembly access"
[Bar] $bar = [Bar]::new()
Write-Host $bar.Name
Write-Host $bar.Greeting()
Write-Host "Finish - Assembly access"
}
}
My Foo.dll is built from the following Bar.cs, and is in the FooMod sub-folder along with the Module file (FooMod.psm1) and the Module Manifest file (FooMod.psd1)
namespace Foo {
public class Bar {
public string Name;
public Bar(string name) => this.Name = name;
public Bar() : this("Bar") {
}
public string Greeting() {
return System.String.Format("Hello! My name is {0}", Name);
}
}
}
I also have a Module Manifest in the FooMod sub-folder called FooMod.psd1 which contains these relevant entries
RootModule = 'FooMod.psm1'
RequiredAssemblies = 'Foo.dll'
Is there anything else I am missing? Using the assembly outside the class works fine, instantiating a PowerShell class (without referencing an assembly / external type) works fine - but combining PowerShell classes and assembly types is a no-go!