Author Posts

January 21, 2017 at 10:41 pm

Is this a bug? The code works perfectly fine in PS 5.0 (RTM). If someone else can confirm then I'll post to UserVoice.

TestClass.ps1

class MyClass
{
    $prop1
    $prop2
}

Test-TestClass.ps1

. C:\Temp\TestClass.ps1

function Test-MyClass
{
    [CmdletBinding()]
    [OutputType([MyClass],[System.Object[]])]
    Param
    ()

    Write-Host -Object 'Test 123'
}

Test-MyClass

Unable to find type [MyClass].
At C:\Temp\Test-MyClass.ps1:6 char:17
+ [OutputType([MyClass],[System.Object[]])]
+ ~~~~~~~~~
+ CategoryInfo : InvalidOperation: (MyClass:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

January 22, 2017 at 12:01 am

A work around is to add the .ps1 files (with classes defined) to ScriptsToProcess in your .psd1

The concern is that working code in 5.0 is breaking in 5.1

Edit: Cancel that. ScriptsToProcess doesn't work either.

January 24, 2017 at 8:02 am

This works for me:

TestClass.ps1:

class MyClass
{
    $prop1
    $prop2
}

Test-TestClass.ps1

Import-Module .\TestClass.ps1

function Test-MyClass
{
    [CmdletBinding()]
    [OutputType([MyClass],[System.Object[]])]
    Param
    ()

    Write-Host -Object 'Test 123'
}

Test-MyClass

Output:

PS C:\Users\Steve> .\Test-TestClass.ps1
Test 123
PS C:\Users\Steve>

Version Info:

PS C:\Users\Steve> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.693
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.693
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

January 24, 2017 at 8:15 am

Thanks testing Steve. Looks like you have an older build than mine. I have build v5.1.14409.1005. Can you tell me what OS you ran the test on?

January 24, 2017 at 8:18 am

Windows 10 Pro (Desktop) and Windows 10 Enterprise (Work PC)

I'll try updating my version to RTM.... guess I shoulda checked there first =)

January 24, 2017 at 8:23 am

Hmm I wonder if the problem only exists on Win7/8.1/2012R2. If you had either of those available it would be a good test.

January 24, 2017 at 8:41 am

I can try tomorrow on Server 2012R2, but I believe it's running the same version as my PC. I'll let you know.

My point was that I have had success using Import-Module over Invocation.

January 24, 2017 at 11:00 am

I cannot reproduce this on Windows 8.1. Zuldan's code works fine.
Here is my $PSVersionTable:

Name                       Value
----                       -----
PSVersion                  5.1.14409.1005
PSEdition                  Desktop
PSCompatibleVersions       {1.0, 2.0, 3.0, 4.0...}
BuildVersion               10.0.14409.1005
CLRVersion                 4.0.30319.42000
WSManStackVersion          3.0
PSRemotingProtocolVersion  2.3
SerializationVersion       1.1.0.1

January 28, 2017 at 12:44 pm

I cannot reproduce the issue on Windows 7 SP1 using the original code by Zuldan. My $PSVersionTable is identical to George's

January 30, 2017 at 7:43 pm

Thanks for everyone testing and providing feedback I still cannot get classes import. I've run the test code above against 29x Windows 2008 R2 servers and every single one is failing. If no one else is seeing this issue then it has to be something in my environment.

Could you guys tell me what version of .NET you're using? I'm using .NET 4.6.2 on all 2008 R2 servers with PS v5.1.14409.1005.