Powershell 5.1 - Cannot find type [MyType]: verify that the assembly containing this type is loaded

2.2k views Asked by At

I have a generic module MyModule.psm1 which creates and returns any custom type provided:

function Create-CustomType {
    param (
        [Parameter(Mandatory)]
        [System.Reflection.TypeInfo] $CustomType
    )
    // Some irrelevant logic ..................................................//
    
    return New-Object -TypeName $CustomType.FullName -Property $objectProperties
}

Also, I have some custom types, defined in the separate file types.ps1:

class User{
    // Some properties
}

Last, but not least I have a RunMyLogic.ps1 script, which looks like that:

Import-Module -Name "$pathToMyCustomModulesFolder\\MyModule.psm1"
. ($PSScriptRoot + '\types.ps1')
# New-Object -TypeName $CustomType.FullName -Property $objectProperties
Create-CustomType -CustomType ([User])

The problem is that when I run RunMyLogic.ps1, my Create-CustomType function fails with the message Cannot find type [User]: verify that the assembly containing this type is loaded.

If I create a User directly in RunMyLogic.ps1 then it works just fine (see commented line). But when I pass it to the MyModule.psm1 it fails. Please, help to figure out what's wrong with it

0

There are 0 answers